upc2585: Rightmost non-zero Digit in N! 快速素数表+快速幂

2585: Rightmost non-zero Digit in N!

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit: 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-9
using 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值