HDU 5922 Minimum’s Revenge(思维题)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛
2016-10-11 19:57
288 查看
此文章可以使用目录功能哟↑(点击上方[+])
Accept: 0 Submit: 0
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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?
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.
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.
2
2
3
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.
解题思路:
【题意】
现有一个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必定满足
![](https://img-blog.csdn.net/20161011194913784)
当且仅当取到等号时,生成树为最小生成树
当然,这种生成树也是我们轻而易举能够构造出来的,如下:
![](https://img-blog.csdn.net/20161011195538958)
【时间复杂度&&优化】
O(1)
题目链接→HDU 5922 Minimum’s Revenge
/*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;
}菜鸟成长记
![](http://acm.fzu.edu.cn/image/problem.gif)
HDU 5922 Minimum’s Revenge
Accept: 0 Submit: 0Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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?
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Input
22
3
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Output
Case #1: 2Case #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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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;
}菜鸟成长记
相关文章推荐
- 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 A - Minimum’s Revenge HDU 5922
- (HDU 5922)Minimum’s Revenge 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU 5926 Mr. Frog’s Game(连连看,暴力)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- hdu5922 2016CCPC东北地区大学生程序设计竞赛 A题 Minimum’s Revenge(思维,水题)
- (HDU 5927)Auxiliary Set 思维题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5922 Minimum’s Revenge 【模拟】(2016CCPC东北地区大学生程序设计竞赛)
- HDU 5929 Basic Data Structure(模拟,deque)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- (HDU 5924)Mr. Frog’s Problem 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- 【HDU5922 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 A】【水题】Minimum’s Revenge
- HDU 5924 Mr. Frog’s Problem(想看证明的来)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- (HDU 5926)Mr. Frog’s Game 水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- 2016CCPC东北地区大学生程序设计竞赛-Minimum’s Revenge
- HDU Mr. Frog’s Game 2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- hdu 5927 Auxiliary Set 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F题(树状dp)
- (HDU 5929)Basic Data Structure 双端队列+模拟 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟
- 2016CCPC东北地区大学生程序设计竞赛 Auxiliary Set (BFSt预处理+ 思维)
- hdu 5924 Mr. Frog’s Problem(2016CCPC东北地区大学生程序设计竞赛 C题,数学思维)