西南交通大学2019ACM集训队选拔训练赛 G题

Tree Dance
描述

The Monkey King is a hero in Dota2.

Naughty monkeys always make fun of his enemies.he jumped from one tree to another repeatedly so that the enemy can’t know his position.

The forest can be seen as a straight line with some nodes(represent trees) numbered from -n to n without 1,0,-1.

For example,a forest with n=4 can be shown below:

-4 -3 -2 2 3 4

the Monkey King can only jump from aa to bb,if and only if there exists an integer x such that 1 < |x| and (ax=b or bx=a). where |x| is absolute value of x. then his score increase |x|.

Monkey hate repeated routes,So you can only jump each route once.(aa to bb and bb to aa is seen as the same route).

You should get the maximum score he can get and he can start at any node.

输入

First line contains only one integer T(1≤T≤20) — Number of test cases.

The following T lines each contains a single integer n (2≤n≤10000) — the parameter of the forest.

输出

For each n,you should output the answer representing the maximum score.

输入样例 1
3
4
6
2
输出样例 1
8
28
0
提示

In the first example you can jump from 2->4->(-2)->(-4)->2.

时间限制 1000MS
内存限制 256MB

题解
理解题意知道猴子跳的两个木桩的绝对值是成倍数关系,也就是说要找题意2~n(正数)中所有成倍数关系的两个数,比如2和4的话,存在四种情况:2—>4,2—>-4,-2—>4,-2—>-4,因此我们只需在正数中寻找,再将找到的次数乘以4就可以了。
在找成倍数的两个数时,要尽量缩小运算时间,按照这样的顺序:先找2的所有倍数2—4—6…,再找3的所有倍数3—6—9…,以此类推,用循环嵌套的方式完成。

代码

#include
int main()
{
int T,n,i,j,s;
scanf("%d",&T);
while(T--)
{s=0;scanf("%d",&n);
for(i=2;i<=n/2;i++)
for(j=i+i;j<=n;j=j+i)
s+=j/i;
printf("%d\n",s*4);}}

你可能感兴趣的:(西南交通大学2019ACM集训队选拔训练赛 G题)