HDU 6134 Battlestation Operational-莫比乌斯


 

Battlestation Operational

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 653    Accepted Submission(s): 367


Problem Description
> The Death Star, known officially as the DS-1 Orbital Battle Station, also known as the Death Star I, the First Death Star, Project Stardust internally, and simply the Ultimate Weapon in early development stages, was a moon-sized, deep-space mobile battle station constructed by the Galactic Empire. Designed to fire a single planet-destroying superlaser powered by massive kyber crystals, it was the pet project of the Emperor, Darth Vader, and its eventual commander Grand Moff Wilhuff Tarkin to expound the military philosophy of the aptly named Tarkin Doctrine.
>
> — Wookieepedia

In the story of the Rogue One, the rebels risked their lives stolen the construction plan of the Death Star before it can cause catastrophic damage to the rebel base. According to the documents, the main weapon of the Death Star, the Superlaser, emits asymmetric energy in the battlefield that cause photons to annihilate and burns everything in a single shot.

You are assigned the task to estimate the damage of one shot of the Superlaser.

Assuming that the battlefield is an n×n grid. The energy field ignited by the Superlaser is asymmetric over the grid. For the cell at i -th row and j -th column, i/j units of damage will be caused. Furthermore, due to the quantum effects, the energies in a cell cancel out if gcd(i,j)1 or i<j .

The figure below illustrates the damage caused to each cell for
n=100 . A cell in black indicates that this cell will not be damaged due to the quantum effects. Otherwise, different colors denote different units of damages.

Your should calculate the total damage to the battlefield. Formally, you should compute
f(n)=i=1nj=1iij[(i,j)=1],


where [(i,j)=1] evaluates to be 1 if gcd(i,j)=1 , otherwise 0 .
 

Input
There are multiple test cases.

Each line of the input, there is an integer n ( 1n106 ), as described in the problem.

There are up to
104 test cases.
 

Output
For each test case, output one integer in one line denoting the total damage of the Superlaser, f(n) mod 109+7 .
 

Sample Input
  
  
1 2 3 10
 

Sample Output
  
  
1 3 8 110



/*
题意 : 要你计算题目中的公式,那个公式简单点来说就是给出一个n,
计算出  i 从 1 到 n,j 从1 到 i ,当 i 与 j互质的时候 答案计数加上[i/j](向上取整)

题解:
做俩个函数 F[i] (表示 i / 1 + i / 2 + ... + i / i 向上取整)
f[i] (表示 i / 1 + i / 2 + ... + i / i 向下取整)
可以发现规律是

1)F[i] = f[i - 1] + i 因为前一项向下取整 后一项的向上取整就是所对应的每个数的值都加1 所以就是加i 
例如
5/1 = 5     4/1 = 4      1
5/2 = 3     4/2 = 2      1
5/3 = 2     4/3 = 1      1
5/4 = 2     4/4 = 1      1
5/5 = 1           0      1

2)f[i] = F[i] - i +(i的因子数)对于当前得F[i]来说 每一项都是向上取整得 要求他向下取整得数,
那么就要减去那些不能整除i的数,那些数是通过向上取整取得的,所以要减去,
直接找那些数不好找 但是找i的因子容易,那么就直接先把F[i]减去i,然后再加上i的因子个数
例如
4/1 = 4   4/1 = 4   0
4/2 = 2   4/2 = 2   0
4/3 = 1   4/3 = 2   1
4/4 = 1   4/4 = 1   0    f[4] = F[4] - 4 + 3

3)这样就算完了F[i],但是里面还有要除去的,就是那些不互质的数也被计算到里面去了,要刨去这一部分
比如2,它只有在 i = 2 的时候 2 / 1 可以计数加2,在 i = 4 的时候 i / 2 = 2 计数也加2了,但是4与2是不互质的
所以要刨去,因为这里的容斥条件是互质关系,所以这部分的容斥可以用到莫比乌斯容斥。
例如:F[12]
12/1  = 12          6/1 = 6
12/2  = 6 *         6/2 = 3
12/3  = 4           6/3 = 2
12/4  = 3 *         6/4 = 2
12/5  = 3           6/5 = 2
12/6  = 2 *         6/6 = 1
12/7  = 2
12/8  = 2 *
12/9  = 2
12/10 = 2 *
12/11 = 2
12/12 = 1 *
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
const int INF = 0x3f3f3f;
const int mod  = 1e9+7;
int mub[maxn],prim[maxn];
bool visit[maxn];
int F[maxn],f[maxn];

void mobius(){
    mub[1] = 1;
    for(int i = 2;i<maxn;++i){
        if(!visit[i]){
            mub[i] = -1;
            prim[++prim[0]] = i;
        }
        for(int j = 1;j<=prim[0] && i*prim[j] < maxn;++j){
            visit[i*prim[j]] = 1;
            if(i%prim[j]==0){///i是primes[j]的整数倍时,i*primes[j]就会包含相同质因子,例如6和3=18
                mub[i*prim[j]] = 0;
                break;
            }
            else///i与prim[j]互质,利用其积性函数的特性得
                mub[i*prim[j]] = -mub[i];///mub[k]=mub[i]*mub[primes[j]] 而primes[j]是质数 mub值为-1
        }
    }
}

void init(){
    F[1] = f[1] = 1;
    for(int i = 2;i<maxn;++i){
        F[i] = f[i-1]+i;
        int x = i;
        int ans = 1;///i的因子个数
        for(int j = 1;prim[j]*prim[j]<=i;++j){
            int cnt = 1;
            while(x%prim[j]==0){
                cnt++;
                x/=prim[j];
            }
            ans *= cnt;
        }
        if(x>1) ans*= 2;/// i可能为素数 则有1和它本身两个因子
        f[i] = F[i]-i+ans;/// 向下取整的等于向上取整的刨去一层然后加上因子个数(是因子的才有贡献)
    }

    for(int i = 2;i<maxn;++i){///f[i]已经没用了
        f[i] = F[i];
    }
    for(int i = 2;i<maxn;++i){
        for(int j = i;j<maxn;j+=i){///j要能除开i
            if(mub[i]){
                F[j]+=mub[i] *f[j/i];///mub[i]可能是正数可能是负数,达到了减多了再加回来目的
            }
        }
    }
    for(int i =2;i<maxn;++i){
        F[i] += F[i-1];
        F[i] %= mod;
    }

}

int main(){
    mobius();
    init();
    int n;
    while(scanf("%d",&n)!=EOF){
        printf("%d\n",F[n]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值