2019 hdu多校round1 1011 Function(数论+线性预处理)

题目传送门

简单题意

\sum_{i = 1}^{n} gcd(\lfloor \sqrt[3]{i}\rfloor,i)\mod998244353,n<=1e21

算法

先分块,有\sum_{i = 1}^{\lfloor\sqrt[3]{n}\rfloor}\sum_{j = i^3}^{min(n,(i+1)^3+1)}gcd(i,j)

令函数f(i,j)=\sum_{x=1}^{m}gcd(i,x)\Rightarrow f(i,j) = \lfloor\frac{j}{i}\rfloor f(i,i)+f(i,j\%i)

f(i,i)=\sum_{d|i}d\phi(\frac{i}{d}),f(i,j)=\sum_{d|i}\frac{j}{d}\phi(d)

\sum_{j=i^3}^{(i+1)^3+1}gcd(i,j),通过差分有f(i,(i+1)^3+1)-f(i,i^3)=3*(i+1)f(i,i)+gcd(i,i^3)=3*(i+1)f(i,i)+i,显然f(i,i)通过线性欧拉筛可以O(n+\sqrt{n}\log{n})预处理.

所以显然只需要考虑上界为n时的情况,O(\sqrt{n})的跑一边f(i,n)即可。

这题卡常很严,所以离线查询(__int128长见识了),整体复杂度为O(n+(t+logn)*\sqrt{n})

#include 
using namespace std;
typedef long long ll;
const int N = 1e7 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x;}
ll gcd(ll a,ll b){return b ? gcd(b,a % b) : a;}
int lcm(int a,int b){return a / gcd(a,b) * b;}
ll fpow(ll a,ll b)
{
    ll res = 1;
    while(b){
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
void read(__int128 &x) {
    static char ch;static bool neg;
    for(ch=neg=0;ch<'0' || '9' a[cnt].n){
            if(cnt == t - 1){
                pos[cnt] = i;
                break;
            }
            while((i + 1) * (i + 1) * (i + 1) > a[cnt].n){
                pos[cnt] = i;
                cnt++;
                ans[cnt] = ans[cnt - 1];
            }

        }
        ans[cnt] = (ans[cnt] + 3LL * (i + 1) * f[i] + i) % mod;
        if((i + 1) * (i + 1) * (i + 1) == a[cnt].n) ans[cnt] = (ans[cnt] + i + 1) % mod,pos[cnt] = 0;
    }
    for(int jq = 0;jq < t;++jq){
        if(pos[jq]){
            ll rnt = a[jq].n % pos[jq],ret = a[jq].n / pos[jq] % mod;
            for(ll i = 1;i * i <= pos[jq];++i){
                ll l = pos[jq] / i;
                if(pos[jq] % i == 0){
                    ans[jq] = (ans[jq] + (rnt / i) * phi[i]) % mod;
                    if(i * i < pos[jq]) ans[jq] = (ans[jq] + (rnt / l) * phi[l]) % mod;
                }
                i = pos[jq] / l;
            }
            ans[jq] = (ans[jq] + (ret - (pos[jq] * pos[jq])) % mod * f[pos[jq]] % mod + pos[jq] + mod) % mod;
        }
    }
    for(int i = 0;i < t;++i){
        for(int j = 0;j < t;++j)
            if(a[j].id == i)printf("%lld\n",ans[j]);
    }
    return 0;
}

 

你可能感兴趣的:(数论)