「NOIP模拟赛」欧拉口算 题解

题目描述

令 $C(n)$ 表示 把 $n$ 拆分成 $a\times b=n(a\leq b)$ 且 $a,b$ 的因子个数相同的方案数 给定一个整数$n$,$(1 \leq n \leq 100)$。 求出$C(n!)$。

思路

先把$n!$拆成若干个质数的乘积。 即:$n!={p_1}^{c_1} \times {p_2}^{c_2} \times \dots \times {p_{tot}}^{c_{tot}}$。 然后设$dp[i][j]$表示在$n!=a\times b$中$a$的目前约数个数为$i$,$b$的目前约数个数为$j$的方案数。 很显然,$dp[i\times (k+1)][j\times (c_p-k+1)]+=dp[i][j](0\leq k \leq c_p,1\leq p\leq tot)$。 答案就是$\sum{}{}dp[i][i]$。 当然,这样肯定会TLE。所以运用一下$\text{meet in the middle}$算法的思想。 分别求出$dp1,dp2$,分别表示2、3、5、7这四个素数下的方案与11、13…97素数下的方案。 这样的答案就是$\sum{}{}dp1[i][j]*dp2[i1][j1]$且满足$i/i1=j1/j$。 但是$dp$数组下标太大了,不会$hash$,所以就是用$map$了。

code

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
using namespace std;
#define int long long
inline int read(){
int res=0,f=1;char ch=getchar();
while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
return res*f;
}
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x<10) putchar(x+'0');
else{
write(x/10);
putchar(x%10+'0');
}
}
int n,tot,mi,ans=0,sum=1,Ans[5010],ttt;
int f[5010],top,g[5010],Gu[5010];
int Get(int x){
// cout<<"Get "<<x<<endl;
for(int i=2;i<=x;i++){
if(x%i==0){
while(x%i==0) x/=i,f[i]++;
}
// cout<<i<<" "<<x<<endl;
if(x==1) break;
}
}
map<pair<int,int>,int> dp1,dp2;//lef,rig,dp_val
pair<int,int> mp(int x,int y){
pair<int,int> pp;pp.first=x;pp.second=y;return pp;
}
int gcd(int a,int b){
return !b?a:gcd(b,a%b);
}
map<pair<int,int>,int> getmap(int l,int r){
map<pair<int,int>,int> m1,m2;
m1.clear();m2.clear();
m1[mp(1,1)]=1;
for(int i=r;i>=l;i--){
if(f[i]!=0){
m2.clear();
for(map<pair<int,int>,int>::iterator j=m1.begin();j!=m1.end();j++){
for(int k=0;k<=f[i];k++){
int x=(*j).first.first*(k+1),y=(*j).first.second*(f[i]-k+1);
int g=gcd(x,y);x/=g;y/=g;
m2[mp(x,y)]+=(*j).second;
}
}
m1.swap(m2);
}
}
return m1;
}
signed main(){
cin>>n;
if(n==1){
puts("1");
return 0;
}
for(int i=2;i<=n;i++) Get(i);
dp1=getmap(7,97);
dp2=getmap(0,6);
for(map<pair<int,int>,int>::iterator i=dp1.begin();i!=dp1.end();i++){//x1/x2=y2/y1
if(dp2.count((*i).first)==1) ans+=dp2[(*i).first]*dp1[(*i).first];
}
write(ans/2);putchar('\n');
return 0;
}