Battlestation Operational
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 228 Accepted Submission(s): 118
>
> — 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
where [(i,j)=1] evaluates to be 1 if gcd(i,j)=1 , otherwise 0 .
Each line of the input, there is an integer n ( 1≤n≤106 ), as described in the problem.
There are up to 104 test cases.
1 2 3 10
1 3 8 110
题目给的T,数据组数非常大,所以是要打表把答案先算出来的。
思路:做俩个函数 F[i] (表示 i / 1 + i / 2 + ... + i / i 向上取整)和 f[i] (表示 i / 1 + i / 2 + ... + i / i 向下取整)
可以发现规律 是 F[i] = f[i - 1] + i ,因为前一项向下取整后,后一项的向上取整就是每个数的值都加一,所以就是加 i
这就可以通过前一项的 f[i - 1] 求得 F[i] 了,那么怎么求 f[i] 呢,对于当前得 F[i]来说,每一项都是向上取整得,要求他向下取整得数,那么就要减去那些不能整除 i 的数,那些数是通过向上取整取得的,所以要减去,直接找那些数不好找,但是找 i 的因子容易 ,那么就直接 先把 F[i] 减去 i ,然后再加上 i 的因子个数,就得到 f[i]了
f[i] = F[i] - i + (i 的因子数)
这样就算完了 F[i] ,但是里面还有要除去的,就是那些不互质的数也被计算到里面去了,要刨去这一部分
比如 2 ,它只有在 i = 2 的时候 2 / 1 可以计数加 2 ,在 i = 4 的时候 i / 2 = 2 计数也加2了,但是4 与 2是不互质的
所以要刨去,因为这里的容斥条件是互质关系,所以这部分的容斥可以用到莫比乌斯容斥。
假设当前 i = 12 ,要容斥掉 2 的倍数 的数,就是 2 4 6 8 10 12 可以发现它们加起来刚好就是 F[6] 就是 F[i / 2]
所以容斥的时候就是 F[j] += mub[i] * F[i / j]
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
#define maxn 1000006
#define mod 1000000007
int f[maxn],F[maxn],mub[maxn],prm[maxn],vis[maxn];
void init(){
f[1] = F[1] = 1; // f[i]表示 i/1 + i/2 + ... + i/i 向下取整的结果 F[i]为向上取整
for(int i = 2;i < maxn;i++){
if(!vis[i]){
mub[i] = -1;
prm[++prm[0]] = i; //打素数表 和 莫比乌斯表
}
for(int j = 1;j <= prm[0] && i * prm[j] < maxn;j++){
vis[i * prm[j]] = 1;
if(i % prm[j] == 0){
mub[i * prm[j]] = 0;
break;
}
mub[i * prm[j]] = -mub[i];
}
}
for(int i = 2;i < maxn;i++){
F[i] = f[i - 1] + i; //对于向上取整的部分,直接拿前一部分向下取整的加上一个 i
int x = i;
int ans = 1;
for(int j = 1;prm[j] * prm[j] <= i;j++){
int cnt = 1;
while(x % prm[j] == 0){
cnt++;
x /= prm[j];
}
ans *= cnt;
} // 这里计算出了 i 的因子个数
if(x > 1) // 为素数的时候 x > 1 则 有1和它本身两个因子
ans *= 2;
f[i] = F[i] - i + ans; // 向下取整的等于向上取整的刨去一层然后加上因子个数(是因子的才有贡献)
}
for(int i = 2;i < maxn;i++){
f[i] = F[i];
} //题目要的是向上取整的,所以保留 F,复制到 f ,拿 f 作为辅助
for(int i = 2;i < maxn;i++){
for(int j = i;j < maxn;j += i){
if(mub[i]){
F[j] += mub[i] * f[j / i];
}
}
}
for(int i = 2;i < maxn;i++){
F[i] += F[i - 1];
F[i] %= mod;
}
}
int main(){
init();
int n;
while(scanf("%d",&n) != EOF){
printf("%d\n",F[n]);
}
return 0;
}