bzoj2721樱花——质因数分解

本文针对洛谷题目2721提供了一种基于质因数分解的方法来求解方程1/x+1/y=1/n!的正整数解的组数。通过对x和y的关系进行推导,并利用(n!)^2的约数个数计算解的数量。

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2721

要推式子!

发现x和y一定都比 n! 大。不妨设 x = n!+k;

  则1/x + 1/y = 1/ n!

  <=> ( n! + k + y ) / ( n! + k ) * y = 1 / n!

  <=> n! * y+ k * y= (n!)^2 + n! * k + n! * y

  <=> y = (n!)^2 / k + n!

所以( x, y ) 的个数 =  y 的个数 = k的个数 = (n!)^2 的约数个数。

通过质因数分解求出约数个数。

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int N=1e6+5;
const ll mod=1e9+7;
int n,pri[N],mindiv[N],cnt[N],xnt;
ll ans;
void init()
{
    ans=1;
    for(int i=2;i<=n;i++)
    {
        if(!mindiv[i])pri[++xnt]=i,mindiv[i]=xnt;
        for(int j=1;j<=xnt&&i*pri[j]<=n;j++)// i*pri[j]<=n!!!
        {
            mindiv[i*pri[j]]=j;
            if((i%pri[j])==0)break;
        }
    }
}
ll calc(int a)
{
    while(a!=1)
    {
        cnt[mindiv[a]]++;
        a/=pri[mindiv[a]];
    }
}
int main()
{
    scanf("%d",&n);
    init();
    while(n)calc(n),n--;
    for(int i=1;i<=xnt;i++)
        (ans*=(cnt[i]*2+1))%=mod;
    printf("%lld",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/Narh/p/9119683.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值