标签:hdu def mem prime while init out iostream mod
题意:
给出一个n,在[1, n] 中挑选几个不同的数相乘,求能的到的最大完全平方数
解析:
最大的肯定是n!, 然后n!不一定是完全平方数 (我们知道一个完全平方数,质因子分解后,所有质因子的质数均为偶数)
用勒让德定理求出每个质数在n!中的数量,如果是奇数,则除去一个这个数,偶数不操作
(输出用%I64d
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <list> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d\n", a) #define plld(a) printf("%lld\n", a) #define pc(a) printf("%c\n", a) #define ps(a) printf("%s\n", a) #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 1e7, INF = 0x7fffffff; const LL MOD = 1e9 + 7; int prime[maxn + 10]; int n; int ans; void get_prime() { ans = 0; for(int i = 2; i <= maxn; i++) { if(!prime[i]) prime[++ans] = i; for(int j = 1; j <= ans && prime[j] <= maxn / i; j++) { prime[i * prime[j]] = 1; if(i % prime[j] == 0) break; } } } int f[maxn + 10]; void init() { f[0] = 1; for(int i = 1; i <= maxn; i++) f[i] = (LL)f[i - 1] * i % MOD; } LL check(LL x, LL p) { LL ret = 0; LL P = p; while(P <= x) { ret += x / P; P = p * P; } return ret; } LL q_pow(LL a, LL b) { LL res = 1; while(b) { if(b & 1) res = res * a % MOD; a = a * a % MOD; b >>= 1; } return res; } int main() { get_prime(); init(); while(scanf("%d", &n) != EOF && n) { LL res = 0, pri = 1; for(int i = 1; i <= ans && prime[i] <= n; i++) { LL cnt = check(n, prime[i]); // cout << cnt << " " << prime[i] << endl; if(cnt & 1) pri = (LL)pri * prime[i] % MOD; } // cout << pri << endl; res = (LL)(f[n] * q_pow(pri, MOD - 2) % MOD); printf("%I64d\n", res); } return 0; }
标签:hdu def mem prime while init out iostream mod
原文地址:https://www.cnblogs.com/WTSRUVF/p/10796819.html