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".
看这个题中的样例。
142857 * 5 = 714285
通过乘法运算,这个数字每位向右移动了一位。这是一个关系,而且是一个递推的关系,这样就好像找到了一条锁链,现在是要找这条锁链的头在哪里。
如果我们知道了最后一位,那么,通过乘法运算,我们可以得到后面的数字的这个位置上的数,并且这个位置上的数正好是这个数的上一位。
以此类推。最后判断一下第一位能否递推出来最后一位就是。
当题目中给的信息足够多的时候,我们完全这个这样利用递推关系来构造答案。
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string.h>
#include <stack>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 1111111
#define P pair<int,int>
#define fst first
#define sec second
#define ll long long
#define MS(x) memset(x,0,sizeof(x))
int t[MAX];
int p,x;
int main()
{
scanf("%d%d",&p,&x);
memset(s,0,sizeof(s)),memset(t,0,sizeof(t));
if(p==1)
{
if(x!=1)
{
cout<<"Impossible"<<endl;
return 0;
}
cout<<1<<endl;
return 0;
}
for(int i=1;i<=9;i++)
{
t[0]=i;
int add=0;
for(int j=1;j<p;j++)
{
int tt=t[j-1]*x+add;
t[j]=tt%10;
add=tt/10;
}
if(t[p-1]!=0&&(t[p-1]*x+add)==i)
{
for(int k=p-1;k>=0;k--)
cout<<t[k];
cout<<endl;
return 0;
}
}
cout<<"Impossible"<<endl;
return 0;
}