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
Output
Input
Output
Input
Output
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;
}
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;
}
相关文章推荐
- python socket编程---从使用Python开发一个Socket示例说到开发者的思维和习惯问题
- 尽管是一个CS专业的学生,小B的数学基础很好并对数值计算有着特别的兴趣,喜欢用计算机程序来解决数学问题。现在,她正在玩一个数值变换的游戏。她发现计算机中经常用不同的进制表示同一个数,如十进制数123表达为16进制时只包含两位数7、11(B),用八进制表示时为三位数1、7、3。按不同进制表达时,各个位数的和也不同,如上述例子中十六进制和八进制中各位数的和分别是18和11。
- HTC ONE里面一个非常奇怪的问题。。。调用kSOAP出错
- 一个完全超出数学一切能力的问题
- 一个数学期望问题
- 今天遇到一个数学问题
- 庞涓VS孙膑——一个很难的数学问题
- 1078:一个数学问题
- 一个售前思维问题
- 【SHELL】一个非常有趣的问题
- 话题:jQuery 关于文件上传表单处理的一个非常怪异的问题
- python socket编程---从使用Python开发一个Socket示例说到开发者的思维和习惯问题
- 在数学中一个非凸的最优化问题
- IOS 7中一个非常重要的是问题!
- OpenGL里用gluTessBeginPolygon画带孔和凹多边形需要非常注意的一个问题!
- JS一个非常经典的问题:在遍历数组时对DOM监听事件,索引值将始终等于遍历结束后的值
- 对一个简单数学问题的浮想
- 【转】一个非常常见但容易被忽略的c++问题——用IPML模式可以解决
- OpenGL里用gluTessBeginPolygon画带孔和凹多边形需要非常注意的一个问题!
- 平面中能否写下不可数个8?——一个有趣的数学问题