【HDU - 5922】Minimum’s Revenge(思维,最小生成树变形)
題干:
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≤100T≤100), which indicates the number of test cases.
For each test case, the first line contains only one integer n (2≤n≤1092≤n≤109), 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 3Sample Output
Case #1: 2 Case #2: 5Hint
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個點的圖,編號1~n,每兩個點u,v之間都有連邊,邊權=lcm(u,v),求最小生成樹的權值和。
解題報告:
一眼掃過去是,所有點都和1號點連邊。簡單證明一下:別對lcm進行公式的化簡,既然每個點的編號都要至少被考慮一次(連邊一條),考慮每個點的編號x對答案的影響,這個點不論和誰連邊,那LCM肯定>=x,且另一個點取x的因子時可以取等,1是所有數的因子,所以和1連邊,所以每個點都可以取等,所以都和1連邊肯定是最小的。等差公式求個和就行了。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int a[MAX],b[MAX],n; int main() {int T,iCase=0;cin>>T;while(T--) {cin>>n;printf("Case #%d: %lld\n",++iCase,1LL*n*(n+1)/2-1);}return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 5922】Minimum’s Revenge(思维,最小生成树变形)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【计蒜客 - 2019南昌邀请赛网络赛
- 下一篇: 【蓝桥杯官网试题 - 历届试题】发现环(