hdu FSF’s game 欧拉定理,素数筛法

FSF’s game

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


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


题意:给一个n可以,有不同的组合(1x1 , 2x1, 2x2, 3x1, ..., Nx(N-1), NxN) ,

对于每个组合,计算A*B/ gcd(A/K,B/K),求所有结果的和。其中k表示A,B的公共因子

解法: A*B/ gcd(A/K,B/K) ===》 A*B/(gcd(A,B)/k) ==》A/gcd*B/gcd*gcd*k

A/gcd 与B/gcd互质, 对于任意NXM 对于N的一个因子gcd,N/gcd,M/gcd互质,且M小于N,因此只要把与N互质的数加起来就可以了

然后k是gcd的因子,计算gcd的因子之和就可以了

计算一个小于N与N互质的数的个数用欧拉定理就可以了,又因为如果i与n互质,那么n-i与n互质

所以 ph(n)*N/2就是小于n且与n互质的数之和


#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define maxn 500007
#define maxm 7000007
#define uint unsigned int
vector<uint>hehe[maxn];
uint copr[maxn],factor[maxn];
uint next[maxm],val[maxm];
uint cnt = 1,head[maxn];
inline void add(uint u,uint p){
    val[cnt]=p;
    next[cnt]=head[u];
    head[u]=cnt++;
    factor[u]+=p;
}
bool check[maxn];
uint ans[maxn];
void init(){
    cnt=1;
    memset(head,0,sizeof(head));
    memset(factor,0,sizeof(factor));
    memset(check,0,sizeof(check));
    uint n = 500000,i,j;
    check[1] = 1;
    for( i = 1;i <= n;i++){
        for( j=i;j<=n;j+=i) add(j,i);
        if(check[i])continue;
        for( j=i;j<=n;j+=i)hehe[j].push_back(i),check[j]=1;
    }
    memset(copr,0,sizeof(copr));
    unsigned long long  u;
    copr[1]=1;
    for( i = 2;i <= n; i++){
        u = i;
        for( j=0;j<hehe[i].size();j++)
            u=u*(hehe[i][j]-1)/hehe[i][j];
        u=u*i/2;
        copr[i] = (uint)u;
    }
    memset(ans,0,sizeof(ans));
    for( i = 1;i <= n; i++){
        ans[i] = ans[i-1];
        int l = 0;
        for( j = head[i];j!=0;j=next[j]){
            ans[i] = ans[i] + i*copr[i/val[j]]*factor[val[j]];
            l++;
        }
    }
}

int main(){
    init();
    int t,n;
    scanf("%d",&t);
    for(int i = 1;i <= t; i++){
        scanf("%d",&n);
        printf("Case #%d: %u\n",i,ans[n]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值