BZOJ 3209 数位dp(求单个phi值模板)

47 篇文章 0 订阅
25 篇文章 0 订阅

传送门

题意是求\prod_{i=1}^{n} biultin popcount(i),n是1e15,枚举1的个数,数位dp

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+10;
const int p=10000007;
int p2;
ll num[60], dp[60][60][60];
/*************************/ //O(sqrt(n))
ll eular(ll n){
    ll ans=n;
    for(int i=2; i*i<=n; i++)
    if(n%i==0){
        ans-=ans/i;
        while(n%i==0)
            n/=i;
    }
    if(n>1) ans-=ans/n;
    return ans;
}
/*****************************/
ll dfs(int pos, bool limit, int s, int tar){
    if(pos==0)
        return s==tar;
    if(s>tar || s+pos<tar) return 0;
    if(!limit && dp[tar][pos][s]!=-1)
        return dp[tar][pos][s];
    int up=limit ? num[pos]:1;
    ll ret=0;
    for(int i=0; i<=up; i++)
        ret=(ret+dfs(pos-1, limit&&(i==up), s+i, tar))%p2;
    return limit ? ret : dp[tar][pos][s]=ret;
}

ll qpow(ll bas, ll m){
    ll ans=1;
    while(m){
        if(m&1)
            ans=ans*bas%p;
        m>>=1;
        bas=bas*bas%p;
    }
    return ans;
}

void solve(ll n){
    memset(dp, -1, sizeof dp);
    int cnt=0;
    while(n){
        num[++cnt]=n&1;
        n>>=1;
    }
    ll ans=1;
    for(int i=1; i<=cnt; i++){
        ll tmp=dfs(cnt, 1, 0, i);
        tmp+=p2;
        ans=ans*qpow(i, tmp)%p;
    }
    printf("%lld\n", ans);
}

int main()
{
    p2=eular(p);
    ll n;
    scanf("%lld", &n);
    solve(n);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值