您的位置:首页 > Web前端

B. Very Beautiful Number codeference 非常考验数学思维的一个问题

2014-02-21 20:58 405 查看
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
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
p, x (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位的n,n*x=一个数,用b【】来存,然后是这个数尾到前面,其余顺眼,那么这个数的最后一位只有可能大于等于x,为啥呢,举例说;

a=142857的7就是b=714285的最前面,最前面那个数是怎么形成的是一个数1*5加上某位得到的,所以可以只必然在x到9之间,而得出了最后一位7的话,那么7*5得到的余数是不是就是我们b的最后一位么!!!!那么a的最前面1是怎么得到的,是1*5+2=7的,而

2是前面给的,1确实本身,只要用7/5就可以得到1了,那么倒数第二位4怎么得到呢???顺次去求即可得到

唉,实在不给力啊!!!!!!!!!!!!!!!

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000005
int a
;
int b
;
int main()
{
int i,j,k;
int p,x,flag=0;
scanf("%d%d",&p,&x);
int c=0;//jiwei
for(i=x;i<=9;i++)
{

a[p]=i;
b[1]=i;
c=0;
for(j=1;j<=p;j++)
{
a[j]=(c*10+b[j])/x;
c=(c*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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iostream 数学 namespace
相关文章推荐