数论:HDU1066-Last non-zero Digit in N!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7929 Accepted Submission(s): 2037
Problem Description
The expression N!, read as "N factorial," denotes the product of the first N positive integers, where N is nonnegative. So, for example,
N N!
0 1
1 1
2 2
3 6
4 24
5 120
10 3628800
For this problem, you are to write a program that can compute the last non-zero digit of the factorial for N. For example, if your program is asked to compute the last nonzero digit of 5!, your program should produce "2" because 5! = 120, and 2 is the last
nonzero digit of 120.
Input
Input to the program is a series of nonnegative integers, each on its own line with no other letters, digits or spaces. For each integer N, you should read the value and compute the last nonzero digit of N!.
Output
For each integer input, the program should print exactly one line of output containing the single last non-zero digit of N!.
Sample Input
1
2
26
125
3125
9999
Sample Output
1
2
4
8
2
8
开始没有看清楚题目,不知道N有多大,WA了N道,后来才知道N可以非常大,不是我能解决的范围了,然后再一看吉大ACM模板,有这个模板,我就照着输了。先把最后所有的0去掉,方法就是在乘的时候统计因子2的个数,然后每遇到一个5,就去掉一个,那么乘出来就没有最后的0了~然后在乘的时候只保留最后一位就可以了,最后把统计了的2的个数乘回去(在统计的时候把2给提出来,这样就可以避免模的除法了!)
#include<stdio.h>
#include<string.h> #define maxn 10001 int lastdigit(char buf[])
{
const int mod[20]={1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2};
int len=strlen(buf),a[maxn],i,c,ret=1;
if(len==1)return mod[buf[0]-'0'];
for(i=0;i<len;i++)
a[i]=buf[len-1-i]-'0';
while(len)
{
ret=ret*mod[a[1]%2*10+a[0]]%5;
for(c=0,i=len-1;i>=0;i--)
{
c=c*10+a[i],a[i]=c/5,c%=5;
}
len-=!a[len-1];
}
return ret+ret%2*5;
} int main()
{
char n[maxn];
while(scanf("%s",n)!=EOF)
{
printf("%d\n",lastdigit(n));
}
return 0;
}
数论:HDU1066-Last non-zero Digit in N!的更多相关文章
- POJ 1150 The Last Non-zero Digit 数论+容斥
POJ 1150 The Last Non-zero Digit 数论+容斥 题目地址: id=1150" rel="nofollow" style="colo ...
- hdoj Last non-zero Digit in N! 【数论】
找规律! 求N!最后非0位的值.比方2是120的最后一个不是0的值. 输入N比較大,要大数保存. 注意到最后0的个数是与5的因数的个数相等.设f(n)为n!的最后非0位. 那么f(n)=((n%5)! ...
- 2018.09.17 atcoder Digit Sum(数论)
传送门 数论好题啊. 首先对于b<=sqrt(n)b<=sqrt(n)b<=sqrt(n)的情况直接枚举b判断一下就行了. 下面谈一谈如何解决b>sqrt(n)b>sqr ...
- HDU 1066 Last non-zero Digit in N!(数论,大数,wait)
The expression N!, read as "N factorial," denotes the product of the first N positive inte ...
- HDU 1060 Leftmost Digit (数论,快速幂)
Given a positive integer N, you should output the leftmost digit of N^N. InputThe input contains se ...
- 10162 - Last Digit (数论+周期规律)
UVA 10162 - Last Digit 题目链接 题意:求S=(11+22+...NN)%10 思路:打出0-9的每一个周期,发现周期为1或2或4.所以S是以20一个周期,打出表后发现20为4. ...
- hdu1066 Last non-zero Digit in N!(求阶乘最后一位不为0的数字)
http://acm.hdu.edu.cn/showproblem.php?pid=1066 转自:https://blog.csdn.net/fengyu0556/article/details/5 ...
- HDU 1060 Leftmost Digit 基础数论
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1060 这道题运用的是数学方法. 假设S=n^n.两边同时取对数,得到lgS=nlgn.即有S=10 ...
- sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)
Crack Mathmen Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Since mathmen take securit ...
随机推荐
- Editplus配置java编译环境
EditPlus配置java编译环境 第一步:检测自己是否已经有jdk 1.win+r,输入cmd,打开控制台 2.控制台下输入javac出现类似的画面,代表jdk已经成功安装 第二步:打开Editp ...
- 在sublime text3下,用快捷键把文件打开到浏览器中
使用背景 在编辑html或者js文件的时候,是否想在浏览器中预览一下, 你的步骤可能是这样的: 找到编辑文件的位置, 右键使用某一浏览器打开.如果是这样,你就out了, 因为在sublime中有更加简 ...
- sql单列合并
有一组这样的数据 1 a 10 2 b 2 4 c 5 1 a 5 在应用中,我们可能需要把出现a的数据合并显示: 1 a 10,5 sqlite上实现: SELECT ...
- vscode 常用插件安装
设置中文语言使用快捷键[Ctrl+Shift+P],弹出的搜索框中输入[configure language],然后选择搜索出来的[Configure Display Language],locale ...
- Struts2笔记2
一.请求参数封装 1.属性驱动: (1).无实体类情况:属性和动作类在一起 a.编写jsp页面,提交数据,例如name和age <form acti ...
- logback的configuration
logback的<configuration>只有三个属性: 1.scan[boolean]:当scan被设置为true时,当配置文件发生改变,将会被重新加载.默认值为true. 2.sc ...
- python之字符串切割
Python中split()函数,通常用于将字符串切片并转换为列表. 一.函数说明: split():语法:str.split(str="",num=string.count(st ...
- LeetCode Search Insert Position (二分查找)
题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...
- Java中的字符串问题
本文章分为三个部分: 1.创建字符串对象的两种方式以及它们的存储方式 2.String a = new String("a")创建了几个对象的问题 3.字符串小例子 ------- ...
- pta 编程题13 File Transfer
其它pta数据结构编程题请参见:pta 这道题考察的是union-find并查集. 开始把数组中每个元素初始化为-1,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量 ...