Codeforces Round #231 (Div. 2) B. Very Beautiful Number
2014-02-21 12:13
453 查看
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 exactly p 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".
大数乘法 - - 注意1是特例。。。。
个位数枚举1到9,然后往大位数推,一直推到第p位,观察如果
①第p位等于个位数,
②第p位后没有进位,
③p-1位不是0 ,
那么就成立。。。
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 exactly p 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".
大数乘法 - - 注意1是特例。。。。
个位数枚举1到9,然后往大位数推,一直推到第p位,观察如果
①第p位等于个位数,
②第p位后没有进位,
③p-1位不是0 ,
那么就成立。。。
#include <cstdio> #include <iostream> #include <string.h> #include <stdlib.h> #include <fstream> #include <math.h> #include <queue> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; char s[1000005]; int main() { //freopen ("input.txt","r",stdin); int p,x; while(~scanf("%d%d",&p,&x)) { if(p==1) //排除特例 1 { if(x==1) cout <<1<<endl; else cout<<"Impossible\n"; continue; } int ok =0; for(int b=1;b<=9;b++) { int temp1=b; //第i位 int temp2=0; //进位数 int t; if(ok) break; int l=p; s[l--]='\0'; for(int i=1;i<=p;i++) { s[l--]='0'+temp1; t=(x*temp1)%10+temp2; temp2=x*temp1/10; temp1=t; while(temp1>=10) temp1-=10,temp2++; } if(temp2==0&&temp1==b&&s[0]!='0') //如果成立1 2 3 { puts(s); ok=1; break; } } if(ok==0) cout<<"Impossible\n"; } 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
- Educational Codeforces Round 39 (Rated for Div. 2) E Largest Beautiful Number
- code forces 394B Very Beautiful Number
- CF---B. Very Beautiful Number
- Codeforces 394 B. Very Beautiful Number
- Codeforces_394B_Very Beautiful Number(数论)
- cf B Very Beautiful Number
- codeforces 394 B. Very Beautiful Number(思路较难,优化的地方多)好题。。。
- CF 231div2 B. Very Beautiful Number
- CF:394B - Very Beautiful Number(逆推)
- Codeforces Round #427 (Div. 2) A. Key races B. The number on the board C. Star sky
- Codeforces Round #427 (Div. 2)-B. The number on the board
- Codeforces Round #241 (Div. 2) AGuess a number!
- SRM 668 DIV 2 VerySecureEncryption 250point
- [DP] SRM 452 Div1 Hard IncreasingNumber
- Educational Codeforces Round 39-E-Largest Beautiful Number(暴力)
- Give a very good method to count the number of ones in a "n" (e.g. 32) bit number.