∞ 等差等比前n项和

链接:https://ac.nowcoder.com/acm/contest/624/C
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

∞, the infinity. It's one of the most complex math symbols. How does it mean? The mathematicians argued for many years. But luckily, the principle of infinity is stable now.

Ramen is doing some boring works on a kind of number string SnSn. To produce SnSn, write down all numbers from 1 to n in ascending order, then concatenate them directly. For example, S7=1234567S7=1234567 and S15=123456789101112131415S15=123456789101112131415. 

Especially, the infinity number string is: S∞=12345678910111213⋯S∞=12345678910111213⋯.

Ramen likes infinity, but he is bad at counting. He wants to know what the digit is at the position p in the infinity number string S∞S∞. Can you help him to solve this complex problem?

输入描述:

The input contains multiple test cases.

The first line is an integer T(1 <= T <= 100000), which represents the number of test cases.

Each of the next T lines contains an integer p(1 <= p <= 1e18), represents the position has been asked.

输出描述:

For each test case, output the digit at position p in S∞S∞ in one single line.

示例1

输入

复制

扫描二维码关注公众号,回复: 5958070 查看本文章
5
1
10
19
66
1000000000000000000

输出

复制

1
1
4
3
3

说明

 

The subsequence of S∞S∞ from 60 to 70 is:

⋯53637383940⋯⋯53637383940⋯

It's apparent that it's 3 when p=66.

题解:等差等比前n项和, 然后先判断是哪个数,然后找出哪一位即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        int pos=0;
        ll cnt=1;
        for(int i=1;i<=17;i++)
        {
            cnt=cnt*10;
            pos=i;
            if((cnt*(9*i-1)+1)/9>=n)
            {
                break;
            }
        }  
        n-=(cnt/10*(9*(pos-1)-1)+1)/9;
    //  cout<<cnt<<" "<<n<<endl;
        ll m1=n/pos;
        ll m2=n%pos;
    //  cout<<m1<<" "<<m2<<endl;
        if(m2==0)
        {
            m1--;
            m2=pos;
        }
    //  cout<<m1<<" "<<m2<<endl;
        cnt/=10;
        cnt+=m1-1;
        cnt++;
        pos=pos-m2;
        while(pos)
        {
            cnt/=10;
            pos--;
        }
        cout<<cnt%10<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/89298752