HDU 5922 Minimum’s Revenge(思维题)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛

Posted queuelovestack

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5922 Minimum’s Revenge(思维题)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛相关的知识,希望对你有一定的参考价值。

此文章可以使用目录功能哟↑(点击上方[+])

 HDU 5922 Minimum’s Revenge

Accept: 0    Submit: 0
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

 Problem Description

There is a graph of n vertices which are indexed from 1 to n. For any pair of different vertices, the weight of the edge between them is the least common multiple of their indexes.

Mr. Frog is wondering about the total weight of the minimum spanning tree. Can you help him?

 Input

The first line contains only one integer T (T≤100), which indicates the number of test cases.

For each test case, the first line contains only one integer n (2≤n≤10^9), indicating the number of vertices.

 Output

For each test case, output one line "Case #x:y",where x is the case number (starting from 1) and y is the total weight of the minimum spanning tree.

 Sample Input

2
2
3

 Sample Output

Case #1: 2
Case #2: 5
Hint
In the second sample, the graph contains 3 edges which are (1, 2, 2), (1, 3, 3) and (2, 3, 6).
Thus the answer is 5.

 Problem Idea

解题思路:

【题意】
现有一个n个结点的一个图

这n个结点的指数分别为1~n

现在规定一条边的权值为该边两个端点的指数的最小公倍数

问这n个点构成的最小生成树的总权值

【类型】
最小生成树+最小公倍数=思维
【分析】
考虑到n个结点的最小生成树有n-1条边

这意味着我们要挑选出n-1个最小公倍数

再考虑到两个正整数a和b,它们的最小公倍数c必定满足c≥max(a,b)

即LCM(a,b)≥max(a,b)

而我们知道最小生成树会包含1~n这n个结点

那么生成树的总权值ans必定满足


当且仅当取到等号时,生成树为最小生成树

当然,这种生成树也是我们轻而易举能够构造出来的,如下:


【时间复杂度&&优化】
O(1)

题目链接→HDU 5922 Minimum’s Revenge

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 100005;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
int main()

    int t,p=1;
    __int64 n;
    scanf("%d",&t);
    while(t--)
    
        scanf("%I64d",&n);
        printf("Case #%d: %I64d\\n",p++,(n+2)*(n-1)/2);
    
    return 0;
菜鸟成长记

以上是关于HDU 5922 Minimum’s Revenge(思维题)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1394 Minimum Inversion Number

hdu 1394 Minimum Inversion Number

[hdu1394]Minimum Inversion Number(树状数组)

hdu1394 Minimum Inversion Number(最小逆序数)

hdu1394Minimum Inversion Number(线段树,求最小逆序数)

HDU 1394Minimum Inversion Number 数状数组 逆序对数量和