hdu 5528 Count a * b 2015长春区域赛 数论 分析

题目

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5528

题目来源:2015长春区域赛金牌题。

简要题意: f(m)=i=0m1j=0m1[ijmodm≢0]g(n)=mnf(m)[ ] 内为真是 1 否则为0
     g(m)mod264

数据范围: 1T20000;1n109

题解

考虑 gcd(m,ij)=m 时条件不成立, gcd(m,i)=d 的数有 φ(md) 个。

此时需要 mdgcd(m,j)mdj j 也就是md的倍数,有 d 个。

于是有f(m)=m2dmdφ(md)

有了这个结论之后就很开心了,因为可以发现前面和后边都是积性函数,分开算减一下就行了。

前面那个式子我们可以不考虑了,后面那个式子还是有点复杂,容易被卡掉。

接下来令 f(m)=dmdφ(md)g(n)=mnf(m)n=i=1kpaii

在做的时候首先我想到了poj 2480,以及神题解,由积性函数性质可得

f(paii)=pai1i(pi+ai(pi1))

g(n)=i=1kg(paii)=i=1kj=0aif(pji)

A掉之后去看了下叉姐的题解,被瞬间完爆,叉姐神变换如下。

g(n)=mndmdφ(md)=d|ndmd|ndφ(md)=d|ndnd=nd(n)

其中 d(n) n 的约数个数,变换我看不懂,但结论简直太优雅。

最终的原来的g(n)公式就是 g(n)=i=1kj=0aip2jini=1k(ai+1)

实现

这题非常卡常数,基本上只够你分解个质因数。

而且还需要打素数表加速分解质因数。

能够分析出来答案的话整个程序还比较好写。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef unsigned long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head

bool vis[32000];
int p[10000];
int tot = 0;

void getPrime(int n) {
    for (int i = 2; i <= n; i++) {
        if (!vis[i]) p[++tot] = i;
        for (int j = 1; j<=tot && i*p[j]<=n; j++) {
            vis[i*p[j]] = true;
            if (i%p[j]==0) break;
        }
    }
}

vector<PII> fac;

LL get(int x) {
    LL ans = 1, ans2 = 1;
    for (int i = 0; i < fac.size(); i++) {
        LL mul = 1, sum2 = 1;
        for (int j = 1; j <= fac[i].se; j++) {
            mul *= fac[i].fi;
            sum2 += mul*mul;
        }
        ans *= (fac[i].se+1);
        ans2 *= sum2;
    }
    return ans2-ans*x;
}

void getFac(int m) {
    for (int i = 1; p[i]*p[i] <= m; i++) {
        if (m % p[i] == 0) {
            int cnt = 0;
            while (m % p[i] == 0) cnt++, m /= p[i];
            fac.pb(mp(p[i], cnt));
        }
    }
    if (m > 1) fac.pb(mp(m, 1));
}

int main() {
    getPrime(31999);
    int t, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        getFac(n);
        printf("%I64u\n", get(n));
        fac.clear();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值