4274: 约素
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1852 Solved: 482
Description
判断一个正整数n的约数个数是否为p,其中p是素数。
Input
第一行给测试总数T(T <= 10000)。
接下来有T行,每行有两个数字n(1 <= n <= 1000000000)和p(2 < p <= 1000000000)。
Output
每组测试数据输出一行,如果n的约数个数是p,输出“YES”,否则输出“NO”。
Sample Input
5
64 7
911 233
1080 13
1024 11
20170211 1913
Sample Output
YES
NO
NO
YES
NO
题目链接:http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4274
题解:
求一个数n的“约数”个数是不是等于p,只需要枚举sqrt(n)之前的数字,如果能整除n,那就产生两个约数n/i和i,不过,当n/i == i的时候,也就是i*i==n时,只增加一个约数。
看了题解之后内心是一千万只草泥马蹦腾而过的感觉。。。这题我wa了不下20次。。。。是约数不是因子啊!!!亲,你看的是假题目哟?
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<sstream>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int main(){
int t;
scanf("%d", &t);
while(t--){
int n, p;
scanf("%d%d", &n, &p);
int cnt = 0;
for(int i=1; i<=sqrt(n); i++){
if(n%i == 0){
if(i*i == n)
cnt++;
else
cnt += 2;
}
}
if(cnt == p){
printf("YES\n");
}
else{
printf("NO\n");
}
}
}