排列数最后非零位:poj 1150 The Last Non-zero Digit
来源:互联网 发布:mql4编程手册 编辑:程序博客网 时间:2024/06/08 14:23
数论+组合数学(好题)
题目大意:
求排列数A(n,m)最后的非零数位
解题思路:
参考英文文献,多读几遍,豁然开朗,分析见功底。
http://www.cppblog.com/abilitytao/archive/2009/10/31/99907.html
大体思路是这样的。A(n,m)=n!/(n-m)!
首先是除去末尾的非零位,就要对每个阶乘素因子分解找到2,5的幂i,k,
求解思路详见本博客的数论部分,贴代码:
把阶乘n!和(n-m)!的2,5,幂找到相减即可。
接下来就是末尾数字的确定。
记pow_2表示分子减去分母后2的幂;
第一,考虑2,5的幂的影响,如果pow_5>pow_2,最后末位数字一定是5,return 5;如果pow_5==pow_2,末位数字是1;如果pow_5 小于pow_2,末位数字取决于2^d的末位数字(d=pow_2-pow_5),不用求出来,利用循环节(2^1=2,2^2=4,2^3=8,2^4=6,2^5=2,……末位数字变化周期为4)
第二,考虑1,3,7,9的影响。首先应该清楚的是,因子除去所有2,5后末位数字只可能是1,3,7,9;同样利用循环节,只需计数3,7,9的数目即可。care that,我们并不是真的将n!去除2,5而是假设因子已经除去了所有的2,5。
问题的关键是如何计数3,7,9的数目。
这里非常巧妙的利用递归(数学上习惯叫递推公式)。
记f(n,x)表示数列1×2×3×4……×n结果的末位数字x的幂,或者理解为序列1,2,3,……n末位数字x的个数;g(n,x)表示小于等于n的奇数序列末位数字x出现的个数。揣摩递推公式
f(n,x)=f(n/2,x)+g(n,x) (1)
g(n,x)=n/10+(n%10>=x?:1:0)+g(n/5,x) (2)
对于(1),以1,2,3,4,5,6,7,8,9,10为例,分奇数序列1,3,5,7,9和偶数序列2,4,6,8,10;奇数序列结果用g(n,x)计数,偶数序列/2 = 1,2,3,4,5正好等于<=5序列x的个数,用f(n/2)表示(n是偶数、奇数都满足,可以尝试一下)
对于(2),也比较好理解,以g(27,3)为例,分为非5的倍数序列1,3,7,9,11,13,17,19,21,23,27和五的倍数序列5,15,25,后者/5=1,3,5即g(n/5,x);前者中n/10表示循环了几次,循环一次有1个出现,当然还要判断当前循环虽然没有完成但可能达到x,(n%10>=x),满足就要计数+1,递归下去就是答案。
好了,看一下这连个递归程序吧~
最后累乘起来%10取个位就是所求结果。。
参考代码+部分注释:
#include <iostream>#include <cstdio>#include <algorithm>#include <map>#include <vector>#include <cstring>#include <cmath>using namespace std;typedef long long ll;const int maxn = 7e4+10;int table[4][4]={6,2,4,8,//注意%4结果与数组下标一一对应 1,3,9,7,//3 1,7,9,3,//5 1,9,1,9,};//9int n,m;int getpow(int n,int p)//返回n!中质因子p的幂{ int q=p,res=0; while(n>=q){res+=n/q;q*=p;} return res;}int g(int n,int x)//小于等于n的奇数序列中x的幂(或x出现的次数),递归实现(美妙){ if(n==0) return 0; return n/10+(n%10>=x?1:0)+g(n/5,x);//例如g(27,3),取十位数表明之前x循环出现2次,个位7>=3表示x又出现一次}int f(int n,int x)//小于等于n序列中x的幂(或x出现的次数),递归实现(美妙){ if(n==0) return 0; return f(n/2,x)+g(n,x);//偶数序列等价于f(n/2,x)奇数序列等价于g(n,x);}int solve(){ int ans=1; /*****先处理2^i,5^k************/ int pow_2=getpow(n,2)-getpow(n-m,2); int pow_5=getpow(n,5)-getpow(n-m,5); if(pow_2<pow_5) return 5; //如果是n!,i>k,这里是组合数不一定,要判断 else if(pow_2==pow_5) ans=1; else ans*=table[0][(pow_2-pow_5)%4]; // cout<<getpow(n,5)<<endl; // cout<<getpow(n-m,5)<<endl; // cout<<ans<<endl; /*****再处理末尾3,5,9************///n!个位数除完所有2、5后末尾只可能是1,3,7,9 int pow_3=f(n,3)-f(n-m,3); int pow_7=f(n,7)-f(n-m,7); int pow_9=f(n,9)-f(n-m,9); ans=(ans*table[1][pow_3%4])%10; ans=(ans*table[2][pow_7%4])%10; ans=(ans*table[3][pow_9%4])%10; return ans;}int main(){ freopen("input.txt","r",stdin); while(cin>>n>>m){ cout<<solve()<<endl; } return 0;}
- 排列数最后非零位:poj 1150 The Last Non-zero Digit
- poj 1150 The Last Non-zero Digit 求排列数的最后非零数
- POJ 1150 The Last Non-zero Digit 阶乘最后非0位
- POJ 1150-The Last Non-zero Digit(求阶乘最后一位非零数)
- 组合数最后非零位:poj 3406 Last digit
- (hdu step 2.3.7)Last non-zero Digit in N!(阶乘最后一位非零位)
- poj 1150 The Last Non-zero Digit
- poj 1150 The Last Non-zero Digit
- POJ 1150-The Last Non-zero Digit
- poj 1150 The Last Non-zero Digit
- poj 1150 The Last Non-zero Digit
- POJ 1150 The Last Non-zero Digit
- poj 1150 The Last Non-zero Digit
- POJ 1150 The Last Non-zero Digit
- (Relax 数论1.27)POJ 1150 The Last Non-zero Digit(求p(n,m)的最后一个非零数字)
- poj 1150:The Last Non-zero Digit(求A(n, m)的最后一个非0位)
- pku 1150 The Last Non-zero Digit 阶乘最后一位非零数
- POJ 1150 The Last Non-zero Digit 数论+容斥
- 开源ECSHOP中SQL SELECT操作集锦,练习SQL
- 01背包问题和完全背包问题
- 数据结构(陈越)PAT练习题 第五周:图(上)
- 码农提高工作效率
- 微信订餐,订餐外卖系统功能 eatcms
- 排列数最后非零位:poj 1150 The Last Non-zero Digit
- MTK修改记事
- HDU 1556 Color the ball 线段树区间更新裸模板
- ScrollView动态添加组件
- cef3 gyp 生成vs工程文件
- 【Hadoop】用web查看hadoop运行状态
- Activity 人为管理后,导致主线程运行缓慢
- 软件公司中的维护团队建设
- 网络爬虫整理