| Task: | Kyselyt |
| Sender: | Yytsi |
| Submission time: | 2017-10-12 15:08:59 +0300 |
| Language: | C++ |
| Status: | READY |
| Result: | 100 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 12 |
| #2 | ACCEPTED | 25 |
| #3 | ACCEPTED | 63 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.05 s | 1 | details |
| #2 | ACCEPTED | 0.05 s | 2 | details |
| #3 | ACCEPTED | 0.06 s | 3 | details |
Code
/*
Written by, Tuukka Yildirim.
input: q -> 1 <= q <= 1000
q lines, where 1 <= k <= 10**18 (** denotes exponentiation).
In other words, maximum value of k is: 1000000000000000000.
I generated a lookup table <tenPowerIndexes>. The table contains data
that yields how many digits are in the range of <k>. For example,
10 -> at this point, numbers start to contain 2 digits.
190 -> at this point, numbers start to contain 3 digits.
... and so on. This array can be generated with the function <generatePowerIndexes()>.
For arbitrary sized integers, the function could be trivially modified to work for up to infinity.
<howManyDigits(k)> fetches this data, based on <k>.
The idea is to find, which power of 10 digit <k> is pointing to, and subsequently calculate it.
By following the comments in the function <findDigit(k)> and analyzing the math, you'll get the picture.
*/
#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))) + 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: ACCEPTED
| 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: ACCEPTED
| input |
|---|
| 1000 615664 916441 627600 279508 ... |
| correct output |
|---|
| 1 2 3 2 2 ... |
| user output |
|---|
| 1 2 3 2 2 ... Truncated |
Test 3
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 1000 672274832941907421 260504693279721732 646999966092970935 100853063389774434 ... |
| correct output |
|---|
| 7 2 2 0 9 ... |
| user output |
|---|
| 7 2 2 0 9 ... Truncated |
