Factorials 阶乘

    题目大意:给你一个数n,求出n ! 的最后一个非零位。

    注释:n<=4200

      想法:开始的想法是觉得这道题应该比较的有趣,因为我们知道,一个数的阶乘的最后的非零位后面或者是0,或者n<=4,所以,我们思考,如何才能有效的登出这个非零位。首先,我们发现,这个非零位后面零的个数是和n!中5的个数有关的,所以,我们思考:如果我们使得这个阶乘没有5会怎么样。想着想着,我相信你的头脑里会自然地蹦出一个定理——唯一分解定理。为什么?因为只有在这个定理的辅助下你才可以将5全部提取出来。我们又想到:由于唯一分解定理的存在,每个数都是有一个或几个定下来的素数组成,我们只需要这句话的一个性质:素数。一个数由素数组成,显然,这个素数是不大于本数的,n的数据范围是4200,是完全在我们的接受范围之内,想到这,这道题的大体轮廓就分为这样几个步骤:

      1.筛出n之前的所有素数,由于n的数据范围过小,我们可以O ( n ) 的方法去筛。

      2.对于每一个素数,我想求出n!中这个元素最多可以被整除多少次,也就是说我们到底有多少数包含多少这个素数。在此,介绍一个定理$f(n,k)=\sum\limits_{i=1}^{\infty} \lfloor \frac{n}{k^i}\rfloor$其中,f(n,k),表示n!中k的个数。

      3.这么筛,显然不对,4200里面2的个数就够我们受的了,我们想得到一种优化,我们发现,我们其实只需要得到这个素数的最后一位即可。

      4.但,还是有些困难,我们又发现了,对于每一个素数来讲(假设这个素数是a)$a^{4*k+i}=a^i$,我们只需处理%4意义下的即可。但是,a==2是需要特判。

      呼~长出一口气,这题就切了。

    最后,附上丑陋的代码......

 #include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int x[];
int ans[];
int num(int a,int b)//计算素数在n!中的个数,这个函数表示b在a!中的个数
{
int ans=;
while(a)
{
ans+=a/b;
a/=b;
}
return ans;
}
int power(int a,int b)//快速幂,其实可以直接乘,因为我们只考虑模4意义下
{
a%=;
int ans=;
while(b)
{
if(b&) ans=(ans*a)%;
b>>=;
a=(a*a)%;
}
return ans;
}
bool prime(int a)//判断是否为素数
{
int k=(int)(sqrt(a));
bool flag=true;
for(int i=;i<=k;i++)
{
if(a%i==)
{
flag=false;
break;
}
}
return flag;
}
int main()
{
int n;
int cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++)//筛素数
{
if(prime(i)) x[++cnt]=i;
}
for(int i=;i<=cnt;i++)//用ans[]存素数个数
{
ans[x[i]]+=num(n,x[i]);
}
ans[]-=ans[];//我们再次用等数量的2将5替换掉,以便将最后的零去掉。
ans[]=;
int ansans=;
for(int i=;i<=cnt;i++)//对于每一个素数来讲,我们进行计算
{
ans[x[i]]%=;
if(ans[x[i]]==&&x[i]==)//特判2,因为别的素数的4次方的最后一位都是1(5已经除去),但2不是
{
ansans*=;
ansans%=;
}
ansans*=power(x[i],ans[x[i]]);
ansans%=;//我们只要最后一位
}
printf("%d\n",ansans);
return ;
}

    小结:错误:

      2A,第一次忘记特判2。

Factorials的更多相关文章

  1. HackerRank Extra long factorials

    传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...

  2. 每日一九度之 题目1038:Sum of Factorials

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  3. POJ 1775 (ZOJ 2358) Sum of Factorials

    Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...

  4. (Problem 34)Digit factorials

    145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...

  5. USACO 3.2 Factorials

    Factorials The factorial of an integer N, written N!, is the product of all the integers from 1 thro ...

  6. 【CodeChef】Small factorials(BigInteger笔记)

    You are asked to calculate factorials of some small positive integers. Input An integer t, 1<=t&l ...

  7. 九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1845 解决:780 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  8. SPOJ:Easy Factorials(占位)

    Finding factorials are easy but they become large quickly that is why Lucky hate factorials. Today h ...

  9. LightOJ - 1189 - Sum of Factorials

    先上题目 Sum of Factorials Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

随机推荐

  1. No bean named 'hibernateTemplate' is defined

    1.错误描述 WARN:2015-05-01 15:42:22[localhost-startStop-1] - Exception encountered during context initia ...

  2. java中final和static

    final的意思是最终的,最后的额,不可变的,在java中也具有相似的含义. final修饰基础数据表示把该数据修饰成常量,意味着不可修改,不可变. final修饰对象的引用的时候,表示该引用不可变, ...

  3. Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.HibernateTemplate

    1.错误描述 严重: Context initialization failed org.springframework.beans.factory.CannotLoadBeanClassExcept ...

  4. windows共享虚拟机ubuntu目录

    1)安装 sudo apt-get install samba 2)配置文件vi /etc/samba/smb.conf 添加如下 3)启动服务 sudo service smbd start 4)w ...

  5. freemarker之include指令(九)

    freemarker之include指令 1.父页面ftl <html> <head> <meta http-equiv="content-type" ...

  6. 爬虫抓包工具Charles设置

    1.安装注册 感谢https://www.jianshu.com/p/89111882fa99提供注册码软件去官网下载安装即可. Registered Name:https://zhile.io Li ...

  7. 【Luogu1919】 A*B Problem升级版(FFT)

    题面戳我 题解 把每个数都直接看做一个多项式,每一位就是一项 现在求用FFT求出卷积 然后考虑一下进位就可以啦 #include<iostream> #include<cstdio& ...

  8. [HDU5765]Bonds

    题面 题意 给出一张\(n\)点\(m\)边无向连通图,求每条边出现在多少个割集中. \(n\le20,m\le\frac{n(n-1)}{2}\) sol 所谓割集,就是指把\(n\)个点分成两个集 ...

  9. iOS开发--XMPPFramework--好友列表(五)

    上一篇文章,我们讨论了调试和好友模块,这一篇,在引入了好友模块后,我们来说说好友列表的显示. 还记得在上一篇中,我们把自动拉去好友列表给关掉了,所以,我们选择在控制器的-(void)viewDidLo ...

  10. angular路由详解三(路由参数传递)

    我们经常用路由传递参数,路由主要有三种方式: 第一种:在查询参数中传递数据 {path:"address/:id"}   => address/1  => Activa ...