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!
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
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.
6 5
142857
1 2
Impossible
6 4
102564
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~9),然后得到相应的结果,而前一位的数是结果的上一次乘法得到的结果,这样就可以把满足这样的数推出来了。代码如下:
#include <iostream>
#include <cstdio>
using namespace std;
int a[1000010],b[1000010];
int main(){
int m,n,tmp,flag;
tmp=flag=0;
scanf("%d%d",&m,&n);
if(n==1&&m==1){
printf("1\n");
return 0;
}
for(int i=1;i<=9;i++){
tmp=0;
for(int j=1;j<=m;j++){
if(j==1){
a[j]=i;
b[j]=(a[j]*n+tmp)%10;
tmp=(i*n)/10;
}
else if(j!=1&&j!=m){
a[j]=b[j-1];
b[j]=(a[j]*n+tmp)%10;
tmp=(a[j]*n+tmp)/10;
}
else if(j==m){
a[j]=b[j-1];
b[j]=(a[j]*n+tmp)%10;
tmp=(a[j]*n+tmp)/10;
if(tmp==0&&b[j]==a[1]&&a[j]>=1)
flag=1;
}
}
if(flag)
break;
}
if(flag){
for(int i=m;i>=1;i--)
printf("%d",a[i]);
printf("\n");
}
else
printf("Impossible\n");
return 0;
}