解题报告:HDU2841 Visible Trees 莫比乌斯反演


解题报告:HDU2841 Visible Trees 莫比乌斯反演

 Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2728    Accepted Submission(s): 1191


Problem Description
There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.

If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
 

Input
The first line contains one integer t, represents the number of test cases. Then there are multiple test cases. For each test case there is one line containing two integers m and n(1 ≤ m, n ≤ 100000)
 

Output
For each test case output one line represents the number of trees Farmer Sherlock can see.
 

Sample Input
  
  
2 1 1 2 3
 

Sample Output
  
  
1 5
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:   2837  2844  2843  2842  2840 
 

Statistic |  Submit |  Discuss | Note



题意:
n*m的表格( 从(1,1)到(n,m)  ),你在(0,0)处,若一棵树在另一棵树的正后方,则看不见,问最多能看见多少棵树。

思路:
稍作分析发现等同于求[1,n]和[1,m]有多少互质的数对,很基础的反演练习题,看看数据规模,应该也可以容斥做,容斥的做法见 解题报告:HDU1695(欧拉+容斥)

代码:
#include<bits/stdc++.h>

using namespace std;

namespace Mobius_table{


const int MAX_N = 1e5;

int all=0;
int pr[MAX_N/10+100];
bool isp[MAX_N+10];
int mu[MAX_N+10];
/*****莫比乌斯函数分块打表前缀和*****/
int sum[MAX_N+10];

inline void init(){
    memset(isp,0,sizeof(isp));
    mu[1] = sum[1] = 1;
    sum[0] = mu[0] = 0;
    for(int i=2;i<=MAX_N;i++){
        if(!isp[i]){
            pr[all++]=i;
            mu[i]=-1;
        }
        for(int j=0,k=pr[j]*i;k<=MAX_N;k=pr[++j]*i){
            isp[k] = true;
            if(i%pr[j]==0){
                mu[k] = 0;
                break;
            }mu[k] = -mu[i];
        }sum[i] = sum[i-1] + mu[i];
    }
//printf("prime num  = %d\n ",all);

}


}
using namespace Mobius_table;
int n,m;

int main()
{
    int T;init();
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        long long ans = 0;
        if(n>m){swap(n,m);}
        for(int i=1,j;i<=n;i=j+1){
            j = min( n/(n/i) , m/(m/i) );
            ans += 1LL*(sum[j]-sum[i-1])*(n/i)*(m/i);
        }printf("%I64d\n",ans);
    }return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值