您的位置:首页 > 产品设计 > UI/UE

poj 1019 Number Sequence 第i位上的数字 (组合数学)

2016-06-11 00:23 429 查看
Number Sequence

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 37699 Accepted: 10890
Description

A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...Sk. Each group Sk consists of a sequence of positive integer numbers ranging from 1 to k, written one after another. 

For example, the first 80 digits of the sequence are as follows: 

11212312341234512345612345671234567812345678912345678910123456789101112345678910
Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by one line for each test case. The line for a test case contains the single integer i (1 ≤ i ≤ 2147483647)
Output

There should be one output line per test case containing the digit located in the position i.
Sample Input
2
8
3

Sample Output
2
2

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define len(x) ( log10(x*1.0)+1 )
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 31268 ;
int a[maxn+10];
void pre()
{
for(int i=1;i<=maxn+1;i++)
{
a[i]=a[i-1]+len(i);
}
}

int work(ll x)
{
ll s=0;int p;
for(int i=0;i<=maxn;i++)//从0开始
{
s+=a[i];
if(s+a[i+1]>=x)
{
p=i;
break;
}
}
ll leave=x-s;
s=0;
for(int i=1;i<=p+1;i++)
{
s+=len(i);
if(s>=leave)
{
p=i;
break;
}

}
return p/(int)( pow(10.0,1.0*s-leave)+0.5)%10;

}
int main()
{
pre();
ll x;
int T;scanf("%d",&T);
while(T--)
{
scanf("%lld",&x);
printf("%d\n",work(x));
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: