标签:http io for ar 代码 amp type ef
题目大意:给定N,然后会有N * ( N + 1)/2个等级的矩形,(1 * 1, 2 * 1, 2* 2, ...N * 1, N * 2.. N* N).将这些矩阵分成大小相同的K * K 的正方形,能够获得金币A * B / gcd(A
/ K, B/ K);然后给定N,问能够得到的总金币。
解题思路:对于sum(N):N * (N + 1) / 2个等级的矩形的金币总和 = sum(N - 1) + f(N):(N * 1, N * 2,...N * N)这一些矩形的金币之和)。
例如N = 8: 1* 8/ gcd(8/1, 1/1) 2* 8 / gcd(8/1,2/1) 2 * 8/ gcd(8/2, 2/2) ....8 * 8 / gcd(8/1,8/2) 8 * 8/ gcd(8/2.8/2), 8 * 8/ gcd(8/4, 8/4), 8 * 8 / gcd(8/8, 8/8)
1* 8 2* 8/2 @ 2*8 3* 8 4 * 8 4 * 8/2@ 4 * 8/4 5 * 8 6 * 8 6*8 /2 @ 7 * 8 8 * 8 @8 * 8/2 8 *8/4 8*8 /8
可以发现gcd(A /K, B/K)是N的因子。另a = gcd, N = n*a.
对于gcd = a
可获得的金币G(a): 1*a * N/ a + 2 * a * N / a + 3 *a * N/a +...+ na * N /a = (1 +2 + 3 + n) * N
那么枚举N的因子n,加起来就是f(N) = sum1..n(G(ai)).
代码:
#include <cstdio> #include <cstring> typedef unsigned __int64 ll; const int maxn = 500005; const ll M = 1LL<<32;//int1会溢出 ll f1[maxn]; ll f2[maxn]; void init () { for (int i = 1; i < maxn; i++) { for (int j = 1; j * i < maxn; j++) f1[i * j] = (f1[i * j] + (1LL + i) * i / 2) % M; } f2[1] = 1; for (int i = 2; i < maxn; i++) f2[i] = (f2[i - 1] + f1[i] * i) % M; } int main () { int t; int n; init (); scanf ("%d", &t); for (int i = 1; i <= t; i++) { scanf ("%d", &n); printf ("Case #%d: %I64u\n", i, f2[n]); } return 0; }
hdu4944FSF’s game(数论),布布扣,bubuko.com
标签:http io for ar 代码 amp type ef
原文地址:http://blog.csdn.net/u012997373/article/details/38532205