poj 1019 Number Sequence 第i位上的数字 (组合数学)
2016-06-11 00:23
429 查看
Number Sequence
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
Sample Output
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;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 37699 | Accepted: 10890 |
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;
}
相关文章推荐
- 严重: Exception occurred during processing request: null原因解析
- UIBezierPath+CAShapeLayer绘图
- POJ1947 Rebuilding Roads
- ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet
- 快速构建不同UI样式的列表适配器方法-AbsAdapter
- Nutch抓取错误——java.sql.BatchUpdateException: Incorrect string value: '\xF2\xA3\xAC\xB7\xEF\xBF.
- android 进阶之UI
- Leetcode Repeated DNA Sequences
- QT模态对话框用法(在UI文件中设置Widget背景图,这个图是一个带阴影边框的图片——酷)
- POJ 2524 Ubiquitous Religions (简单并查集,三种方式)
- java.util.AbstractStringBuilder源码分析
- QNetworkAccessManager跳转URL处理(使用QNetworkRequest::RedirectionTargetAttribute获得跳转URL)
- 跨平台生成GUID/UUID
- UITabBarController+UINavigationController+UIToolBar
- GUIDE D'IONIC [Français]
- GUI_POINT的坐标
- leetcode---Unique Paths---动规
- POJ 1947 Rebuilding Roads(树形DP)
- 响应式布局基本实现Media Query(媒介查询)
- 【Arduino官方教程第一辑】示例程序 6-1 读取ADXL3xx加速度计