2020 China College Student Programming Competition (CCPC)-Network Trial 1002 Graph Theory Class

HDUOJ 6889 Graph Theory Class

Topic link

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>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

Number thesis ~
Find the rules, what my teammates found is this:
It is easy to find a few small ones on the table
n = 5 n=5n=5
years = 2 ∗ (2 + 3 + 5) + (6) = 26 years = 2 * (2 + 3 + 5) + (6) = 26a n s=2(2+3+5)+(6)=26
n = 6 n=6 n=6
years = 2 ∗ (2 + 3 + 5 + 7) + (6) = 40 years = 2 * (2 + 3 + 5 + 7) + (6) = 40a n s=2(2+3+5+7)+(6)=40
⋯ \cdots
n = 9 n=9 n=9
years = 2 ∗ (2 + 3 + 5 + 7) + (6 + 8 + 9 + 10) = 67 years = 2 * (2 + 3 + 5 + 7) + (6 + 8 + 9 + 10) = 67a n s=2(2+3+5+7)+(6+8+9+10)=6 7
It is not difficult to find: the
answer isnnSum of prime numbers within n times2 22 plusnnExcept4 4within nFor composite numbers other than 4 , a magical algorithmM in 25 Min25must be used hereM i n 2 5 , you can quickly calculatennThe sum of prime numbers within n , the rest is fine, and the composite sum is1 − n 1-n1The sum of n and the sum of prime numbers can be subtracted. The AC code is as follows:

#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;
}

Guess you like

Origin blog.csdn.net/qq_43765333/article/details/108696461