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

108 篇文章 0 订阅

题目链接

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≤1e10,1e8≤K≤1e9).

The input guarantees that K is a prime number.

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

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

数论题~
找规律,我队友找到的是这样的:
打表几个小的很容易发现
n = 5 n=5 n=5
a n s = 2 ∗ ( 2 + 3 + 5 ) + ( 6 ) = 26 ans=2*(2+3+5)+(6)=26 ans=2(2+3+5)+(6)=26
n = 6 n=6 n=6
a n s = 2 ∗ ( 2 + 3 + 5 + 7 ) + ( 6 ) = 40 ans=2*(2+3+5+7)+(6)=40 ans=2(2+3+5+7)+(6)=40
⋯ \cdots
n = 9 n=9 n=9
a n s = 2 ∗ ( 2 + 3 + 5 + 7 ) + ( 6 + 8 + 9 + 10 ) = 67 ans=2*(2+3+5+7)+(6+8+9+10)=67 ans=2(2+3+5+7)+(6+8+9+10)=67
不难发现:
答案就是 n + 1 n+1 n+1 以内的素数和乘 2 2 2 加上 n + 1 n+1 n+1 以内的除了 4 4 4 以外的合数和,这里就必须要用到一种神奇的算法 M i n 25 Min25 Min25,可以快速计算出 n + 1 n+1 n+1 以内的素数和,剩下的就很好算了,合数和就是 [ 2 , n + 1 ] [2,n+1] [2,n+1] 求和再减去素数和即可,AC代码如下:

#include <bits/stdc++.h>

using namespace std;
const int N = 1000010;
typedef long long ll;
ll mod, n, t, nn;

ll power(ll a, ll b) { return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1; }

namespace Min25 {
    int prime[N], id1[N], id2[N], flag[N], ncnt, m;
    ll g[N], sum[N], a[N], T, n;

    inline int 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() {
        T = sqrt(n + 0.5);
        for (int i = 2; i <= T; i++) {
            if (!flag[i]) prime[++ncnt] = i, sum[ncnt] = sum[ncnt - 1] + i;
            for (int 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 (int i = 1; i <= ncnt; i++)
            for (int 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)];
    }
}

void Clear() {
    memset(Min25::prime, 0, sizeof(Min25::prime));
    memset(Min25::id1, 0, sizeof(Min25::id1));
    memset(Min25::id2, 0, sizeof(Min25::id2));
    memset(Min25::flag, 0, sizeof(Min25::flag));
    memset(Min25::g, 0, sizeof(Min25::g));
    memset(Min25::sum, 0, sizeof(Min25::sum));
    memset(Min25::a, 0, sizeof(Min25::a));
    Min25::T = 0;
    Min25::n = 0;
    Min25::ncnt = 0;
    Min25::m = 0;
}

void run() {
    scanf("%lld", &t);
    while (t--) {
        Clear();
        scanf("%lld%lld", &nn, &mod);
        ll u = (Min25::solve(nn + 1)) % mod;
        ll sum = (nn + 3ll) % mod * nn % mod * power(2, mod - 2) % mod;
        ll ans = (2 * u % mod + (sum - u - 4ll)) % mod;
        if (ans < 0) ans = (mod - (-ans) % mod) % mod;
        printf("%lld\n", ans % mod);
    }
}

int main() {
    run();
    return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值