zoukankan      html  css  js  c++  java
  • 【数学】质数的k次方和

    来源的claris的模板,应该按我的水平不能超过claris。

    质数的0次方和,也就是质数个数。

    已通过wolfram alpha验证。

    int mod;
    
    inline ll add_mod(ll x, ll y) {
        return (x + y >= mod) ? (x + y - mod) : (x + y);
    }
    
    inline ll sub_mod(ll x, ll y) {
        return (x < y) ? (x - y + mod) : (x - y);
    }
    
    inline ll sum(ll n) {
        n %= mod;
        return n;
    }
    
    const int MAXN = 1e6 + 5;
    
    ll ssum[MAXN];
    ll lsum[MAXN];
    bool mark[MAXN];
    
    ll prime_cnt(ll n) {
        const int v = sqrt(n);
        ssum[0] = 0;
        lsum[0] = 0;
        memset(mark, 0, sizeof(mark[0]) * (v + 1));
        for(ll i = 1; i <= v; ++i) {
            ssum[i] = sum(i) - 1;
            lsum[i] = sum(n / i) - 1;
        }
        for(ll p = 2; p <= v; ++p) {
            if(ssum[p] == ssum[p - 1])
                continue;
            ll psum = ssum[p - 1];
            ll q = p * p;
            ll ed = min((ll)v, n / q);
            ll delta1 = (p & 1) + 1;
            for(ll i = 1; i <= ed; i += delta1) {
                if(!mark[i]) {
                    ll d = i * p;
                    ll tmp = (d <= v) ? lsum[d] : ssum[n / d];
                    tmp = sub_mod(tmp, psum);
                    lsum[i] = sub_mod(lsum[i], tmp);
                }
            }
            ll delta2 = p * delta1;
            for(ll i = q; i <= ed; i += delta2)
                mark[i] = 1;
            for(ll i = v; i >= q; --i) {
                ll tmp = ssum[i / p];
                tmp = sub_mod(tmp, psum);
                ssum[i] = sub_mod(ssum[i], tmp);
            }
        }
        return lsum[1];
    }
    

    质数的1次方和,也就是质数求和。

    已通过wolfram alpha验证。
    已通过HDU的2020CCPC的1002验证。

    int mod;
    
    inline ll add_mod(ll x, ll y) {
        return (x + y >= mod) ? (x + y - mod) : (x + y);
    }
    
    inline ll sub_mod(ll x, ll y) {
        return (x < y) ? (x - y + mod) : (x - y);
    }
    
    inline ll mul_mod(ll x, ll y) {
        return x * y % mod;
    }
    
    inline ll sum(ll n) {
        n %= mod;
        return (n * (n + 1)) / 2 % mod;
    }
    
    const int MAXN = 1e6 + 5;
    
    ll ssum[MAXN];
    ll lsum[MAXN];
    bool mark[MAXN];
    
    ll prime_sum(ll n) {
        const int v = sqrt(n);
        ssum[0] = 0;
        lsum[0] = 0;
        memset(mark, 0, sizeof(mark[0]) * (v + 1));
        for(ll i = 1; i <= v; ++i) {
            ssum[i] = sum(i) - 1;
            lsum[i] = sum(n / i) - 1;
        }
        for(ll p = 2; p <= v; ++p) {
            if(ssum[p] == ssum[p - 1])
                continue;
            ll psum = ssum[p - 1];
            ll q = p * p;
            ll ed = min((ll)v, n / q);
            ll delta1 = (p & 1) + 1;
            for(ll i = 1; i <= ed; i += delta1) {
                if(!mark[i]) {
                    ll d = i * p;
                    ll tmp = (d <= v) ? lsum[d] : ssum[n / d];
                    tmp = sub_mod(tmp, psum);
                    tmp = mul_mod(tmp, p);
                    lsum[i] = sub_mod(lsum[i], tmp);
                }
            }
            ll delta2 = p * delta1;
            for(ll i = q; i <= ed; i += delta2)
                mark[i] = 1;
            for(ll i = v; i >= q; --i) {
                ll tmp = ssum[i / p];
                tmp = sub_mod(tmp, psum);
                tmp = mul_mod(tmp, p);
                ssum[i] = sub_mod(ssum[i], tmp);
            }
        }
        return lsum[1];
    }
    

    低次的求和,用拉格朗日插值求出来,然后再改动一下tmp = mul_mod(tmp, p);这里。

  • 相关阅读:
    Jsoup的学习
    String中对字符串进行操作的一些方法
    Httpclient的学习(一)
    初识爬虫见到的两个类 BufferedWriter和 BufferedReader
    ZooKeeper学习笔记(二)——内部原理
    FastDFS安装指南
    基于CentOS6.5的Dubbo及Zookeeper配置
    虚拟机克隆后遇到的网络相关的问题
    ElasticSerach 6.x的安装及配置
    YARN-HA高可用集群搭建
  • 原文地址:https://www.cnblogs.com/purinliang/p/13703156.html
Copyright © 2011-2022 走看看