问题及代码:
运行结果:
则有1/m=0.xxxx...xxxaxxx
其中a为小数点后第n位即所求的答案
接下来a=(10^n/m)%10;
存在b,c使得10^n=m*b*10+c (其中c=(10^n)%(10*m)=10*(10^(n-1))%m即c<10*m);
所以a=((m*b*10+c)/m)%10=(b*10+c/m)%10=(c/m)%10=c/m;
这个过程中只要求c就能求出a=c/m;
而c=10*(10^(n-1))%m这个可用二分递归求解如程序中的10*solve(m,n-1))
所以a=(10*solve(m,n-1))/m;保险起见加了个"%10";
Just a Numble
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Now give you two integers n m, you just tell me the m-th number after radix point in 1/n,for example n=4,the first numble after point is 2,the second is 5,and all 0 followed
Input
Each line of input will contain a pair of integers for n and m(1<=n<=10^7,1<=m<=10^5)
Output
For each line of input, your program should print a numble on a line,according to the above rules
Sample Input
4 2 5 7 123 123
Sample Output
5 0 8
Author
Source
HDU 2007-10 Programming Contest_WarmUp
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:HDU.cpp
*作 者:单昕昕
*完成日期:2015年2月9日
*版 本 号:v1.0
*/<span style="font-family: monospace; font-size: 14px; line-height: 19.59375px; white-space: pre; background-color: rgb(240, 240, 240);"> //AC代码1</span>
#include<stdio.h>
long long solve(int m,int n)
{
long long t;
if(n==0)
return 1;
if(n==1)
return 10%m;
t=solve(m,n/2);
t*=t;
if(n%2==1)
t*=10%m;
return t%m;
}
int main()
{
int n,m;
while(scanf("%d%d",&m,&n)!=EOF)
printf("%I64d\n",((10*solve(m,n-1))/m)%10);
return 0;
}<p> //AC代码2</p><p>#include <iostream></p><p>using namespace std;</p><p>int main()</p><p>{</p><p> int n,m,i,s,t;</p><p> while(cin>>n>>m)</p><p> {</p><p> if(n==1) </p><p> cout<<0<<endl; //如果n为1,则输出0;</p><p> else</p><p> {</p><p> s=10;</p><p> for(i=1; i<=m; i++) //计算小数点后面m位上的数字</p><p> {</p><p> </p><p> t=s/n; </p><p> s=s%n*10; //如n=4,m=1,则有10/4=2;</p><p> } //如n=4,m=2,则有s=10%4/*10=20,20/4=5;</p><p> cout<<t<<endl; </p><p> }</p><p> }</p><p> return 0;</p><p>}</p>
运行结果:
知识点总结:
则有1/m=0.xxxx...xxxaxxx
其中a为小数点后第n位即所求的答案
接下来a=(10^n/m)%10;
存在b,c使得10^n=m*b*10+c (其中c=(10^n)%(10*m)=10*(10^(n-1))%m即c<10*m);
所以a=((m*b*10+c)/m)%10=(b*10+c/m)%10=(c/m)%10=c/m;
这个过程中只要求c就能求出a=c/m;
而c=10*(10^(n-1))%m这个可用二分递归求解如程序中的10*solve(m,n-1))
所以a=(10*solve(m,n-1))/m;保险起见加了个"%10";
学习心得:
度娘出品,必属优品。