Codeforces Round #231 (Div. 2) B Very Beautiful Number(数字递推)

来源:互联网 发布:网络解锁 编辑:程序博客网 时间:2024/05/17 07:14

一看到题目的时候就感觉很有思路,后写了几次试了一下,感觉对了。可惜代码能力不够强,一直在dbug,最后才过了样例。但是感觉怪怪的,要过不了,最后真的卡在了24组数据上了啊。sad、、、又是细节没注意啊、、

递推的方法,枚举最后一位数字,因为这个数字里面的顺序是有关系的所以最后一位dp[q]*x一定等于dp[q-1]。

比如样例:6 5 中的142857 和 714285。

7*5 的个位数一定是7前面的5,也是下面的最后一位5.

(5*5+3)%10 = 8是142857中5前面的8,也是714285中5前面的8.

同理模拟过程就可以推出来,一个q位数。但是这样枚举的时候最后一个数推出来的时候一定要判断一下是否有进位,否则就会卡在24组数据上了啊。

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 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!

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

#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7#define M 10001000//#define LL __int64#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898using namespace std;const int maxn = 2001000;int dp[maxn];int s1[maxn], s2[maxn];int main(){    int p, x;    while(cin >>p>>x)    {        int flat;        int sss;       for(int i = 1; i <= 9; i++)        {            dp[p] = i;            int s = p;            int t = p-1;            int add = 0;            flat = 0;            sss = 0;            while(s)            {                dp[t] = ((dp[s]*x)%10+add)%10;                add = ((dp[s]*x)+add)/10;                s1[sss] = dp[s];                s2[sss++] = dp[t];                t--;                s--;            }            if(add > 0)//就是这里没有判啊。。。                continue;            if(s1[0] == s2[p-1] && dp[1])            {                flat = 1;                break;            }        }        if(!flat)            cout<<"Impossible"<<endl;        else        {            for(int i = 1; i <= p; i++)                cout<<dp[i];            cout<<endl;        }    }    return 0;}


0 0