HDU5528 - Count a * b

Posted rrrr-wys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5528 - Count a * b相关的知识,希望对你有一定的参考价值。

HDU5528 - Count a * b


做法:求(sum_{m|n}(m^2 - sum_{i=1}^{m}sum_{j=1}^m [m|(ij)]))

(h(m) = sum_{i=1}^{m}sum_{j=1}^m [m|(ij)] = sum_{i=1}^msum_{j=1}^m [frac{m}{(i,m)}|frac{i}{(i,m)}j])
$ = sum_{i=1}^msum_{j=1}^m [frac{m}{(i,m)}|j] = sum_{i=1}^m frac{m}{frac{m}{(i,m)}} = sum_{i=1}^m (i,m)$
$ = sum_{d|m} d sum_{i=1}^m [(i,m)=d] = sum_{d|m} d sum_{i=1}^{frac{m}{d}} [(i,frac{m}{d})=1] = sum_{d|m}dvarphi(frac{m}{d})$

$
sum_{m|n} sum_{d|m}dvarphi(frac{m}{d}) = sum_{d|n}dsum_{m|frac{n}{d}}varphi(m) = sum_{d|n}dfrac{n}{d} = sum_{d|n}n
$
所以(ans = sum_{d|n} d^2 - nsigma_0(n))两个值均为积性函数,预处理素因子,合并答案即可。本题时限较紧,不能直接使用试除法分解,尽量优化常数。

#include <bits/stdc++.h>
typedef unsigned long long ll;
const int N = 1e6 + 7;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while( ! isdigit(c) ) { if(c == ‘-‘) f = -1; c = getchar(); }
    while( isdigit(c) ) { x = x*10 + c - ‘0‘; c = getchar(); }
    return x * f;
}
inline void write( ll x ) {
    if( x >= 10 ) write( x / 10 );
    putchar( x % 10 + ‘0‘ );
}
using namespace std;
int T;
ll n;
int p[N], notp[N];
void init() {
    notp[1] = 1;
    for(int i = 2; i <= 1e6; ++i) {
        if(!notp[i]) p[++p[0]] = i;
        for(int j = 1; j <= p[0] && p[j]*i <= 1e6; ++j) {
            notp[i*p[j]] = 1;
            if(i%p[j] == 0) break;
        }
    }
}
ll solve(ll n) {
    ll ans = 1, num = 1, tn = n;
    for(int i = 1; i <= p[0] && p[i]*p[i] <= n; ++i) if(n%p[i] == 0){
        ll t = p[i], tmp1 = 1, tmp2 = 0;
        while(n%t==0) {
            n/=t;
            tmp1 = tmp1*t*t + 1;
            ++tmp2;
        }
        ans *= tmp1;
        num *= (tmp2 + 1);
    }
    if(n!=1) {
        num*=2;
        ans *= (1 + n*n);
    }
    ans -= num*tn;
    return ans;
}
int main() {
    T = read();
    init();
    while(T--) {
        n = read();
        write(solve(n)); putchar(‘
‘);
    }
    return 0;
}





以上是关于HDU5528 - Count a * b的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5528 Count a*b

HDU 6016 - Count the Sheep

hdu 6016 Count the Sheep

hdu5878 I Count Two Three(二分+ 打表)

HDU 4588 Count The Carries 数位DP || 打表找规律

HDU4372 Count the Buildings