博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
USACO 3.2 Factorials
阅读量:4551 次
发布时间:2019-06-08

本文共 2117 字,大约阅读时间需要 7 分钟。

Factorials

The factorial of an integer N, written N!, is the product of all the integers from 1 through N inclusive. The factorial quickly becomes very large: 13! is too large to store in a 32-bit integer on most computers, and 70! is too large for most floating-point variables. Your task is to find the rightmost non-zero digit of n!. For example, 5! = 1 * 2 * 3 * 4 * 5 = 120, so the rightmost non-zero digit of 5! is 2. Likewise, 7! = 1 * 2 * 3 * 4 * 5 * 6 * 7 = 5040, so the rightmost non-zero digit of 7! is 4.

PROGRAM NAME: fact4

INPUT FORMAT

A single positive integer N no larger than 4,220.

SAMPLE INPUT (file fact4.in)

7

OUTPUT FORMAT

A single line containing but a single digit: the right most non-zero digit of N! .

SAMPLE OUTPUT (file fact4.out)

4 ———————————————————————————— 以为一个个把个位乘起来%10就好了,然并不,有些时候 例如75*4和74*14的最右非零位是不一样的,其实我们只需要手动去除2和5这两个质因子剩下的乘起来%10就可以了 纪念我的智障……
1 /* 2 ID: ivorysi 3 PROG: fact4 4 LANG: C++ 5 */ 6 #include 
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define siji(i,x,y) for(int i=(x);i<=(y);++i)14 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)15 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)16 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)17 #define inf 0x7fffffff18 #define MAXN 40000519 #define ivorysi20 #define mo 9779797721 #define ha 97471122 #define ba 4723 #define fi first24 #define se second25 //#define pis pair
26 using namespace std;27 typedef long long ll;28 int two,five;29 int n;30 int ans=1;31 void divide(int &u) {32 while(u%5==0) {++five;u/=5;}33 while(u%2==0) {++two;u/=2;}34 }35 void solve() {36 scanf("%d",&n);37 siji(i,1,n) {38 int tmp=i;39 divide(tmp);40 ans=(ans*tmp+10)%10;41 }42 two-=five;43 siji(i,1,two) ans=ans*2%10;44 printf("%d\n",ans);45 }46 int main(int argc, char const *argv[])47 {48 #ifdef ivorysi49 freopen("fact4.in","r",stdin);50 freopen("fact4.out","w",stdout);51 #else52 //freopen("f1.in","r",stdin);53 #endif54 solve();55 }

 

 

转载于:https://www.cnblogs.com/ivorysi/p/6131568.html

你可能感兴趣的文章
微信小程序 使用mpvue
查看>>
常用模块-02
查看>>
接口测试总结
查看>>
测试的基本概念
查看>>
【ZOJ】3209 Treasure Map
查看>>
ActiveMQ 消息队列服务
查看>>
《程序是给自己看的还是给别人看的》
查看>>
(12) PHP 随笔---Smarty模板引擎 单模板多缓存、局部不缓存 20--21
查看>>
【转】Math.Atan2 方法
查看>>
C++设计模式之工厂方法模式
查看>>
poj3984_bfs+回溯路径
查看>>
MyEclipse使用技巧
查看>>
[译]径向镜片反畸变滤波
查看>>
畅通工程-最小生成树+并查集
查看>>
top命令输出解释以及load average 详解及排查思路
查看>>
Ajax的封装
查看>>
Java传入参数个数不确定可用(Type ... values)
查看>>
POJ 2081
查看>>
记录下zend studio 的xdebug 在调试安装
查看>>
ES6阅读笔记
查看>>