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;
}