Codeforces Round #231 (Div. 2) B Very Beautiful Number(數字遞推)
一看到題目的時候就感覺很有思路,後寫了幾次試了一下,感覺對了。可惜程式碼能力不夠強,一直在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組資料上了啊。
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!
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
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.
6 5
142857
1 2
Impossible
6 4
102564
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;
}
相關文章
- Codeforces Round #325 (Div. 2) B 遞推
- Codeforces Round #448 (Div. 2)B
- Codeforces Round #450 (Div. 2) B
- Codeforces Round #213 (Div. 2) A. Good NumberGo
- Codeforces Round #251 (Div. 2) A/B/D
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)B
- Codeforces Round #362 (Div. 2) B 模擬
- Codeforces Round #336 (Div. 2) B 暴力
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #245 (Div. 2) B - Balls GameGAM
- Codeforces Round #228 (Div. 2) A. Fox and Number GameGAM
- Codeforces Round 977 (Div. 2)(B-C2)
- Codeforces Round #491 (Div. 2) B. Getting an A
- Codeforces Round #361 (Div. 2) B BFS最短路
- Codeforces Round #323 (Div. 2) B 模擬
- Codeforces Round #321 (Div. 2) B 二分
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #242 (Div. 2) B. Megacity
- Codeforces Round #249 (Div. 2) B. Pasha Maximizes
- Codeforces Round #247 (Div. 2) B - Shower Line
- Codeforces Round #253 (Div. 2) B - Kolya and Tandem Repeat
- Codeforces Round #252 (Div. 2) B. Valera and FruitsUI
- Codeforces Round #246 (Div. 2) B. Football Kit
- Codeforces Round #244 (Div. 2) B. Prison Transfer
- Codeforces Round #280 (Div. 2 A,B,C,D,E)
- Codeforces Round #228 (Div. 2) B. Fox and CrossROS
- Codeforces Round #215 (Div. 2) B. Sereja and Suffixes
- Codeforces Round #243 (Div. 2) B. Sereja and Mirroring
- Codeforces Round #196 (Div. 2) B. Routine Problem
- Codeforces Round #251 (Div. 2) B. Devu, the Dumb Guydev
- Codeforces Round #209 (Div. 2) B. Permutation
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)