Graph Theory Class(min25筛求1e10素数前缀和)

Problem Description

This class is on graph theory. Mr. Kruskal teaches babies the concept of minimal spanning tree, and how to calculate the minimal spanning tree of a given graph.

Now, it's time for an in-class quizz. Mr. Kruskal shows a special graph G: G is a complete undirected graph with n vertices, and vertices in G are indexed from 1 to n. The weight of the edge between the ith vertex and the jth vertex is equal to lcm(i+1,j+1). Babies are asked to find the minimal spanning tree of G.

As a super baby, Baby Volcano quickly finds an answer, but he is not sure on the correctness of his answer. Your task is to tell Baby Volcano the weight sum of all edges on the minimal spanning tree, so that he could verify his answer.

Given two positive integers, lcm(i,j) is defined as the minimal positive integer k satisfying both i and j are factors of k.

Input

The first line contains a single integer t(1≤t≤50), the number of testcases.

For each testcase, the first line contains two integers n,K(1≤n≤1010,108≤K≤109).

The input guarantees that K is a prime number.

The input guarantees that there are no more than 5 testcases with n>109.

Output

For each testcase, output a single line with a single integer, the answer module K.

Sample Input

3 3 998244353 100 998244353 1000000000 998244353

Sample Output

10 6307 192026508

Source

2020中国大学生程序设计竞赛(CCPC) - 网络选拔赛

调了3个半小时 还是凉了。。。不能说是板子问题,主要是我套前缀和公式前没对 n 取模,改了几次心态就炸了。

都是我的锅qaq(过了这题好像也莫得名额 博弈也没想出来

下面用了两个板子。

首先是来自知乎的:https://www.zhihu.com/question/29580448/answer/882461056

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1000010;

typedef long long ll;

ll mod;
ll qpow(ll a, ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b & 1)
            ans = ans * a % mod;
        a = a * a % mod;
        b /= 2;
    }
    return ans % mod;
}

ll prime[N], id1[N], id2[N], flag[N], ncnt, m;

ll g[N], sum[N], a[N], T, n;

inline ll ID(ll x)
{
    return x <= T ? id1[x] : id2[n / x];
}

inline ll calc(ll x)
{
    return x * (x + 1) / 2 - 1;
}

inline ll f(ll x)
{
    return x;
}

inline void init()
{
    ncnt = m = 0;
    T = sqrt(n + 0.5);
    for (ll i = 2; i <= T; i++)
    {
        if (!flag[i])
            prime[++ncnt] = i, sum[ncnt] = sum[ncnt - 1] + i;
        for (ll j = 1; j <= ncnt && i * prime[j] <= T; j++)
        {
            flag[i * prime[j]] = 1;
            if (i % prime[j] == 0)
                break;
        }
    }
    for (ll l = 1; l <= n; l = n / (n / l) + 1)
    {
        a[++m] = n / l;
        if (a[m] <= T)
            id1[a[m]] = m;
        else
            id2[n / a[m]] = m;
        g[m] = calc(a[m]);
    }
    for (ll i = 1; i <= ncnt; i++)
        for (ll j = 1; j <= m && (ll)prime[i] * prime[i] <= a[j]; j++)
            g[j] = g[j] - (ll)prime[i] * (g[ID(a[j] / prime[i])] - sum[i - 1]);
}

inline ll solve(ll x)
{
    if (x <= 1)
        return x;
    return n = x, init(), g[ID(n)];
}

int main()
{
    ll n, t;
    scanf("%lld", &t);
    while(t--) {
        scanf("%lld%lld", &n, &mod);
        ll ans = (solve(n + 1) - 2 + mod) % mod;
        ll inv2 = qpow(2, mod - 2) % mod;
        ll tmp = ((n + 4) % mod * (n - 1) % mod) % mod * inv2 % mod;
        printf("%lld\n", (ans + tmp) % mod);
    }
}

 下面来自https://www.cnblogs.com/purinliang/p/13703156.html

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll 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_cnt(ll n) {
    const ll 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];
}

ll qpow(ll a, ll b) {
    ll ans = 1;
    while(b) {
        if(b & 1)
            ans = ans * a % mod;
        a = a * a % mod;
        b /= 2;
    }
    return ans % mod;
}

int main() {
    ll n, t;
    scanf("%lld", &t);
    while(t--) {
        scanf("%lld%lld", &n, &mod);
        ll ans = prime_cnt(n + 1) - 2;
        ll inv = qpow(2, mod - 2) % mod;
        ll tmp = ((n + 4) % mod * (n - 1) % mod) * inv % mod;
        printf("%lld\n", (ans + tmp) % mod);
    }
    return 0;
}

 

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
正在热议
更多
# 听劝,这个简历怎么改 #
14099次浏览 183人参与
# 面试被问“你的缺点是什么?”怎么答 #
6405次浏览 99人参与
# 水滴春招 #
16485次浏览 349人参与
# 入职第四天,心情怎么样 #
11321次浏览 63人参与
# 租房找室友 #
8027次浏览 53人参与
# 读研or工作,哪个性价比更高? #
26163次浏览 356人参与
# 职场新人生存指南 #
199236次浏览 5510人参与
# 参加完秋招的机械人,还参加春招吗? #
27000次浏览 276人参与
# 文科生还参加今年的春招吗 #
4114次浏览 31人参与
# 简历无回复,你会继续海投还是优化再投? #
48629次浏览 561人参与
# 你见过最离谱的招聘要求是什么? #
144719次浏览 829人参与
# 如果重来一次你还会读研吗 #
155719次浏览 1706人参与
# 机械人选offer,最看重什么? #
69077次浏览 449人参与
# 选择和努力,哪个更重要? #
44310次浏览 493人参与
# 如果再来一次,你还会学硬件吗 #
103647次浏览 1245人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
20521次浏览 414人参与
# 招聘要求与实际实习内容不符怎么办 #
46753次浏览 494人参与
# 22届毕业,是读研还是拿外包offer先苟着 #
4652次浏览 27人参与
# 你们的毕业论文什么进度了 #
901291次浏览 8961人参与
# 软开人,你觉得应届生多少薪资才算合理? #
81379次浏览 496人参与
# 国企还是互联网,你怎么选? #
109198次浏览 853人参与
牛客网
牛客企业服务