“玲珑杯”ACM比赛 Round #9 A -- Check-in Problem [因子个数]【数论】
2017-02-13 08:51
525 查看
题目连接:http://www.ifrog.cc/acm/problem/1084
———————————————————————————————————-.
A – Check-in Problem
Time Limit:5s Memory Limit:128MByte
Submissions:921 Solved:55
DESCRIPTION
A positive integer x is called p-bizarre number if the number of the divisors of x is p exactly.
Your task is testing whether the given positive integer n is a p-bizarre number or not.
INPUT
The first line contains a positive integer T, which represents there are T test cases.
The following is test cases. For each test case:
The only one line contains a positive integer n and an odd prime p.
1≤T≤10^5,1≤n≤10^18,2< p≤10^9
OUTPUT
For each test case, output in one line, print “YES” (without quote) if n is a p-bizarre number, print “NO” (without quote) otherwise.
SAMPLE INPUT
3
9 3
971528476274196481 7
150094635296999121 37
SAMPLE OUTPUT
YES
NO
YES
———————————————————————————————————-.
题目大意:
就是问你n的因子个数是不是p个
解题思路:
对于一个素数n的因子个数 我们可以对n做算术基本定理展开
n=pa11×pa22×pa33×...×parr
那么数的因子个数就是 ∑ri=1(a1+1)×(a2+1)×(a3+1)×...×(an+1)
input里面又说
The only one line contains a positive integer n and an odd prime p.
那么对于p是素数的情况 只能说明n的质因子只有一种,
因为上述,所以我想到以对1e6(因为题目说p最小是3,n最大是10^18,所以1e6就够了)内的素数筛法取一遍,然后二分寻找答案即可注意会爆LL ,但是无论怎么控制溢出,最后代码写成了这样但是还是WA…心塞…
献上官方题解
注意到p 是质数,只有当 n 是质数的 p−1次幂时, n 的约数才可能恰好有 p 个,所以判定一个正整数 n 是 p−奇异数,只需检验 p−1n√ 是整数,且 p−1n√ 是质数。预处理 109−−−√ 以内的素数(共 3401 个),进行开根和判断素数即可,时间复杂度O(n√lnn)。
事实上 p>3 的情况很少有解,直接预处理所有有解的情况即可,可以防止写出有问题的开根,而 p=3 的判断素数也可以用
Miller-Rabin 算法判定(需要 O(1) 的模乘法)。
改了2个小时的溢出,最后都没签到。。。。。。
献上标程一枚
———————————————————————————————————-.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
[/code]
———————————————————————————————————-.
A – Check-in Problem
Time Limit:5s Memory Limit:128MByte
Submissions:921 Solved:55
DESCRIPTION
A positive integer x is called p-bizarre number if the number of the divisors of x is p exactly.
Your task is testing whether the given positive integer n is a p-bizarre number or not.
INPUT
The first line contains a positive integer T, which represents there are T test cases.
The following is test cases. For each test case:
The only one line contains a positive integer n and an odd prime p.
1≤T≤10^5,1≤n≤10^18,2< p≤10^9
OUTPUT
For each test case, output in one line, print “YES” (without quote) if n is a p-bizarre number, print “NO” (without quote) otherwise.
SAMPLE INPUT
3
9 3
971528476274196481 7
150094635296999121 37
SAMPLE OUTPUT
YES
NO
YES
———————————————————————————————————-.
题目大意:
就是问你n的因子个数是不是p个
解题思路:
对于一个素数n的因子个数 我们可以对n做算术基本定理展开
n=pa11×pa22×pa33×...×parr
那么数的因子个数就是 ∑ri=1(a1+1)×(a2+1)×(a3+1)×...×(an+1)
input里面又说
The only one line contains a positive integer n and an odd prime p.
那么对于p是素数的情况 只能说明n的质因子只有一种,
因为上述,所以我想到以对1e6(因为题目说p最小是3,n最大是10^18,所以1e6就够了)内的素数筛法取一遍,然后二分寻找答案即可注意会爆LL ,但是无论怎么控制溢出,最后代码写成了这样但是还是WA…心塞…
献上官方题解
注意到p 是质数,只有当 n 是质数的 p−1次幂时, n 的约数才可能恰好有 p 个,所以判定一个正整数 n 是 p−奇异数,只需检验 p−1n√ 是整数,且 p−1n√ 是质数。预处理 109−−−√ 以内的素数(共 3401 个),进行开根和判断素数即可,时间复杂度O(n√lnn)。
事实上 p>3 的情况很少有解,直接预处理所有有解的情况即可,可以防止写出有问题的开根,而 p=3 的判断素数也可以用
Miller-Rabin 算法判定(需要 O(1) 的模乘法)。
改了2个小时的溢出,最后都没签到。。。。。。
献上标程一枚
———————————————————————————————————-.
#include <cmath> #include <stdio.h> #include <cassert> #include <algorithm> typedef long long LL; const int maxn = 31623, maxm = 17, maxp = 61, maxt = 100001, maxv2 = (int)1e9; const LL maxv = (LL)1e18; int tot, pr[maxn], d[maxn], sz[maxm]; LL pp[maxm][maxn]; bool isprime(int x) { if(x < 2) return 0; if(x < maxn) return d[x] == x; for(int i = 0; i < tot && pr[i] * pr[i] <= x; ++i) if(x % pr[i] == 0) return 0; return 1; } int main() { for(int i = 2; i < maxn; ++i) { if(!d[i]) pr[tot++] = d[i] = i; for(int j = 0, k; (k = i * pr[j]) < maxn; ++j) { d[k] = pr[j]; if(d[i] == pr[j])break; } } for(int i = 2; i < maxm; ++i) for(int j = 0; j < tot; ++j) { int rem = pr[i] - 1; LL val = 1, lim = maxv / pr[j]; for( ; rem && val <= lim; --rem, val *= pr[j]); if(rem) break; pp[i][sz[i]++] = val; } int t; LL n, p; assert(scanf("%d", &t) == 1 && 1 <= t && t < maxt); while(t--) { assert(scanf("%lld%lld", &n, &p) == 2 && 1 <= n && n <= maxv && (p & 1) && p <= maxv2 && isprime(p)); if(p >= maxp || d[p] != p) { puts("NO"); continue; } if(p == 3) { LL val = (LL)sqrt(n); for( ; val * val > n; --val); for( ; (val + 1) * (val + 1) <= n; ++val); puts(val * val == n && isprime(val) ? "YES" : "NO"); continue; } for(int i = 2; i < maxm; ++i) if(pr[i] == p) { puts(*std::lower_bound(pp[i], pp[i] + sz[i], n) == n ? "YES" : "NO"); break; } } return 0; }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
[/code]
相关文章推荐
- “玲珑杯”ACM比赛 Round #9 A -- Check-in Problem [因子个数]【数论】
- “玲珑杯”ACM比赛 Round #9 题解 (待续)
- “玲珑杯”ACM比赛 Round #18 B -- 数论你还会快速幂【规律】
- 玲珑杯1052 - See car——STL集合的应用(“玲珑杯”ACM比赛 Round #4 )
- “玲珑杯”ACM比赛 Round #18 C -- 图论你先敲完模板(dp)
- “玲珑杯”ACM比赛 Round #18 A -- 计算几何你瞎暴力
- 玲珑杯”ACM比赛 Round #18 C -- 图论你先敲完模板
- “玲珑杯”ACM比赛 Round #19 A.A simple math problem【打表找规律】
- “玲珑杯”ACM 热身赛 # 2.5 A-B (数论)
- “玲珑杯”ACM比赛 Round #13
- “玲珑杯”ACM比赛 Round #16 Down the Rabbit Hole
- “玲珑杯”ACM比赛 Round #4 E -- array(dp)
- “玲珑杯”ACM比赛 Round #19 B.Buildings【二分+RMQ】
- "玲珑杯”ACM比赛 Round #8-E XJT Love Digits(递推)
- Lonlife-ACM 1005 - Spoon Devil's RP Test(同余定理)——“玲珑杯”acm比赛-试运行赛
- “玲珑杯”ACM比赛 Round #4 E题
- “玲珑杯”ACM比赛 Round #18 A -- 图论你先敲完模板(DP+思路)
- "玲珑杯”ACM比赛 Round #8-D XJT Loves Boggle(dfs)
- 玲珑杯”ACM比赛 Round #15 D 咸鱼商店【二分+01背包】
- 玲珑杯”ACM比赛 Round #4 1054 - String cut 暴力。学到了扫描的另一种思想