1、http://codeforces.com/problemset/problem/394/B
2、题目大意:
写出一个n位数,使得n*x的值正好是n的最后一位移到第一位的那个数,如果有输出来满足条件的最小的那个数,如果没有输出Impossible
思路:
因为n非常大,所以这道题目不可能一个数一个数的遍历,但是x是非常小的,而我们可以知道这个n位数的最后一位是>=x的,那么我们可以遍历最后一位,我们知道最后一位了,自然结果的第一位就知道了,知道了结果的第一位,原来的第一位就可以推出来,原来的第一位就是结果的第二位,
举个例子来说,n=6 x=5
我们用?代表该位未知
那么按照最后一位来假设这个六位数,每步的结果分别是
?????6 6 ????0, 5*6=30所以最后一位是0
1 ????6 6 ????0 前边数的第一位是这么计算的6/5=1...1
1 ? ? ? ? 6 6 1 ? ? ? 0 后边的1是来自前边数的第一位,因为他们相差一位
1 2 ?? ? 6 6 1 ? ? ? 0 6/5余1,对于6后边的1来说就相当于是10,11/5=2....1
1 2 ?? ? 6 6 1 2 ? ? 0 后边的步骤都同前三步了,不再解释
1 2 2 ? ? 6 6 1 ? ? ? 0
1 2 2 ? ? 6 6 1 2 ? ? 0
1 2 2 4 ? 6 6 1 2 ? ? 0
1 2 2 4 ? 6 6 1 2 4 ? 0
1 2 2 4 8 6 6 1 2 2 4 0 最后一位应是8,不是0,所以不对
那么当n=7,就是正确的,不再证明
3、题目:
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 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".
4、AC代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 1000005 int a[N]; int b[N]; int main() { int p,x,ans,flag=0; scanf("%d%d",&p,&x); for(int i=x;i<=9;i++) { a[p]=i; b[1]=i; ans=0; for(int j=1;j<=p;j++) { a[j]=(ans*10+b[j])/x; ans=(ans*10+b[j])%x; b[j+1]=a[j]; } if(b[p]==(i*x)%10) { for(int k=1;k<=p;k++) printf("%d",a[k]); printf("\n"); flag=1; break; } } if(flag==0) printf("Impossible\n"); return 0; }