UVA - 10706 Number Sequence

先找到是在哪个集合内,再找到是集合内的哪个元素,最后找到元素的第几位数

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
long long table[100010];
long long psum[100010];
int n=100000;
void maketable()
{
    int i,flag=1,x=0;
    for(i=1;i<=n;i++)
    {
        if(i%flag==0)
        {
            flag*=10;
            x++;
        }
        table[i]=table[i-1]+x;
        psum[i]+=table[i]+psum[i-1];
    }
}
int main()
{
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);
    maketable();
    int T,i,t;
    long long x;
    char s[100];
    cin>>T;
    while(T--)
    {
        cin>>x;
        i=lower_bound(psum,psum+n,x)-psum;
        if(psum[i]==x)
            cout<<i%10<<endl;
        else
        {
            x-=psum[i-1];
            i=lower_bound(table,table+n,x)-table;
            if(table[i]==x)
                cout<<i%10<<endl;
            else
            {
                x-=table[i-1];
                sprintf(s,"%d",i);
                cout<<s[x-1]<<endl;
            }
        }
    }
    return 0;
}

UVA - 10706

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1647" target="_blank">Number Sequence

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

id=21357">Submit
Status

Description

Problem B

Number Sequence

Input: standard input

Output: standard output

Time Limit: 1 second

A single positive integer iis given. Write a program to find the digit located in the position
iin the sequence of number groups S1S2…Sk. Each group
Skconsists 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 <=25), 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                           Output for Sample Input


2

8

3


2

2


Problem source: Iranian Contest

Special Thanks: Shahriar Manzoor, EPS.

Source

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 4. Algorithm Design

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Problem Solving Paradigms ::
option=com_onlinejudge&Itemid=8&category=316">
Divide and Conquer -Binary Search

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Problem Solving Paradigms :: Divide and Conquer ::
Binary Search

时间: 2024-10-11 22:23:53

UVA - 10706 Number Sequence的相关文章

uva 10706 Number Sequence(找规律)

uva 10706 Number Sequence A single positive integer iis given. Write a program to find the digit located in the position iin the sequence of number groups S1S2-Sk. Each group Skconsists of a sequence of positive integer numbers ranging from 1 to k, w

【矩阵快速幂】UVA 10698 G - Yet another Number Sequence

[题目链接]click here~~ [题目大意] Let's define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n-1) + f(n-2), n > 1 When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values of a and b , you ca

UVA - 10689 Yet another Number Sequence 矩阵快速幂

                  Yet another Number Sequence Let’s de?ne another number sequence, given by the following function:f(0) = af(1) = bf(n) = f(n − 1) + f(n − 2), n > 1When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values

Uva10689 Yet another Number Sequence ( 矩阵快速幂 )

Uva 10689Yet another Number Sequence(  矩阵快速幂  ) 题意: 就是矩阵快速幂,没什么好说的. 分析: 其实还是斐波那契数列.只是最后对应的矩阵不是(1,1)是(a,b)了 MOD = 1; for( int i = 0; i < m; ++i ) MOD *= 10; 代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace s

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

TOJ 1203: Number Sequence

1203: Number Sequence Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 957            Accepted:208 Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Gi

ACM—Number Sequence(HDOJ1005)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 主要内容: A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). 看到这样的公式很容易想到递归调用求解,但是在本题中n的取