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

HDUOJ 6889 Graph Theory Class

题目链接

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

数论题~
找规律,我队友找到的是这样的:
打表几个小的很容易发现
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 n n 以内的素数和乘 2 2 2 加上 n n n 以内的除了 4 4 4 以外的合数和,这里就必须要用到一种神奇的算法 M i n 25 Min25 Min25,可以快速计算出 n n n 以内的素数和,剩下的就很好算了,合数和就是 1 − n 1-n 1n 求和再减去素数和即可,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;
}

猜你喜欢

转载自blog.csdn.net/qq_43765333/article/details/108696461