51nod-阶乘分数-阶乘数质因数分解/组合计数

题目来源:  Spoj
基准时间限制:1 秒 空间限制:131072 KB 分值: 80  难度:5级算法题
 收藏
 关注
1/N! = 1/X + 1/Y,给出N,求满足条件的整数解的数量。例如:N = 2,1/2 = 1/3 + 1/6,1/2 = 1/4 + 1/4。由于数量可能很大,输出Mod 10^9 + 7。
Input
输入一个数N(1 <= N <= 1000000)。
Output
输出解的数量Mod 10^9 + 7。
Input示例
2
Output示例
2


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

#define ll long long
#define rep(x, y, z) for(int x=y;x<z;x++)
#define lson rt<<1, L, m
#define rson rt<<1|1, m, R
typedef pair<int, int> P;
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define fi first
#define se second
const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;

int n;
ll ans;

ll qmod(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 pr[maxn], vis[maxn], cnt;
//素数筛法
void ini(){ 
	rep(i, 2, maxn){
		if(vis[i]) continue;
		pr[cnt++] = i;
		for(int j = i + i; j < maxn; j += i) vis[j] = 1;
	}
}
//阶乘数质因数分解
ll cal(int n, int p){
	return n < p ? 0 : cal(n / p, p) + n / p;
}
//计数部分:可以先不考虑重复,则方案数为每个质因数的个数+1的乘积。易想到这样通过除以2来去重,但是由于公式转化后为平方,所有质因数均为偶数,会有一组情况没有被算两次,因此要先讲答案+1再除以2。例如n = 3,有2, 2, 3, 3的质因数,对于2,有3种选法,不选,2,22,对于3有3种选法,不选,3,33,应用乘法原理即可,可以注意到23,23这种情况只被计算了一次,因此要+1。
void divide(int n){
	ans = 1;
	for(int i = 0; i < cnt && pr[i] <= n; i++){
		ll t = (cal(n, pr[i]) * 2 + 1) % mod; 
		ans = ans * t % mod;
	}
}

int main(){
#ifdef ac
	freopen("in.txt","r",stdin);
#endif
	//freopen("out.txt","w",stdout);
    cin>>n;
	ini();
	ll m = qmod(2, mod - 2);
	divide(n);
	ans++;
	ans = ans * m % mod;
	cout<<ans<<endl;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值