Codeforces_394B_Very Beautiful Number(数论)

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 xtimes. 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 px (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和倍数x,构造出最小的Very Beautiful Number。

如果一个数是Very Beautiful Number,那么如果将该数的个位移至首位之前后变成的数是原数的x倍。

如果不存在则输出“Impossible”。


分析:

最容易想到的是当x=1的时候,答案就是p个1;

当p=1且x!=1的时候,不存在答案。

但是接下来怎么做呢?

可以采用DP思想,首先确定下个位数,从1到9都试一遍,然后不断的退出高一位,到p位时构成答案。

例如p=6,x=4:

当个位尝试到4的时候,

算百位时,4*4=16,则百位为16%10;

算千位时,16/10+16%10*4=25,则千位为25%10;

算万位时,25/10+25%10*4=22,则万位为22%10;

以此类推......

当算到p位时,直接取算出的答案的个位,进位直接扔掉。

然后在模拟高精度,检验构造出来的答案是否满足条件,注意如果算到最高位时大于10,那么说明构造出的数乘上x后就超过p位了,不满足条件。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int p,x;
int a[1234567],b[1234567],tmp[1234567];
int main() {
    while(~scanf("%d%d",&p,&x)) {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(tmp,0,sizeof(tmp));
        if(x==1) {
            for(int i=0; i<p; i++) {
                printf("1");
            }
            printf("\n");
            continue;
        }
        if(p==1&&x!=1) {
            printf("Impossible\n");
            continue;
        }

        bool flag = false;
        for(int i=1; i<10; i++) {
            a[0]=i;
            for(int j=1; j<p; j++) {
                a[j]=a[j-1]/10+a[j-1]%10*x;
                a[j-1]%=10;
            }
            a[p-1]%=10;//最高位的进位扔掉,只取个位

            //如果是前导零,则不符合条件
            if(a[p-1]==0) {
                continue;
            }

            //高精度检验
            for(int j=1; j<p; j++) {
                b[j-1]=a[j];
            }
            b[p-1]=a[0];
            int carry=0;
            for(int j=0; j<p; j++) {
                tmp[j]=a[j]*x+carry;
                carry=tmp[j]/10;
                if(j!=0) {
                    tmp[j-1]%=10;
                }
            }
            bool TS=true;
            for(int j=0; j<p; j++) {
                if(tmp[j]!=b[j]) {
                    TS=false;
                    break;
                }
            }

            if(TS) {
                for(int j=p-1; j>=0; j--) printf("%d",a[j]);
                printf("\n");
                flag=true;
                break;
            }

        }
        if(!flag) printf("Impossible\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值