CF---B. Very Beautiful Number
2015-01-04 14:31
549 查看
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".
//这个题p最大为1000000, 暴力枚举字符串肯定不行, 采用的是从个位数字开始,依次向高位枚举的方式。
Code:
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".
//这个题p最大为1000000, 暴力枚举字符串肯定不行, 采用的是从个位数字开始,依次向高位枚举的方式。
Code:
#include <stdio.h> #include <string.h> #include <math.h> #define N 1000100 char f ; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); // freopen("textout.txt", "w", stdout); #endif int i,j,k,t;int p,x; while(scanf("%d %d",&p,&x)!=EOF){ int flag=0; for(i=1;i<=9;i++){ //先初始化; memset(f,0,sizeof(f)); f[0]=f[p]=i+'0';int yu=0; for(j=p;j>0;j--){ t=(f[j]-'0')*x; f[j-1]=(t+yu)%10+'0'; yu=(t+yu)/10; } if(yu==0 && f[0]==f[p] && f[1]!='0'){ flag=1;break; } } if(flag){ for(i=1;i<=p;i++) printf("%c",f[i]); printf("\n"); } else printf("Impossible\n"); } return 0; }
相关文章推荐
- CF:394B - Very Beautiful Number(逆推)
- cf B Very Beautiful Number
- CF 394B:Very Beautiful Number
- CF 231div2 B. Very Beautiful Number
- Codeforces Round #231 (Div. 2) B. Very Beautiful Number
- code forces 394B Very Beautiful Number
- Codeforces Round #231 (Div. 2) B Very Beautiful Number
- codeforces 394 B. Very Beautiful Number(思路较难,优化的地方多)好题。。。
- Codeforces_394B_Very Beautiful Number(数论)
- Codeforces Round #231 (Div. 2)B. Very Beautiful Number
- Codeforces 394 B. Very Beautiful Number
- Codeforces Round #231 (Div. 2) B Very Beautiful Number(数字递推)
- Beautiful Palindrome Number(回文数)
- 【CF 617E】 XOR and Favorite Number (Mo's algorithm)
- hdu 5062 Beautiful Palindrome Number( dp )
- collect some favorite web site design work link, those are very beautiful
- Careercup Facebook Keep the number beautiful...
- cf C. Prime Number
- Beautiful Palindrome Number 美丽的回文数 HD 5062
- CF-B.Beautiful Divisors