之江学院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
50
官方题解:
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; }
相关文章推荐
- 之江学院 K: qwb与小数(思维求A/B第n位小数)
- 之江学院2017ACM校赛 Problem B: qwb与矩阵(记忆化搜索)
- 之江学院 K: qwb与小数(思维求A/B第n位小数)
- 之江学院2017ACM 校赛Problem D: qwb与神奇的序列(找规律 快速幂)
- 之江学院2017ACM 校赛 Problem J: qwb又偷懒了(树状数组)
- 之江学院2017ACM 校赛Problem D: qwb与神奇的序列
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛 K: qwb与小数 [快速幂]【思维】
- 之江学院2017ACM 校赛 Problem M: qwb与二叉树(记忆化搜索)
- 之江学院2017ACM 校赛Problem D: qwb与神奇的序列
- 之江学院2017ACM 校赛Problem G: qwb去面试
- 之江学院第0届校赛决赛 K-qwb与小数
- ZJCOJ qwb与小数 思维+快速幂
- 2017浙工大之江学院校赛 C 组合数学+思维
- (思维分析,贪心)湘大校赛,D.最小的数
- qwb与整数对 (思维枚举难)
- qwb与小数
- 2012中南大学校赛F题 - 旋转卡壳的思维...
- GDUT2017校赛:Problem H: tmk买礼物(思维)
- 计算机程序的思维逻辑 - 小数计算为什么会出错?
- 之江学院第0届校赛决赛 F-qwb has a lot of Coins