FSF’s gameTime Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 811 Accepted Submission(s): 414
Problem Description
FSF has programmed a game.
In this game, players need to divide a rectangle into several same squares. The length and width of rectangles are integer, and of course the side length of squares are integer. After division, players can get some coins. If players successfully divide a AxB rectangle(length: A, width: B) into KxK squares(side length: K), they can get A*B/ gcd(A/K,B/K) gold coins. In a level, you can’t get coins twice with same method. (For example, You can get 6 coins from 2x2(A=2,B=2) rectangle. When K=1, A*B/gcd(A/K,B/K)=2; When K=2, A*B/gcd(A/K,B/K)=4; 2+4=6; ) There are N*(N+1)/2 levels in this game, and every level is an unique rectangle. (1x1 , 2x1, 2x2, 3x1, ..., Nx(N-1), NxN) FSF has played this game for a long time, and he finally gets all the coins in the game. Unfortunately ,he uses an UNSIGNED 32-BIT INTEGER variable to count the number of coins. This variable may overflow. We want to know what the variable will be. (In other words, the number of coins mod 2^32)
Input
There are multiply test cases.
The first line contains an integer T(T<=500000), the number of test cases Each of the next T lines contain an integer N(N<=500000).
Output
Output a single line for each test case.
For each test case, you should output "Case #C: ". first, where C indicates the case number and counts from 1. Then output the answer, the value of that UNSIGNED 32-BIT INTEGER variable.
Sample Input
Sample Output
|
sigma(A*B / gcd(A/K,B/K),K属于A和B公因子)为二元组(A, B)的价值。
题意:现在给你一个N,问你(1x1 , 2x1, 2x2, 3x1, ..., Nx(N-1), NxN)二元组的价值和。
思路:我们设Ans[n]为(1x1 , 2x1, 2x2, 3x1, ..., nx(n-1), nxn)二元组价值和。
定义Psum[n] = (1x1 , 1x2, 1x3, 1x4, ..., (n-1)xn, nxn)二元组价值和
可以得到Ans[n] = Ans[n-1] + Psum[n]。因此我们只需要预处理所有Psum[]就可以解决这个问题。
比较渣,打个表找到规律。。。
Psum[i] = (i / j + 1) * (i / j) / 2,其中j是i的所有因子。 可以考虑筛素法。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 500000+1
#define LL long long
using namespace std;
LL Ans[MAXN], Psum[MAXN];
void init_Ans()
{
memset(Psum, 0, sizeof(Psum));
for(LL i = 1; i < MAXN; i++)
for(LL j = i; j < MAXN; j += i)
Psum[j] += (j / i + 1) * (j / i) / 2, Psum[j] %= (1LL<<32);
Ans[0] = 0;
for(LL i = 1; i < MAXN; i++)
Ans[i] = (Ans[i-1] + Psum[i] * i) % (1LL<<32);
}
int main()
{
init_Ans();
int t, k = 1;
scanf("%d", &t);
while(t--)
{
int n; scanf("%d", &n);
printf("Case #%d: %lld\n", k++, Ans[n]);
}
return 0;
}