[COGS 2524]__完全平方数

Description

一个数如果是另一个整数的完全平方,那么我们就称这个数为完全平方数(Pefect Sqaure),也称平方数。小A认为所有的平方数都是很perfect的~

于是他给了小B一个任务:用任意个不大于n的不同的正整数相乘得到完全平方数,并且小A希望这个平方数越大越好。请你帮助小B告诉小A满足题意的最大的完全平方数。

Input

输入仅 1行,一个数n。

Output

输出仅 1 行,一个数表示答案。由于答案可以很大,

所以请输出答案对 100000007 取模后的结果。

Sample Input1

7

Sample Output1

144

Sample Input2

9

Sample Output2

5184

题解

完全平方数,要保证所有质因数的次数都是偶数,

贪心的思想,因数越多越好,我们不妨将$1~n$全选,再全部质因数分解。

若枚举到的质因数次数为奇,$-1$即可。

 1 #include <set>
 2 #include <map>
 3 #include <ctime>
 4 #include <cmath>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <string>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 #define LL long long
15 #define Max(a, b) ((a) > (b) ? (a) : (b))
16 #define Min(a, b) ((a) < (b) ? (a) : (b))
17 #define sqr(x) ((x)*(x))
18 using namespace std;
19 const int N = 5000000;
20 const LL MOD = 100000007;
21 
22 int n;
23 bool isprime[N+5];
24 int q[N+5], tot;
25 int pre[N+5], cnt[N+5];
26 
27 void prepare() {
28     memset(isprime, 1, sizeof(isprime));
29     isprime[1] = false;
30     for (int i = 2; i <= n; i++) {
31     if (isprime[i]) q[++tot] = i;
32     for (int j = 1; j <= tot && i*q[j] <= n; j++) {
33         isprime[i*q[j]] = false;
34         pre[i*q[j]] = q[j];
35         if (!(i%q[j])) break;
36     }
37     }
38 }
39 
40 LL pow(LL a,LL b) {
41     LL c = 1;
42     while (b) {
43     if (b&1) c = (c*a)%MOD;
44     b >>= 1;
45     a = (a*a)%MOD;
46     }
47     return c;
48 }
49 
50 int main() {
51     scanf("%d", &n);
52     prepare();
53     for (int i = 2; i <= n; i++) {
54     int j = i;
55     while (pre[j]) {
56         cnt[pre[j]]++;
57         j /= pre[j];
58     }
59     cnt[j]++;
60     }
61     LL ans = 1;
62     for (int i = 2; i <= n; i++) ans = (ans*pow((LL)i, (LL)cnt[i]/2*2))%MOD;
63     printf("%lld
", ans);
64     return 0;
65 }
原文地址:https://www.cnblogs.com/NaVi-Awson/p/7496025.html