HDU[6889] Graph Theory Class 【质数和】

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≤10^10,10^8≤K≤10^9).

The input guarantees that K is a prime number.

The input guarantees that there are no more than 5 testcases with n>10^9. 

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个点的完全图,点 i 和 j 之间的边权为 lcm(i+1, j+1),求最小生成树的边权和。

分析:

首先,其实可以把点的编号转化为 2~n+1,边权为 lcm(i,j) 。

先考虑任意两个数的 lcm(i,j),若 j 为质数,则肯定和 2 组合最小 lcm = 2*j ,若 j 为合数,则与 j 的任一个因数组合都最小 lcm = j 。

按上述思路,编号为 3~n+1 的点都可以找到一个点相连。

接着考虑上述思路是否能构成一个树。

可以证明,若每个点都和编号比其小的唯一一点相连,必可以形成一棵树。

所以上述思路可行。

那么最小生成树的边权和即为:2*(3~n+1的所有质数和) + (3~n+1的所有合数和) 。

所以只需要求出 3~n+1 的所有质数和,再加上 3~n+1 所有数的和即可。

具体解释见代码。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <assert.h>
#define INF 0x3f3f3f3f
#define mst(a,num) memset(a,num,sizeof a)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
//const ll mod = 1e9 + 7;
const int maxn = 100000 + 5;
const int N = 1000010;

ll n,mod;

ll qpow(ll num,ll p){
    ll res=1;
    while(p){
        if(p&1)  res=(res*num)%mod;
        num=(num*num)%mod;
        p>>=1;
    }
    return res%mod;
}

inline ll V2IDX(ll v, ll N, ll Ndr, ll nv) {
    return v >= Ndr ? (N/v - 1) : (nv - v);
}

ll primesum(ll N) {     //求取1~N的所有质数和
    ll *S;
    ll *V;

    ll r = (ll)sqrt(N);
    ll Ndr = N/r;

    assert(r*r <= N and (r+1)*(r+1) > N);

    ll nv = r + Ndr - 1;

    V = new ll[nv];
    S = new ll[nv];

    for (ll i=0; i<r; i++) {
        V[i] = N/(i+1);
    }
    for (ll i=r; i<nv; i++) {
        V[i] = V[i-1] - 1;
    }

    for (ll i=0; i<nv; i++) {
        S[i] = V[i] * (V[i] + 1) / 2 - 1;
    }

    for (ll p=2; p<=r; p++) {
        if (S[nv-p] > S[nv-p+1]) {
            ll sp = S[nv-p+1];
            ll p2 = p*p;
            for (ll i=0; i<nv; i++) {
                if (V[i] >= p2) {
                    S[i] -= p * (S[V2IDX(V[i]/p, N, Ndr, nv)] - sp);
                } else {
                    break;
                }
            }
        }
    }

    return S[0];
}

int main() {
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld",&n,&mod);
        if(n==1){       //n=1特判
            printf("0\n");
            continue;
        }
        ll sum=primesum(n+1)%mod;
        sum=(sum-2+mod)%mod;
        ll nsum=((n+2)%mod)*((n+1)%mod)%mod*qpow(2,mod-2)%mod;
        nsum=(nsum-3+mod)%mod;
        ll ans=(sum+nsum)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值