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();
}
USACO 3.2 Factorials的更多相关文章
- 【USACO 3.2】Factorials(阶层非零尾数)
题意:输出n的阶层最后一个非0数. 题解:可以把5和2的个数算出来,每次把5和2都除掉,最后乘上比5多出来的2.我的解法是,每次把尾巴的0去掉,并且保留3位,算到最后取尾数就可以了.. /* TASK ...
- USACO Section 3.2: Factorials
这题注意要保存%10000的数. /* ID: yingzho1 LANG: C++ TASK: fact4 */ #include <iostream> #include <fst ...
- USACO . Your Ride Is Here
Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- 洛谷 P2726 阶乘 Factorials Label:Water
题目背景 N的阶乘写作N!,表示小于等于N的所有正整数的乘积. 题目描述 阶乘会变大得很快,如13!就必须用32位整数类型来存储,到了70!即使用浮点数也存不下了. 你的任务是找到阶乘最前面的非零位. ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
随机推荐
- 【译】Objectively Speaking 2: A Crash Course in Objective-C for iOS 6
In this Objective-C tutorial, you will create a simple movie quotes quiz app. Along the way, you’ll ...
- Moq & RhinoMocks
Moq & RhinoMocks 使用Mock对象进行测试一般都会有以下三个关键步骤: 使用接口来描述需要测试的对象 为实际的产品代码实现这个接口 以测试为目的,在Mock对象中实现这个接口 ...
- Spring3.2 + Hibernate4.2
Spring3.2 + Hibernate4.2 前三篇随笔中介绍了 用原生的JDBC访问数据库.一种高效的数据库连接池druid.用Spring的JDBC框架访问数据库. 本文继续介绍第三种数据库访 ...
- Jquery轻量级幻灯插件-OWL Carousel--支持触屏的移动浏览器
Jquery轻量级幻灯插件-OWL Carousel--支持触屏的移动浏览器 在项目中,需要做一个幻灯功能,领导说需要一个小清醒的啊,轻量级的.刚开始搜索到这个: CRAFTYSLIDE插件.但是用起 ...
- Android SDK文档如何查找
肯定很多人都会有疑问,怎样使用Android SDK 文档该如何使用呢?里面有那么多内容,又全是英文的,接下来告诉大家. 以下内容来自网络. ----------------------------- ...
- MySQL模式 : Strict Mode
I. Strict Mode阐述 根据 mysql5.0以上版本 strict mode (STRICT_TRANS_TABLES) 的限制: 1).不支持对not null字段插入null值 2). ...
- 为outlook增加“邮件召回”功能
outlook 2007开始软件自带邮件召回功能.2003版本没有,可惜此版本盗版最厉害,用户很广,这次项目中用户分布很广,其中outlook2003版本用户数甚多,达到397人. 不可能让他们新装2 ...
- Python学习入门基础教程(learning Python)--5.7 Python文件数据记录存储与处理
本节主要讨论Python下如何通过文件操作实现对数据记录集的存储与处理的操作方法. 在Python里和其他高级语言一样可以通过文件读写将一些记录集写入文件或者通过文件读操作从文件里读取一条或多条和数据 ...
- window.setTimeout()函数的使用
<script type="text/javascript"> //此程序主要完成页面定时关闭功能 function closeMyWindow() { window. ...
- [C++STDlib基础]关于C标准输入输出的操作——C++标准库头文件<cstdio>
网上实例 总结 /* _STD_BEGIN using _CSTD clearerr; using _CSTD fclose; using _CSTD feof; using _CSTD ferror ...