HDU 6134 Battlestation Operational-莫比乌斯

来源:互联网 发布:比利牛斯山的城堡 知乎 编辑:程序博客网 时间:2024/06/11 15:23

 

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 ati-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 ifgcd(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 2310
 

Sample Output
138110



/*题意 : 要你计算题目中的公式,那个公式简单点来说就是给出一个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      15/2 = 3     4/2 = 2      15/3 = 2     4/3 = 1      15/4 = 2     4/4 = 1      15/5 = 1           0      12)f[i] = F[i] - i +(i的因子数)对于当前得F[i]来说 每一项都是向上取整得 要求他向下取整得数,那么就要减去那些不能整除i的数,那些数是通过向上取整取得的,所以要减去,直接找那些数不好找 但是找i的因子容易,那么就直接先把F[i]减去i,然后再加上i的因子个数例如4/1 = 4   4/1 = 4   04/2 = 2   4/2 = 2   04/3 = 1   4/3 = 2   14/4 = 1   4/4 = 1   0    f[4] = F[4] - 4 + 33)这样就算完了F[i],但是里面还有要除去的,就是那些不互质的数也被计算到里面去了,要刨去这一部分比如2,它只有在 i = 2 的时候 2 / 1 可以计数加2,在 i = 4 的时候 i / 2 = 2 计数也加2了,但是4与2是不互质的所以要刨去,因为这里的容斥条件是互质关系,所以这部分的容斥可以用到莫比乌斯容斥。例如:F[12]12/1  = 12          6/1 = 612/2  = 6 *         6/2 = 312/3  = 4           6/3 = 212/4  = 3 *         6/4 = 212/5  = 3           6/5 = 212/6  = 2 *         6/6 = 112/7  = 212/8  = 2 *12/9  = 212/10 = 2 *12/11 = 212/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;}


阅读全文
0 0
原创粉丝点击