码迷,mamicode.com
首页 > 其他好文 > 详细

之江学院程序设计竞赛

时间:2017-06-06 22:22:35      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:long   tput   学院   之间   ret   ace   lld   output   space   

Description

qwb遇到了一个问题:将分数a/b化为小数后,小数点后第n位的数字是多少?

做了那么多题,我已经不指望你能够帮上他了。。。

Input

多组测试数据,处理到文件结束。(测试数据<=100000组)

每组测试例包含三个整数a,b,n,相邻两个数之间用单个空格隔开,其中0 <= a <1e9,0 < b < 1e9,1 <= n < 1e9。

Output

对于每组数据,输出a/b的第n位数,占一行。
 
 

Sample Input

1 2 1
1 2 2

Sample Output

5

0

题解:设a=0.1*x1*b+0.01*x2*b+0.001*x3*b+0.0001*x4*b+.......+(0.1)^n*xn*b
//xn={0,1,2...9}

则10^(n-1)*a=10^(n-2)*b*x1+10^(n-3)*b*x2+10^(n-4)*b*x3+.......+0.1*xn*b=(10^(n-1)*a)/b*b+0.1*xn*b

则10^(n)*a=10*(10^(n-1)*a/b*b)+0.1*xn*b

则xn=(10^(n)*a-10*(10^(n-1)*a%b))/(b)=(10*(10^(n-1)*a-(10^(n-1)*a/b*b)))/(b)=(10*(10^(n-1)%b))/b

#include"stdio.h"
#include"cstdio"
#include"algorithm"
#include"string.h"
using namespace std;
typedef long long ll;
ll b;
ll small(ll x,ll n)
{
    ll c=1;
    while(n>0)
    {
        if(n&1) c=c*x%b;
        x=x*x%b;
        n>>=1;
    }
    return c;
}
int main()
{
    ll n,a;
    while(scanf("%lld%lld%lld",&a,&b,&n)!=EOF)
    {
        ll ret;
        ret=small(10,n-1);
        printf("%lld\n",10*(a*ret%b)/b);    
    }    
} 

 

之江学院程序设计竞赛

标签:long   tput   学院   之间   ret   ace   lld   output   space   

原文地址:http://www.cnblogs.com/lch316/p/6953592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!