NYOJ 70-阶乘因式分解(二)

题目地址:NYOJ 70

思路:n!=(1*2*3*4*......*(n-1)*n)=(m*2m*3m*.....*(k-1)m*km)*其他=(1*2*3*....*k)*m*其他 。当中km是最大值。km<=n所以k的最大值是n/m,所以[1,n]中有n/m个数能被n整除,

然后[1,n]中每一个数除以m,就剩了[1,n/m],然后继续依据上面的步骤进行运算。一直到n=0的时候结束,所得的结果和为终于的结果.

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-6;
int main()
{
    int T,n,m,i;
    LL sum;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        sum=0;
        while(n){
            sum+=n/m;
            n=n/m;
        }
        printf("%d\n",sum);
    }
    return 0;
}



posted @ 2016-02-05 14:44  phlsheji  阅读(171)  评论(0编辑  收藏  举报