您的位置:首页 > 其它

URAL 2025 Line Fighting 水题、贪心、均分

2016-07-30 23:20 676 查看
B - Line Fighting
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice URAL
2025

Description

Boxing, karate, sambo… The audience is sick of classic combat sports. That is why a popular sports channel launches a new competition format based on the traditional Russian entertainment called line fighting. There can be from
2 to k teams taking part in a competition,
and there are n fighters altogether in all the teams. Before the competition starts, the fighters are divided into teams: each fighter becomes a member of exactly one team. Two fighters fight each other if they are members
of different teams. The organizers believe
that the more the number of fights between fighters, the higher the popularity of a competition will be. Help the organizers to
distribute fighters between teams so as to maximize the number of fights and output this number.

Input

The first line contains the number of tests T (1 ≤ T ≤ 10). In each of the following T lines you are given a test: integers n and k
separated with a space (2 ≤ k ≤ n ≤ 10 4).

Output

For each test output the answer (one integer) in a separate line.

Sample Input

inputoutput
3
6 3
5 5
4 2

12
10
4

Source

UESTC 2016 Summer Training #17 Div.2

URAL 2025

My Solutiion

贪心

尽可能均摊 t = n/k; res = n - t*k; 然后res个 t+1, n - res 个t, 然后算下就好了

复杂度 O(n)

#include <iostream>
#include <cstdio>

using namespace std;
typedef long long LL;
const int maxn = 1e4 + 8;
/*
const LL Hash = 1e15 + 7;
inline LL mod(LL a)
{
return a - (a/Hash)*Hash;
}

LL pow_mod(LL a, LL i)
{
if(i == 0) return mod(1);
LL t = pow_mod(a, i>>1);
t = mod(t*t);
if(i & 1) t = mod(t*a);
return t;
}
*/

int val[maxn];

int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
//freopen("b.txt", "w", stdout);
#endif // LOCAL
LL T, n, k, t, ans, res;
scanf("%I64d", &T);
while(T--){
ans = 0;
scanf("%I64d%I64d", &n, &k);
t = n/k;
res = n - t*k;
t++;
for(int i = 1; i <= res; i++){
ans += t*(n - i*t);
}

n -= res*t;
t--;
for(int i = res+1; i <= k; i++){
if(i != k)ans += t*(n - (i - res)*t);
}
printf("%I64d\n", ans);

}
return 0;
}


Thank you!

------from ProLights
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: