阶乘因式分解(一)
时间限制:3000 ms | 内存限制:65535 KB
难度:2
描述
给定两个数m,n,其中m是一个素数。
将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m。
输入
第一行是一个整数s(0
#include <iostream>
#include <stdio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
int s;
scanf("%d",&s);
while(s--){
int n,m;
scanf("%d%d",&n,&m);
int count = 0;
for(int i=1;i<=n;i++) {
int tmp = i;
//printf("%d ",tmp);
while(tmp!=0){
if(tmp%m == 0){
count++;
}else{
break;
}
tmp/=m;
}
//printf("%d\n",count);
}
printf("%d\n",count) ;
}
return 0;
}
注解:
注意在求i有几个j因子时,如10 2
10 / 2 =5 计数加1
5 / 2 =2 计数加1 但实际上5没有2这个因子 ,一次一旦发现不能整除就需要跳过进行下一次循环。(代码中的break正是此作用)