-
2018-02-15 02:26:14
Number Sequence
Describe
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:
11212312341234512345612345671234567812345678912345678910123456789101112345678910Input
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
注意,是求第i位的数字而不是数,例如12345678910这个序列中第10位数字是1而不是10。我们可以将整个序列分成一个个以数1开始的小序列,利用cmath中的log10函数可以计算出一个数的位数,用数组存放小序列的位数,从而确定第i位所在小序列,进而确定它在哪一个数的哪一位上,最后输出即可。
#include<iostream> #include<cmath> #define N 32000 using namespace std; //小心溢出 unsigned int a[N]={0,1}; unsigned int b[N]={0,1}; int main() { for(int i=2;i<N;i++) { a[i]=a[i-1]+log10((double)i)+1;//log10(n)+1即n的位数 b[i]=b[i-1]+a[i]; } int t; cin>>t; while(t--) { int i,j,n,sum=0; cin>>n; for(i=1;i<N;i++)//定位所在序列 { if(b[i]>=n) break; } n-=b[i-1]; for(j=1;j<N;j++)//定位所在数 { sum+=log10((double)j)+1; if(sum>=n) break; } n=sum-n; while(n--) j/=10;//将所求数位置于个位 cout<<j%10<<endl; } return 0; }
更多相关内容 -
Number Sequence 数字序列问题
2020-01-06 11:48:52A 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 ... -
Number Sequence/数字序列
2019-07-02 09:27:30一:杭电原题摘录 http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2§ionid=2&problemid=8 二....很容易就能想到递归,但是超出内存 ...int fac(int a,int b,int n)//超出内存 ... f...一:杭电原题摘录
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2§ionid=2&problemid=8
二.题目分析
很容易就能想到递归,但是超出内存
int fac(int a,int b,int n)//超出内存 { int f; if (n== 1||n==2) f=1; else f=(a*fac(a,b,n-1)+b*fac(a,b,n-2) )%7; return f; }
因为f(n)的值要对7取余,所以不难想到f(n)的值可能存在周期.
那我们就去找周期,看是否存在?
周期不就是一直重复T个数,那么我们就说这组数存在周期,且为T.
在这个问题中具体化为,如果存在一个数T,使得f(T)=f(T+1)=1,那不说明开始重复了吗.(代码中,break跳出的i是T+2,不想改了)
三.我的收获
周期是一般规律的一种,上一篇分蛋糕是递归同样是对规律的科学精准预测,周期的发现往往使问题变得简单.当然也不会重复使用一些数,致使空耗内存.
四.AC代码
#include <iostream> using namespace std; int main(int argc, char** argv) { int A,B,n,i,T; int a[1000]; cin >> A >> B >> n; while( A!=0 || B!=0 || n!=0) { a[2]=1; a[1]=1; for(i=3;i<1000;i++){ a[i] = ( A * a[i-1] + B * a[i-2]) % 7; // cout<<"a["<<i<<"]"<<"="<<a[i]<<endl; if(a[i] == 1 && a[i-1] == 1) break; } //cout<< "T:"<<i<<endl; T=i-2; if(n%T==0) cout<<a[T]<<endl; else cout<<a[n%T]<<endl; cin >> A >> B >> n; } return 0; }
-
Number Sequence (正解:矩阵快速幂)
2018-07-28 11:15:39Number Sequence 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...Number Sequence
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 contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input1 1 3 1 2 10 0 0 0
Sample Output
2 5
题意:
题意很简单就是给你一个数n,让你求F(n)
分析:
网上有很多博客上写找规律,找循环节,但是当你们问他为什么这个周期是这些,却很难回答。如果它真的具有规律并具有固定周期,一定可以通过某种方法证明,只不过我们不会,但对于这个题目应该是没法证明的,看hduoj的discuss中就给了下面几组测试数据:
input
247 602 35363857
376 392 9671521
759 623 18545473
53 399 46626337
316 880 10470347
0 0 0
output
4
3
5
2
3
这几组测试数据如果是通过所谓规律AC的代码,有几个实际上是不对的因此考虑到斐波那契数列的性质,这个题和斐波那契数列很类似,发现我们应该使用矩阵快速幂来做,矩阵很小,模板直接写。
(f(3)f(2))=(a1b0)(f(2)f(1))(161) (161) ( f ( 3 ) f ( 2 ) ) = ( a b 1 0 ) ( f ( 2 ) f ( 1 ) )
f(3)=a+b f ( 3 ) = a + b
(f(n)f(n−1))=(a1b0)n−2(f(2)f(1))(162) (162) ( f ( n ) f ( n − 1 ) ) = ( a b 1 0 ) n − 2 ( f ( 2 ) f ( 1 ) )
设 A=(a1b0)n−2 A = ( a b 1 0 ) n − 2
那么 ans=(A[0][0]+A[0][1])%7 a n s = ( A [ 0 ] [ 0 ] + A [ 0 ] [ 1 ] ) % 7但是我做这题的时候就掉进了一个坑,我的结构体写了构造函数,导致快速幂每次调用乘法的时候都会调用构造函数,导致超时。。。这回真的是记住了
code:
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct Matrix{ int m[2][2]; }; Matrix mul(Matrix a,Matrix b){ Matrix ans; for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ ans.m[i][j] = 0; for(int k = 0; k < 2; k++){ ans.m[i][j] = (ans.m[i][j] + a.m[i][k] * b.m[k][j] % 7) % 7; } } } return ans; } int main(){ int A,B,n; while(scanf("%d%d%d",&A,&B,&n) != EOF){ if(A == 0 && B == 0 && n == 0) break; if(n <= 2){ printf("1\n"); continue; } Matrix a,ans; a.m[0][0] = A; a.m[0][1] = B; a.m[1][0] = 1; a.m[1][1] = 0; ans.m[0][0] = ans.m[1][1] = 1; ans.m[0][1] = ans.m[1][0] = 0; n -= 2; while(n){ if(n & 1) ans = mul(ans,a); n >>= 1; a = mul(a,a); } printf("%d\n",(ans.m[0][0] + ans.m[0][1]) % 7); } return 0; }
-
HDU-1005-Number Sequence
2016-10-11 03:43:31ACM模版描述题解一看公式就知道这道题在51Nod上做过一次,于是按照老思路准备水过,可是却意外发现了自己曾经的写法实在是想当然了,如果不是这道题51Nod数据比较水,我一定过不去~~~以前在做这道题时,感觉循环一定...描述
题解
一看公式就知道这道题在51Nod上做过一次,于是按照老思路准备水过,可是却意外发现了自己曾经的写法实在是想当然了,如果不是这道题51Nod数据比较水,我一定过不去~~~
以前在做这道题时,感觉循环一定是从第一项开始的,也就是循环节的前两项一定是1、1,然而事实并非如此,当我依然用这种思路写时,HDU给我的结果是
WA
,于是我改掉了这个可能存在的问题,结果顺利AC了……看来有一句话说的真不假,你样例过了,并不能代表你对了,甚至你AC了,同样不能确保你是对的!!!
代码
#include <iostream> using namespace std; const int MAXN = 300; const int MOD = 7; int f[MAXN] = {1, 1, 1}; int main(int argc, const char * argv[]) { int A, B, n; while (cin >> A >> B >> n) { if (A == 0 && B == 0 && n == 0) { break; } int beg = 1; int end = 1; int cycle = 1; bool flag = false; for (int i = 3; i <= n && !flag; i++) { f[i] = (A * f[i - 1] + B * f[i - 2]) % MOD; for (int j = 2; j < i; j++) { if (f[i] == f[j] && f[i - 1] == f[j - 1]) { cycle = i - j; beg = j - 1; end = i - 1; flag = true; break; } } } if (flag) { cout << f[beg + (n - end) % cycle] << '\n'; } else { cout << f[n] << '\n'; } } return 0; }
参考
-
杭电ACM OJ 1005 Number Sequence
2015-08-25 13:51:04A 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). 超过49个数之后一定会出现和之前的... -
Number Sequence (排列组合)
2016-09-29 21:49:02大致题意: 有一串数字串,其规律为 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112······k ...输入位置n,计算这一串数字第n位是什么数字,注意是... -
杭电ACM刷题(2):1005,Number Sequence
2017-05-11 22:43:47Problem 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 T -
Number Sequence(存在循环规律)
2016-07-22 19:47:39Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1803 Accepted Submission(s): 688 Problem Description A -
NumberSequence
2008-04-02 17:40:32how to use ax Number Sequence -
HDUOJ 1005 Number Sequence(DP求公式)
2016-04-13 20:19:49Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 146218 Accepted Submission(s): 35530 Problem Description A number -
HDU - 5014 Number Sequence(贪心+位运算)
2014-09-18 21:19:27Number Sequence Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u [Submit] [Go Back] [Status] Description There is a special number ... -
poj1019——Number Sequence(数学)
2017-02-10 23:20:16DescriptionA single positive integer i is ... 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 i -
uva10689 Yet another Number Sequence
2015-04-08 16:12:50Yet another Number Sequence Input: standard input Output: standard output Time Limit: 3 seconds Let's define another number sequence, given by the following function: f(0) = a -
[ACMcoder] Number Sequence
2015-05-26 18:58:25Problem 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 i -
HDU 5014 Number Sequence(贪心)
2014-09-16 01:34:43当时想到了贪心,但是不知为何举出了反列。。。。...才发现我是逗比。...There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● -
HDU-1005,Number Sequence(有规律的数学题)
2020-02-01 11:41:36Problem 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: ... -
递归类别hduoj1005(Number Sequence)
2015-03-06 16:57:43Number SequenceTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 116663 Accepted Submission(s): 28381 Problem DescriptionA number sequence i -
uva 10706 Number Sequence(找规律)
2015-02-11 21:45:52uva 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 ... -
Number Sequence【规律】
2016-11-08 17:41:55Number Sequence f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B*f(n-2)) % 7 -
hdoj1005 Number Sequence 代码
2011-09-12 14:00:46hdoj1005 Number Sequence, 杭州电子科技大学oj题目代码 -
HDU1711-Number Sequence
2014-08-11 19:04:22Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11038 Accepted Submission(s): 5038 Problem Description Given two sequ