阶乘因式分解(一)

时间:2015-01-31 20:36:49   收藏:0   阅读:198

阶乘因式分解(一)

时间限制:3000 ms | 内存限制:65535 KB
难度:2
 
描述

给定两个数m,n,其中m是一个素数。

将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m。

 
输入
第一行是一个整数s(0<s<=100),表示测试数据的组数 随后的s行, 每行有两个整数n,m。
输出
输出m的个数。
样例输入
2
100 5
16 2
样例输出
24
15
my answer:#include<iostream>
using namespace std;
int shumu(int a,int b)
{
	int c = 0;
	while(a%b == 0)
	{
		c++;
		a=a/b;
	}
	return c;
}
int main()
{
	int n;
	cin>>n;
	while(n--){
		int x,y;
		cin>>x>>y;
		int count = 0;
		for(int i = 1; i <=x; i ++)
		{
			count+=shumu(i,y);
		}
		cout<<count<<endl;
	}
	return 0;
}最优代码: 
#include<iostream>
using namespace std;
int get(int n,int num)
{
	if(n==0) return 0;
	else return get(n/num,num)+n/num;
}
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		int a,b;
		cin>>a>>b;
		cout<<get(a,b)<<endl;
	}
}        

  

 
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!