樱花
求1/x+1/y=1/n!的正整数解数目,答
正文
/*
原题:T1624「一本通 6.2 练习 5」樱花(HackerRank Equations)
题意:求 1/x + 1/y = 1/n! 的正整数解 (x,y) 数目,答案对 1e9+7 取模。
思路:令 N=n!,方程化为 (x-N)(y-N)=N²。正整数解个数 = N² 的正约数个数 = ∏(2e_p+1),
其中 e_p 为质数 p 在 n! 中的指数(e_p=∑⌊n/p^k⌋)。筛出 ≤n 的质数累加即可。
复杂度:时间 O(n log log n),空间 O(n)
易错点:1) 约数个数是 (2e+1) 的乘积而非 e+1;2) 指数求和用 ⌊n/p^k⌋ 累加;3) 乘法边乘边取模防溢出。
*/
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int MOD=1000000007;
const int MAX=1000000;
vector<int> primes;
void sieve(){
vector<bool> isp(MAX+1,true);
isp[0]=isp[1]=false;
for(int i=2;i<=MAX;i++){
if(isp[i]) primes.push_back(i);
for(int j=0;j<(int)primes.size() && i*primes[j]<=MAX;j++){
isp[i*primes[j]]=false;
if(i%primes[j]==0) break;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
sieve();
int n;
cin>>n;
ll ans=1;
for(int p:primes){
if(p>n) break;
ll e=0,pw=p;
while(pw<=n){
e+=n/pw;
pw*=p;
}
ans=ans*(2*e+1)%MOD;
}
cout<<ans<<"\n";
return 0;
}
一本通·提高篇的其它内容
- 活动安排
- 种树
- 喷水装置
- 加工生产调度
- 智力大冲浪
- 数列极差
- 数列分段
- 线段
- 家庭作业
- 钓鱼
- 糖果传递
- 愤怒的牛
- Best Cow Fences
- 曲线
- 数列分段 II
- 扩散
- 灯泡
- 传送带
- 数的划分
- 生日蛋糕
- 小木棍
- Addition Chains
- 埃及分数
- 平板涂色
- 质数方阵
- 靶形数独
- 电路维修
- 魔板
- Knight Moves
- 棋盘游戏
- Keyboarding
- 移动玩具
- 山峰和山谷
- Oulipo
- 图书管理
- Power Strings
- Seekthe Name, Seek the Fame
- Friends
- A Horrible Poem
- Beads
- Antisymmetry
- 门票
- 收集雪花
- 剪花布条
- Power Strings
- Radio Transmission
- OKR-Periods of Words
- 似乎在梦中见过的样子
- Censoring
- Phone List
- The XOR Largest Pair
- Nikitosh 和异或
- Immediate Decodability
- L 语言
- Secret Message 秘密信息
- 背单词
- The Xor-longest Path
- Keywords Search
- 玄武密码
- Censoring