Ertugrul_'s blog

By Ertugrul_, history, 4 years ago, In English

in cses online judge a introductory catagory problem digit queries (http://https://cses.fi/problemset/task/2431) when i submit this problem solutin i got wrong answer in test case 3 , 4 . I think you know guys cses online judge provided test case like codeforces . so I want to test it locally , Unfortunately i noticed that in test case 4 in codeforces compiler and my pc found output 6 for input 158888888888888841 but, in cses show user output 7. my solution is correct or wrong it's not problem why two place this two different output. what should be the problem....

input: 1
158888888888888841

cses output: 7

codeforces and my pc output: 6

this is my code :

#include <bits/stdc++.h>
using namespace std;
 
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); 
 
    long long q, cur = 0, t = 1 ; 
    vector <long long> pre ; 
    pre.push_back(0) ; 
    for (long long l = 1, r = 10,c=1 ; cur < 1e18 ; l *= 10, r*= 10,c++) {
        pre.push_back((r-l)*c) ; 
        cur += (r-l)*c ; 
        ++t ; 
    } 
    for (int i=1;i<t;++i) pre[i] += pre[i-1] ; 
    cin >> q ; 
    while (q--) {
        long long nth ; 
        cin >> nth ; 
        int x = lower_bound(pre.begin(), pre.end(), nth) - pre.begin() ; 
        if (pre[x] > nth) x-- ; 
        if (pre[x] == nth) { 
            cout << 9 << endl ; 
            continue ; 
        } 
        long long dif = nth - pre[x], digit = x+1 ; 
        long long num = pow(10, digit-1) - 1 + (dif-1)/digit ; 
        dif = dif - (dif-1)/digit * digit ; 
        num++ ; 
        int a[digit] = {}, i = digit-1 ; 
        while (num > 0) {
            a[i--] = num%10 ; 
            num /= 10 ; 
        }
        cout << a[dif-1] << endl ; 
    }
    
    return 0;
}

// sorry for my poor english...

  • Vote: I like it
  • -17
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Pro tips: don't use pow function.

Here is the reason: Link

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it
You should post your code like this bro...