#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
int main()
{
int n;
cin>>n;
unordered_map<int,int>primes;
while(n--)
{
int x;
cin>>x;
for(int i=2;i<=x/i;i++)//可以用线性筛预处理优化
while(x%i==0)
{
x/=i;
primes[i]++;
}
if(x>1)primes[x]++;
}
LL res=1;
for(auto prime:primes)res=res*(prime.second+1)%mod;
cout<<res<<endl;
return 0;
}
求阶乘约数个数之和
#include<bits/stdc++.h>
using namespace std;
int main()
{
int res=0,n;
cin>>n;
for(int i=1;i<=n;i++)res+=n/i;
cout<<res;
return 0;
}
整除分块
ans = 0;
for(int l = 1, r; l <= n; l = r + 1)//知道左端点和分块数值求右端点
{
r = n / (n / l);
ans += n / l * (r - l + 1);
}
GCD/LCM
O
(
l
o
g
(
a
+
b
)
)
O(log(a+b))
O(log(a+b))
ll gcd(ll a, ll b)
{
return b ? gcd(b, a % b) : a;
}
ll lcm(ll a,ll b)
{
return a/gcd(a,b)*b;
}
互质与欧拉函数
欧拉函数
ll phi(ll x)
{
ll res = x;
for (int i = 2; i <= x / i; i ++ )
if (x % i == 0)
{
res = res / i * (i - 1);
while (x % i == 0) x /= i;
}
if (x > 1) res = res / x * (x - 1);
return res;
}
筛法求欧拉函数(1~n,欧拉函数之和)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
ll primes[N],n,phi[N],cnt;
bool st[N];
ll get_eulers(ll n)
{
phi[1]=1;
for(int i=2;i<=n;i++)
{
if(!st[i])
{
primes[cnt++]=i;
phi[i]=i-1;
}
for(int j=0;primes[j]<=n/i;j++)
{
st[i*primes[j]]=1;
if(i%primes[j]==0)
{
phi[i*primes[j]]=phi[i]*primes[j];
break;
}
phi[i*primes[j]]=phi[i]*(primes[j]-1);
}
}
ll res=0;
for(int i=1;i<=n;i++)res+=phi[i];
return res;
}
int main()
{
int n;
cin>>n;
cout<<get_eulers(n);
}
0X03 组合计数
求组合数并取模
递推
O
(
N
2
)
O(N^2)
O(N2)
for (int i = 0; i < N; i ++ )
for (int j = 0; j <= i; j ++ )
if (!j) c[i][j] = 1;
else c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
逆元预处理
O
(
N
l
o
g
N
)
O(NlogN)
O(NlogN)
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010, mod = 1e9 + 7;
int fact[N], infact[N];
int qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1) res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int main()
{
fact[0] = infact[0] = 1;
for (int i = 1; i < N; i ++ )
{
fact[i] = (LL)fact[i - 1] * i % mod;
infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
}
int n;
scanf("%d", &n);
while (n -- )
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", (LL)fact[a] * infact[b] % mod * infact[a - b] % mod);
}
return 0;
}
ACM数学0X01 质数质数判定试除法 O(N)O(\sqrt N)O(N)bool is_prime(long long x){ if (x < 2) return false; for (int i = 2; i <= x / i; i ++ )//i*i<=x可能会溢出 if (x % i == 0)return false; return true;}试除法优化O(N/3)O(\sqrt N/3)O(N/3)bool i