ACdream1084

连接:点击打开链接

题意:将A(n , m)转化成k进制后末尾0的个数

代码:

#include <iostream>
#include <cstring>
using namespace std;
long long prime[500005],num[500005];
long long fac[50005],count[50005];
long long getsum(long long a,long long b){
    long long sum=0;
    while(a){
    a/=b;
    sum+=a;
    }
    return sum;
}
int main(){
   long long i,j,k,t,n,m,x,temp,minn,sumsum;
   k=0;
   memset(prime,0,sizeof(prime)) ;
   for(i=2;i<=500000;i++){
   if(!prime[i])
   num[k++]=i;
   for(j=0;j<k&&num[j]*i<=500000;j++){
   prime[num[j]*i]=1;
   if(i%num[j]==0)
   break;
   }
   }
   cin>>t;
   while(t--){
   memset(count,0,sizeof(count));
   cin>>n>>m>>x;                      //转换成k进制有多少个零相当于能连续除几个k
   temp=0;                            //因此将k进行素因子分解,例如20=2*2*5。。。 
   for(i=0;i<k&&num[i]*num[i]<=x;i++){//阶乘有几套k的素因子就有几个零
   if(x%num[i]==0){
   fac[temp]=num[i];
   while(x%num[i]==0){
   x/=num[i];
   count[temp]++;
   }
   temp++;
   }
   }
   if(x>1){
   fac[temp]=x;count[temp]=1;
   temp++;
   }
//   for(i=0;i<temp;i++)
//    cout<<fac[i]<<" "<<count[i]<<endl;
   minn=9999999999;
   for(i=0;i<temp;i++){
   sumsum=getsum(n,fac[i])-getsum(n-m,fac[i]);
   minn=min(sumsum/count[i],minn);
   }
   cout<<minn<<endl;
   }
   return 0;
}
</span>

你可能感兴趣的:(ACdream1084)