CSES - Datatähti 2018 alku - Results
Submission details
Task:Kyselyt
Sender:HeikkiSimojoki
Submission time:2017-10-14 19:48:01 +0300
Language:C++
Status:READY
Result:37
Feedback
groupverdictscore
#1ACCEPTED12
#2ACCEPTED25
#30
Test results
testverdicttimegroup
#1ACCEPTED0.05 s1details
#2ACCEPTED0.06 s2details
#30.10 s3details

Code

#include <iostream>
#include <cmath>

//Amount of digits in a line where the greatest number is the greatest number which still has n digits
unsigned long amtDigits(unsigned long n){
    if(n == 1) return 9;
    //Largest number
    unsigned long k = pow(10, n) - 1;

    //Substract the numbers where there are less than n digits, multiply by length of one number,
    //and add the length of the subtracted numbers
    k -=  pow(10, n - 1) - 1;
    k *= n;
    k += amtDigits(n - 1);

    return k;
}

unsigned long getNum(unsigned long n){
    if(n < 10) return n;

    
    unsigned long i = 2;
    while(true){
        // See if n is in the range of numbers with i digits
        unsigned long bottom = amtDigits(i - 1);
        unsigned long top = amtDigits(i);
        
        if(bottom <= n && n <= top){
            //count n from bottom
           
            n = n - bottom - 1;
            
            //The number n is pointing in
            unsigned long num = pow(10, i-1) + n / i;
            
            //The requested digit of said number
            unsigned long dig = n - (( n / i ) * i);

            //Index the digit backwards
            dig = i - dig;

            //Get the digit
            return (num % (unsigned long)pow(10, dig)) / (unsigned long)pow(10, dig - 1); 
        }
        i++;
    }
}


 int main(){
    int numCommands;
    std::cin >> numCommands;


    unsigned long commands[numCommands];

    for(int i = 0; i < numCommands; i++){
        std::cin >> commands[i];
    }
    
    for(int i = 0; i < numCommands; i++){
        std::cout << getNum(commands[i]) << "\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:

input
1000
672274832941907421
260504693279721732
646999966092970935
100853063389774434
...

correct output
7
2
2
0
9
...

user output
3
8
4
0
0
...
Truncated