upc2585: Rightmost non-zero Digit in N! 快速素数表+快速幂
来源:互联网 发布:c语言画生日蛋糕 编辑:程序博客网 时间:2024/04/25 12:17
2585: Rightmost non-zero Digit in N!
Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 24 Solved: 5
[Submit][Status][Web Board]
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 rightmost non-zero digit of the factorial for N in base B.
Input
In the first line there is an integer T (T <= 100), indicates the number of test cases.
In each case, there is a single line with integers N and B
(0<=N<=10^7, 2<=B<=10^4).
Output
For each case, the output format is “Case #c: ans”.
c is the case number start from 1.
ans is the answer of this problem. The ans is in base 10.
See the sample for more details.
Sample Input
213 311 11
Sample Output
Case #1: 2Case #2: 10
N!在B进制下右边第一个非0的数。
把B分解素因子存到b数组,N!分解素因子存到a数组,从a数组减去最多能减的b数组(a能减去b的几倍说明有几个零),也就是把N!中能产生B(在B进制下产生0)的全去掉,把a数组剩下的乘起来,不断对B取余。
B分解的时候用一般的方法,不断除素数表中的数就行了。
一开始NC,对N!从1循环到N每次都用分解B的方法分解,结果TLE。对于N!含某个素数p的个数,只需要用N不断除以p,每次把除出来的数加上就行了。因为N/p是至少含1个p因子的数的个数(每p个数就至少含有1个至少有1个p因子的数),再除以p是至少含2个p因子的个数...以此类推,直到N为0。
这里a数组剩下乘的时候要用到快速幂,开始打素数表的时候要用快速的那种,否则会TLE。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<cstdlib>#include<cmath>#define INF 0x3f3f3f3f#define MAXN 10000010#define MAXM 700010#define MOD 1000000007#define MAXNODE 4*MAXN#define eps 1e-9using namespace std;int T,N,B,num,a[MAXM],b[MAXM],prime[MAXM];bool vis[MAXN>>1];void prime_table(){ memset(vis,0,sizeof(vis)); int sq=sqrt(MAXN+1); prime[0]=2; num=1; for(int i=3;i<=sq;i+=2){ if(vis[i>>1]) continue; for(int j=i*i;j<=MAXN;j+=(i<<1)) vis[j>>1]=1; } for(int i=1;i<=(MAXN>>1);i++) if(!vis[i]) prime[num++]=i<<1|1;}long long bigpow(long long x,long long n,long long M){ long long ret=1,t=x%M; while(n){ if(n&1) ret=ret*t%M; t=t*t%M; n>>=1; } return ret;}int main(){ freopen("in.txt","r",stdin); int cas=0; prime_table(); scanf("%d",&T); while(T--){ scanf("%d%d",&N,&B); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); int t; for(int i=0;i<num&&prime[i]<=N;i++){ t=N; while(t){ t/=prime[i]; a[i]+=t; } } t=B; for(int i=0;i<num&&prime[i]<=t;i++){ while(t%prime[i]==0){ b[i]++; t/=prime[i]; } } int MAX=INF; for(int i=0;i<num&&prime[i]<=B;i++) if(b[i]) MAX=min(MAX,a[i]/b[i]); for(int i=0;i<num&&prime[i]<=B;i++) if(b[i]) a[i]-=MAX*b[i]; int ans=1; for(int i=0;i<num&&prime[i]<=N;i++) if(a[i]) ans=ans*bigpow(prime[i],a[i],B)%B; printf("Case #%d: %d\n",++cas,ans); } return 0;}
- upc2585: Rightmost non-zero Digit in N! 快速素数表+快速幂
- 2013年辽宁省赛 NEU1311 Rightmost non-zero Digit in N!
- hdu1066 Last non-zero Digit in N!
- hdu1066 Last non-zero Digit in N!
- HDU1066---Last non-zero Digit in N!
- HDU1066 Last non-zero Digit in N!
- hdu1066 Last non-zero Digit in N!
- Last non-zero Digit in N!
- hdu1066Last non-zero Digit in N!
- Last non-zero Digit in N!
- Last non-zero Digit in N!
- 快速排幂-----Digit Rightmost
- HDU Rightmost Digit (快速幂)
- Rightmost Digit(快速幂)
- Rightmost Digit 快速幂取模
- HDU 1061 Rightmost Digit 【快速求幂】
- HDU 1061-Rightmost Digit(快速幂)
- hdu 1061 Rightmost Digit 快速幂
- HDU 1215 七夕节
- linux 启动navicat
- 《算法导论》笔记 第15章 15.4 最长公共子序列
- restrict
- 三种情境下的友好处理
- upc2585: Rightmost non-zero Digit in N! 快速素数表+快速幂
- Android-自定义Dialog
- install apt-get 时被锁定的解决办法
- 2014就分个好看撒地方
- 百度地图坐标转换
- C#实现Web文件上传
- 苹果发布iPad Air及视网膜屏iPad mini
- 欧拉函数
- js 单链表翻转