【HDU4944】預處理(類似素數篩選)



FSF’s game

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 443    Accepted Submission(s): 211


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
Hint
In 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
 

Author
UESTC
 

Source



題解轉自:http://blog.csdn.net/keshuai19940722/article/details/38519681#comments


題目大意:給定N,可以用不大於N的長a和寬b,組成N(N1)2種不同的矩形,對於每個矩形ab要計算它的值,K爲矩形a,b可以拆分成若干個KK的正方形。abgcd(a/k,b/k),輸出所有矩形值的和。

解題思路:假設有邊a和b,那麼k肯定即使a的因子也是b的因子。定義f(n)爲矩形最長邊等於n的情況下所有矩形值的和。那麼f(n)=val(1n)+val(2n)++val(nn),枚舉n的因子作爲k,現在假設有因子k,使得n=ka:
g(k)=1knk+2knk++aknk

=1n+2n++an

=(1+a)a2n

f(n)=g(k)(k爲n的因子)
然後用類似素數篩選法的方式處理處f(i)的值,對應再累加即使答案。

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
const long long MOD = 1LL << 32;
const int maxn = 500000;
unsigned long long f[maxn + 5], s[maxn + 5];
void init()
{
    memset(f, 0, sizeof(f));
    s[0] = f[1] = 0;
    for (int i = 1; i <= maxn; i++)
    {
        for (int k = i, d = 1; k <= maxn; k += i, d++)
        {
            f[k] += (1LL + d) * d / 2;
            f[k] %= MOD;
        }
        f[i] = f[i] * i % MOD;
    }
    for (long long i = 1; i <= maxn; i++)
        s[i] = (s[i - 1] + f[i]) % MOD;
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    init();
    int cas, n;
    scanf("%d", &cas);
    for (int kcas = 1; kcas <= cas; kcas++)
    {
        scanf("%d", &n);
        printf("Case #%d: %I64u\n", kcas, s[n]);
    }

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章