UVa 568: Just the facts
来源:互联网 发布:程序员都有什么app 编辑:程序博客网 时间:2024/05/17 21:57
这题要求N!的末尾非零位,考虑用模除法保留阶乘计算中每次乘法后所得结果的最后几位数。
当遇到乘数为5的倍数时,从保留的最后几位数中分出2与乘数因子中的5配对。
阶乘做完后模10得到结果。
另:
- 这种方法只适合N较小的情况。
- 保留每次乘积的至少末5位,因为计算3125!的对饮结果时如果保留的位数过少,将没有足够的2与3125=5^5中的5匹配消去,会得到错误结果
我的解题代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;int main(){int N;int digit;//用于保留乘积的末5位int k;while(cin >> N){digit = 1;for(int i=1; i<=N; i++){k=i;while(k%5==0){//从digit中除去2,消去k中的5k = k/5;digit = digit/2;}digit = (digit*k)%100000;}digit %= 10;printf("%5d -> %d\n",N,digit);}return 0;}
- uva 568 - Just the Facts
- Uva 568 - Just the Facts
- uva 568 just the facts
- UVa 568 - Just the Facts
- uva 568 - Just the Facts
- uva-568 - Just the Facts
- Uva 568 - Just the Facts
- UVa 568 Just the Facts
- UVa 568: Just the facts
- UVA 568 Just the Facts
- uva 568 - Just the Facts
- uva 568 Just the Facts
- UVa 568 - Just the Facts
- uva 568 Just the Facts
- UVA 568 - Just the Facts
- uva 568 - Just the Facts
- uva 568 just the facts
- UVa 568 - Just the Facts
- 林徽因 - 《人间四月天》
- PHP中用SimpleXMLElement解析xml
- 自己动手写Django app,第二部分【完】
- hdu4618Palindrome Sub-Array(乱搞)
- 自己动手写Django app,第三部分【完】
- UVa 568: Just the facts
- UITableView 使用小结
- XFire实现WebService一:使用XfireConfigurableServlet
- 自己动手写Django app,第四部分【全剧终】
- 反向键索引研究
- 解决SurfaceView设置透明造成覆盖其他组件的替代方案
- 杭电多校联合第一场hdu4606 occupy cities
- hdu 1254 推箱子
- linux下如何生成任意大小的测试文件