ZSTU 4274 約素 約數,不是因子啊!

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");
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章