您的位置:首页 > 其它

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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: