CF 394B Very Beautiful Number(逆推)

Description

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 exactlyx 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 Input

Input

6 5

Output

142857

Input

1 2

Output

Impossible

Input

6 4

Output

102564

Sample Output

Hint

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

思路:枚举个位数,从x(因为个位数是由第一位数乘x+进位得到的,并且第一位数乘x+进位不会产生进位,所以个位数一定大于等于x)到9,推出前面的n-1位,如果得到的数字符合条件,输出结果退出。特判一位数(n=1),x必须是1,否则不可能。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,x;
int b[1000010],flag=1;
int main()
{
    scanf("%d%d",&n,&x);
    if(n==1&&x!=1)
    {
        puts("Impossible");
        return 0;
    }
    for(int i=x;i<10;i++)
    {
        memset(b,0,sizeof b);
        int t=0;
        b[n-1]=i;
        for(int j=n-2;j>=0;j--)
        {
            b[j]=(b[j+1]*x+t)%10;
            t=(b[j+1]*x+t)/10;
        }
        if(b[0]&&((b[0]*x+t)==b[n-1])&&(b[0]*x+t)/10==0)//不能有前导0,不能有进位,第一位乘x加进位与个位相等
            break;
        if(i==9)
            flag=0;
    }
    if(flag)
        for(int i=0;i<n;i++)
            printf("%d",b[i]);
    else
        printf("Impossible");
    puts("");
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值