您的位置:首页 > 其它

之江学院2017ACM 校赛 Problem K: qwb与小数(思维 快速幂)

2017-06-03 16:19 183 查看


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/b的第n位可以由第n-1位求出,第n-1位除的余数乘10再除以b就是第n位的数,答案就是a*pow_mod(10,n-1.b)%b*10/b或者
a*pow_mod(10,n,10*b)/b%10。快速幂的时候模取b即可。 

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;

ll qmod(ll x, ll p, ll mod)
{
ll ans = 1;
while(p)
{
if(p%2) ans = ans*x%mod;
x = x*x%mod;
p /= 2;
}
return ans;
}

int main(void)
{
ll a, b, n;
while(~scanf("%lld%lld%lld", &a, &b, &n))
printf("%lld\n", a*qmod((ll)10, n-1, b)%b*10/b);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: