HDU6134(莫比乌斯反演)

Battlestation Operational

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


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
 

Source

2017 Multi-University Training Contest - Team 8


解题思路:直接莫比乌斯反演,最后要用到一个差分技巧。我的思路参考这个人:点击打开链接

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
typedef long long LL;
const LL mod = 1e9 + 7;
int prime[maxn];
bool valid[maxn];
int mu[maxn];
LL f[maxn];
LL g[maxn];
LL dp[maxn];
int n;
void getMu(int N = 1000000)
{
    mu[1] = 1;
    memset(valid, true, sizeof(valid));
    int cnt = 0;
    for(int i = 2; i <= N; i++)
    {
        if(valid[i])
        {
            mu[i] = -1;
            prime[++cnt] = i;
        }
        for(int j = 1; j <= cnt && i * prime[j] <= N; j++)
        {
            valid[i * prime[j]] = false;
            if(i % prime[j] == 0)
            {
                mu[i * prime[j]] = 0;
                break;
            }
            mu[i * prime[j]] = -mu[i];
        }
    }
}
void getG(int N = 1000000)
{
      for(int i = 1; i <= N; i++)
      {
          g[i]++;
          for(int j = i; j <= N; j += i)
          {
              g[j + 1]++;
          }
      }
      g[0] = 0;
      for(int i = 1; i <= N; i++)
      {
          g[i] = g[i] + g[i - 1];
      }
}
void getF(int N = 1000000)
{
    memset(f, 0, sizeof(f));
    for(int i = 1; i <= N; i++)
    {
        if(mu[i] == 0) continue;
        for(int j = i; j <= N; j += i)
        {
            f[j] = (f[j] + mu[i] * g[j / i]) % mod;
        }
    }
}
void initDp(int N = 1000000)
{
    dp[0] = 0;
    for(int i = 1; i <= N; i++)
    {
        dp[i] = (dp[i - 1] + f[i]) % mod;
    }
}
void init()
{
    getMu();
    getG();
    getF();
    initDp();
}
int main()
{
    init();
    while(~scanf("%d", &n))
    {
        cout<<dp[n]<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值