51Nod 1189 数论 + 线性筛

题目链接


题意:
1 / N ! = 1 / X + 1 / Y ( 0 &lt; x &lt; = y ) 1/N! = 1/X + 1/Y(0&lt;x&lt;=y) 1/N!=1/X+1/Y0<x<=y,给出 N N N,求满足条件的整数解的数量。例如: N = 2 , 1 / 2 = 1 / 3 + 1 / 6 , 1 / 2 = 1 / 4 + 1 / 4 N = 2,1/2 = 1/3 + 1/6,1/2 = 1/4 + 1/4 N=21/2=1/3+1/61/2=1/4+1/4。由于数量可能很大,输出 m o d mod mod 1 0 9 + 7 10^9 + 7 109+7


思路:

尝试变换式子:
1 n ! = 1 x + 1 y = x + y x y \frac{1}{n!} = \frac{1}{x} + \frac{1}{y} = \frac{x+y}{xy} n!1=x1+y1=xyx+y
则:
n ! = x y x + y n! = \frac{xy}{x+y} n!=x+yxy
x y − x n ! − y n ! = 0 xy - xn! - yn! = 0 xyxn!yn!=0
推出:
( x − n ! ) ( y − n ! ) = ( n ! ) 2 (x-n!)(y-n!) = (n!)^2 (xn!)(yn!)=(n!)2

因为 1 n ! = 1 x + 1 y \frac{1}{n!} = \frac{1}{x} + \frac{1}{y} n!1=x1+y1
x , y &gt; n ! x,y &gt; n! x,y>n!

则x,y的数目等价于 ( n ! ) 2 (n!)^2 (n!)2的因子个数
x &lt; = y x&lt;=y x<=y故 最终答案为(因子个数+ 1 1 1)/ 2 2 2,向下取整。

处理因子个数可以用因子个数定理,对于 n ! n! n!,可以用线性筛处理,总复杂度:
O ( n l o g n ) O(nlogn) O(nlogn)

代码:

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;

const int mod = 1e9 + 7;
const int A = 1e6 + 10;
bool vis[A];

ll fast_mod(ll n,ll m){
    ll res = 1;
    while(m){
        if(m&1) res = (res*n)%mod;
        n = n*n%mod;
        m >>= 1;
    }
    return res;
}

int main(){
    ll ans = 1;

    int n;
    scanf("%d",&n);

    for(int i=2 ;i<=n ;i++){
        if(!vis[i]){
            int cnt = 0;
            for(int j=i ;j<=n ;j+=i){
                vis[j] = 1;
                int now = j;
                while(now%i == 0){
                    now /= i;
                    cnt++;
                }
            }
            ans = ans*(1LL*2*cnt+1)%mod;
        }
    }

    ll inv2 = fast_mod(2,mod-2);
    ans = (ans+1)%mod*inv2%mod;
    if(ans<0) ans += mod;
    printf("%I64d\n",ans);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值