HDU 6134 数学公式 + 莫比乌斯反演

17 篇文章 0 订阅
1 篇文章 0 订阅

Battlestation Operational

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


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 】 (向上取整)

题目给的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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值