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≤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
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.
题目分析
题目大意:建图,求图最小边权和:边权为两点序号的 L C M LCM LCM。
我们很容易想到:使每条边权最小的时候,应该将所有的点与1点相连,使得所有边权最小化。
因此图的结构被确定了,每条边的边权也唯一固定: 2 , 3 , 4 … n 2,3,4…n 2,3,4…n。可以推出以下公式: n ∗ ( n + 1 ) / 2 − 1 n * (n + 1) / 2 - 1 n∗(n+1)/2−1
AC Code#include
#define ll long long
using namespace std;
int main(){
int t = 0, cas = 0; cin >> t;
while(t--){
ll ans = 0, n = 0; cin >> n;
ans = (n + 2) * (n - 1) / 2;
cout
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?