您的位置:首页 > 其它

Codeforces Round #231 (Div. 2) B. Very Beautiful Number

2014-02-21 12:13 453 查看
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 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!

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".

大数乘法 - - 注意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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces acm