Codeforces Round #231 (Div. 2) B Very Beautiful Number(数字递推)
2014-02-21 10:00
274 查看
一看到题目的时候就感觉很有思路,后写了几次试了一下,感觉对了。可惜代码能力不够强,一直在dbug,最后才过了样例。但是感觉怪怪的,要过不了,最后真的卡在了24组数据上了啊。sad、、、又是细节没注意啊、、
递推的方法,枚举最后一位数字,因为这个数字里面的顺序是有关系的所以最后一位dp[q]*x一定等于dp[q-1]。
比如样例:6 5 中的142857 和 714285。
7*5 的个位数一定是7前面的5,也是下面的最后一位5.
(5*5+3)%10 = 8是142857中5前面的8,也是714285中5前面的8.
同理模拟过程就可以推出来,一个q位数。但是这样枚举的时候最后一个数推出来的时候一定要判断一下是否有进位,否则就会卡在24组数据上了啊。
B. Very Beautiful Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the
teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactlyp decimal digits,
and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very
beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!
Input
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
Output
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful
number" without leading zeroes.
Sample test(s)
input
output
input
output
input
output
Note
Sample 1: 142857·5 = 714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible".
递推的方法,枚举最后一位数字,因为这个数字里面的顺序是有关系的所以最后一位dp[q]*x一定等于dp[q-1]。
比如样例:6 5 中的142857 和 714285。
7*5 的个位数一定是7前面的5,也是下面的最后一位5.
(5*5+3)%10 = 8是142857中5前面的8,也是714285中5前面的8.
同理模拟过程就可以推出来,一个q位数。但是这样枚举的时候最后一个数推出来的时候一定要判断一下是否有进位,否则就会卡在24组数据上了啊。
B. Very Beautiful Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the
teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactlyp decimal digits,
and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very
beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!
Input
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
Output
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful
number" without leading zeroes.
Sample test(s)
input
6 5
output
142857
input
1 2
output
Impossible
input
6 4
output
102564
Note
Sample 1: 142857·5 = 714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible".
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <stack> #include <map> #include <set> #define eps 1e-7 #define M 10001000 //#define LL __int64 #define LL long long #define INF 0x3f3f3f3f #define PI 3.1415926535898 using namespace std; const int maxn = 2001000; int dp[maxn]; int s1[maxn], s2[maxn]; int main() { int p, x; while(cin >>p>>x) { int flat; int sss; for(int i = 1; i <= 9; i++) { dp[p] = i; int s = p; int t = p-1; int add = 0; flat = 0; sss = 0; while(s) { dp[t] = ((dp[s]*x)%10+add)%10; add = ((dp[s]*x)+add)/10; s1[sss] = dp[s]; s2[sss++] = dp[t]; t--; s--; } if(add > 0)//就是这里没有判啊。。。 continue; if(s1[0] == s2[p-1] && dp[1]) { flat = 1; break; } } if(!flat) cout<<"Impossible"<<endl; else { for(int i = 1; i <= p; i++) cout<<dp[i]; cout<<endl; } } return 0; }
相关文章推荐
- Codeforces Round #231 (Div. 2)B. Very Beautiful Number
- Codeforces Round #231 (Div. 2) B Very Beautiful Number
- Codeforces Round #231 (Div. 2) B. Very Beautiful Number
- CF 394B:Very Beautiful Number
- code forces 394B Very Beautiful Number
- CF:394B - Very Beautiful Number(逆推)
- cf B Very Beautiful Number
- CF 231div2 B. Very Beautiful Number
- Codeforces_394B_Very Beautiful Number(数论)
- Codeforces 394 B. Very Beautiful Number
- Educational Codeforces Round 39 (Rated for Div. 2) E Largest Beautiful Number
- CF---B. Very Beautiful Number
- codeforces 394 B. Very Beautiful Number(思路较难,优化的地方多)好题。。。
- Codeforces Round #350 (Div. 2) F. Restore a Number(有一位数为n的数,在后面添上n这个数,然后将所有的数字打乱输出,再给你一串原数字的子串,输出最小)
- Codeforces Round #411(Div. 2)D. Minimum number of steps【思维递推+快速幂】
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number【莫队算法】
- iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键
- Codeforces Round #394 (Div. 2) Dasha and Very Difficult Problem
- UIKeyboardTypeNumberPad 数字键盘添加完成按钮
- javascript 数字(Number类型)的范围,整数的范围是多少?