NYOJ 70 阶乘因式分解(二)

原题链接

这题需要一些小技巧。

附ac代码:

#include <stdio.h>

int main(){
	int t, n, m, count, x;
	scanf("%d", &t);
	while(t-- && scanf("%d%d", &n, &m)){
		count = 0;
		n -= n % m;
		while(n >= m){
			x = n;
			while(x % m == 0){
				++count;
				x /= m;
			}
			n -= m;
		}
		printf("%d\n", count);
	}
	return 0;
}

再附上原题标程:(感谢原作者李文鑫

a /= b;是求共有多少个数里有b;把这些个数加到k里去,在把所有含有b因子的数里除掉一个b。如此循环下去,直到没有数含因子b。

 
/*
http://www.cnblogs.com/liwenxin/archive/2011/04/12/jiechengyinshifenjie.html
*/

#include <iostream>
using namespace std;

int jc(int n,int m)
{
    int sum=0;
    while(n)
    {
        sum+=n/m;
        n/=m;
    }
    return sum;
}

int main()
{
    int s;
    cin>>s;
    while(s--)
    {
        int n,m;
        cin>>n>>m;
        cout<<jc(n,m)<<endl;
    }
    return 0;
}
        


posted on 2014-02-11 23:54  长木Qiu  阅读(133)  评论(0编辑  收藏  举报