USACO 3.2 Factorials

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就可以了
纪念我的智障……
 /*
ID: ivorysi
PROG: fact4
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define MAXN 400005
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
//#define pis pair<int,string>
using namespace std;
typedef long long ll;
int two,five;
int n;
int ans=;
void divide(int &u) {
while(u%==) {++five;u/=;}
while(u%==) {++two;u/=;}
}
void solve() {
scanf("%d",&n);
siji(i,,n) {
int tmp=i;
divide(tmp);
ans=(ans*tmp+)%;
}
two-=five;
siji(i,,two) ans=ans*%;
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("fact4.in","r",stdin);
freopen("fact4.out","w",stdout);
#else
//freopen("f1.in","r",stdin);
#endif
solve();
}
 
上一篇:.pages怎么在windows上打开?Windows下打开在Mac中编辑的.pages文件方法


下一篇:Android中那些有你不知道的事