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!
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是特例。。。。
个位数枚举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;
}