此文章可以使用目录功能哟↑(点击上方[+])
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
3
Sample Output
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;
}
菜鸟成长记