Faculty Dividing Powers--数论

#include <iostream>

#include <stdio.h>

using namespace std;

#define N 1200005

#define inf ~0U>>1

#define LL __int64


int a[6000000],p[N];


LL cal(LL n,LL k)

{

    LL sum=0;

    while(n>=1) {

        n/=k;

        sum+=n;

    }

    return sum;

}


int main()

{

    int i,j,t,cnt=0;

    memset(a,0,sizeof(a));

    memset(p,0,sizeof(p));

    for(i=2;i<=N;i++) {

        if(!a[i])

            p[++cnt]=i;

        for(j=i+i;j<N;j+=i)

            a[j]=1;

    }

    scanf("%d",&t);

    while(t--) {

        LL n,k,ans=inf;

        scanf("%I64d%I64d",&n,&k);

        ans*=inf;

        int num=1;

        for(i=1;k>1&&k>p[i]&&i<=cnt;i++)

            if(k%p[i]==0) {

                num=1;

                for(k/=p[i];k%p[i]==0;k/=p[i])

                    num++;

                ans=min(ans,cal(n,p[i])/num);

            }

        if(k>1)

            ans=min(ans,cal(n,k));

        printf("%I64d\n",ans);

    }

    return 0;

}

/*

 任何一个合数都可以表示成几个质数的乘积,质数的乘积是本身。

 任何一个数都可以表示成几个素数的和,素数是他本身

 比如要算 10!可以最大整除除以2 的多少次方 

 a10  9 8 7 6 5 4 3 2  1

 a列的数除以2 变为(取整)

 b5 4 3 2 1

 同理得

 c2 1

 那么ans=10/2+5/2+2/2

 

 

 

 题意是说给出n , k 求出最大的 i 使得 n! % k^i == 0 ...

 假设最简单的情况...k是质数...要求 n! = 1*2*3...*n ...易看出在k的倍数里..1k..k的平方的有2k..k的立方中有3k... 那么 n!  k的个数为  n/k+n/(k^2)+n/(n^3)....及为最大的 i ...

 拓展一步..k非质数..但只有一个质因子..89125 之类的...可以先求出在 n! 中有多少个其质因子,设为x...那么有多少个k..就是 i = x/p...p是指k为起质因数的多少次方..

 最终拓展出题目所要求的任意数的情况..k=a1^k1 * a2^k2 * a3^k3...an^kn  其中a1,a2...an为质数..可以算出n!中有多少a1,a2,a3...an...而组成一个k需要k1a1..k2a2..knan..那么也就是说n(a1)/k1 , n(a2)/k2 .... n(an)/kn...中最小的就是答案...

 Problem description

 Fred Faculty and Paul Power love big numbers. Day after day Fred chooses a random integer n and he computes n!. His friend Paul amuses himself by computing several powers of his randomly chosen integer k like k2, k3 and so on. On a hot summer day, Fred and Paul got really, really bored, so they decided to play a joke on their buddy Dave Divider. Fred chooses a random integer n while Paul chooses a random integer k. They want Dave to find the biggest integer i such that ki divides n! without a remainder, otherwise they will throw a cake in Dave's face. Because Dave does not like cakes in his face, he wants you to help him finding that integer i.

 

 Input

 The first line contains the number of test cases t (1 ≤ t ≤ 100). Each of the following t lines contains the two numbers n,k (2 ≤ n ≤ 1018, 2 ≤ k ≤ 1012) separated by one space.

 

 Output

 For each test case, print the maximum integer i on a separate line.

 

 Sample Input

 2

 5 2

 10 10

 Sample Output

 3

 2

*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值