CSES - Datatähti 2018 alku - Results
Submission details
Task:Kyselyt
Sender:Yytsi
Submission time:2017-10-02 22:18:37 +0300
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.05 s1details
#20.05 s2details
#30.04 s3details

Code

#include <iostream>
#include <string>

using namespace std;
typedef long long ll;
ll tenPowerIndexes[17] = {10LL, 190LL, 2890LL, 38890LL, 488890LL,
						5888890LL, 68888890LL, 788888890LL,
						8888888890LL, 98888888890LL, 1088888888890LL, 11888888888890LL,
						128888888888890LL, 1388888888888890LL, 14888888888888890LL,
						158888888888888890LL, 1688888888888888890LL};
			
ll customPow(ll base, ll exponent)
{
	if (exponent == 0LL) return 1LL;
	ll res = base;
	while (exponent != 1LL)
	{
		res *= base;
		exponent--;
	}
	return res;
}
			
// This is a function that could generate the list <tenPowerIndexes>.
void generatePowerIndexes()
{
	ll prevSum = 0LL;
	
	for (ll i = 0LL; i < 17LL; i++)
	{
		ll range = 9LL * customPow(10LL, i);
		ll digCount = i + 1LL;
		
		ll newSum = prevSum + (range * digCount);
		prevSum = newSum;
		tenPowerIndexes[i] = newSum + 1;
	}
}

ll howManyDigits(ll k)
{
	ll digits = 1LL;
	for (int i = 0; i < 17; i++)
	{
		if (k < tenPowerIndexes[i]) break;
		else digits++;
	}
	
	return digits;
}

ll findDigit(ll k)
{
	if (k < 10LL) return k;
	
	ll d = howManyDigits(k);
	// d-space is now calculated.
	
	// Normalized index starting from the d-digit number.
	// ... 97 98 99 100 101 102 ...
	// For example, 100 will have the index 0 ==> p.
	ll p = k - tenPowerIndexes[d - 2];
	
	
	// Let's calculate what power of 10 digit are we trying to find (10**0, 10**1 etc).
	ll region = (d - 1) - (p % d);
	
	if (region == (d - 1))
	{
		// +1 required.
		return (p / (d * customPow(10LL, d))) + 1;
	}
	
	return (p / (d * customPow(10LL, region))) % 10;
}

int main() 
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int q;
	cin >> q;
	cin.ignore();
	
	for (int i = 0; i < q; i++)
	{
		ll search;
		cin >> search;
		cin.ignore();
		cout << findDigit(search) << "\n";
	}
	
	return 0;
}

Test details

Test 1

Group: 1

Verdict:

input
1000
582
214
723
273
...

correct output
0
1
7
7
6
...

user output
0
1
7
7
6
...
Truncated

Test 2

Group: 2

Verdict:

input
1000
615664
916441
627600
279508
...

correct output
1
2
3
2
2
...

user output
1
2
3
2
1
...
Truncated

Test 3

Group: 3

Verdict:

input
1000
672274832941907421
260504693279721732
646999966092970935
100853063389774434
...

correct output
7
2
2
0
9
...

user output
7
2
2
0
9
...
Truncated