您的位置:首页 > 其它

HDU 4944 FSF’s game 解题报告(递推)

2014-08-12 18:50 351 查看

FSF’s game

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

Total Submission(s): 72    Accepted Submission(s): 30


[align=left]Problem Description[/align]
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)
 

[align=left]Input[/align]
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).
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

3
1
3
100

 

[align=left]Sample Output[/align]

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

 

[align=left]Author[/align]
UESTC
 

[align=left]Source[/align]
2014 Multi-University Training Contest 7
 
    解题报告: 比赛时没有想出来。赛后看了神牛们的解体报告。给个链接:http://blog.csdn.net/a601025382s/article/details/38517329
    上篇说的很详细了。为什么比赛时没有搞出来才是问题……
    贴个代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
freopen("input.in", "r", stdin);
#endif // ACM

work();
}

/***************************************************/

const int maxn = 500000;
unsigned num[maxn + 5];
unsigned dp[maxn + 5];

void init()
{
fff(i, 1, maxn)
for(ULL j = i; j <= maxn; j += i)
num[j] += (j/i+1)*j/i/2;

fff(i, 1, maxn)
dp[i] = dp[i-1] + num[i] * (unsigned)i;
}

void work()
{
init();

int T;
scanf("%d", &T);
fff(cas, 1, T)
{
int n;
scanf("%d", &n);
printf("Case #%d: %u\n", cas, dp
);
}
}


    表要是表达式的转化很巧妙。当推理出 f(n) = Σ n * (i/c1 + i/c2 + ... + i/cm), cj为gcd(i, n)的所有因子时,我们枚举因子cj。相当于横向的式子先纵向统计了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: