您的位置:首页 > 其它

hdu 4944 FSF’s game

2014-08-13 10:14 417 查看


FSF’s game

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 294    Accepted Submission(s): 134


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

3
1
3
100

 

Sample Output

Case #1: 1
Case #2: 30
Case #3: 15662489

HintIn the second test case, there are six levels(1x1,1x2,1x3,2x2,2x3,3x3)
Here is the details for this game:
1x1: 1(K=1); 1x2: 2(K=1); 1x3: 3(K=1); 2x2: 2(K=1), 4(K=2); 2x3: 6(K=1); 3x3: 3(K=1), 9(K=3);
1+2+3+2+4+6+3+9=30

 

题意:

给定一个整数n,求∑fun(i,j)(1<=i<=j<=n)。其中fun(i,j)=∑i*j/gcd(i/k,j/k)(k为i和j的公因子)。

题解:

主要预处理,然后O(1)的复杂度打出结果。

容易知道dp方程为dp
=dp[n-1]+∑fun(i,n)(1<=i<=n),dp[1]=1。所以我们只要求出所有的∑fun(i,n)(1<=i<=n),就能求得多有的dp
。fun(i,n)=n*(i/c1+i/c2+...),其中cj为n和i的最大公约数d的所有因子。那么我们可以枚举所有n的因子j,对于因子j,存在这个因子且小于等于n的数字中有
j, 2*j, 3*j  ,...,n。那么我们对此求和我们就可以得到  s
=(1+2+3+...n/j)*n = (n/j+1)*(n/j)/2*n。那么val
=∑fun(i,n)(1<=i<=n)  = ∑ s[j](j为n的因子)。

之后用dp
=dp[n-1]+val
递推公式就能求得结果了。

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64

const int N = 500005;
LL mod=1;
LL dp
,val
;

void fun(int n) {
for(LL i=1; i<n; i++) {
for(LL j=i; j<n; j+=i) {
val[j]+=(j/i+1)*(j/i)/2;
}
}
}
void init() {
mod=mod<<32;
memset(val,0,sizeof(val));
fun(N);
dp[1]=1;
for(int i=2; i<N; i++) {
dp[i]=dp[i-1]+val[i]*i;
dp[i]%=mod;
}
}

int main() {
int T;
int n;
init();
cin>>T;
for(int cas=1; cas<=T; cas++) {
scanf("%d",&n);
printf("Case #%d: %I64d\n",cas,dp
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu 4944 FSFs game