zoukankan      html  css  js  c++  java
  • HDU5528

    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;
    }
    
    
  • 相关阅读:
    天梯赛5-12 愿天下有情人都是失散多年的兄妹 【dfs】
    poj2718 Smallest Difference【贪心】
    HDU problem 5635 LCP Array【思维】
    codeforces 782C Andryusha and Colored Balloons【构造】
    HDU 4278 Faulty Odometer【进制转换】
    codeforces B. The Meeting Place Cannot Be Changed【二分】
    POJ 3264 Balanced Lineup 【线段树】
    HDU 1850
    CodeForces-714C
    HDU Problem 1247 Hat's Words 【字典树】
  • 原文地址:https://www.cnblogs.com/RRRR-wys/p/9736250.html
Copyright © 2011-2022 走看看