Codeforces Round #231 (Div. 2) B Very Beautiful Number(數字遞推)

畫船聽雨發表於2014-02-21

一看到題目的時候就感覺很有思路,後寫了幾次試了一下,感覺對了。可惜程式碼能力不夠強,一直在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.1415926535898

using 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;
}


相關文章