****HDU - 2841Visible Trees 容斥+欧拉函数

本文探讨了一种典型的算法问题——视界树问题。该问题要求计算在特定条件下可见的树木数量,采用数学方法如欧拉函数及容斥原理进行解答,并提供两种不同的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Hint

题意

一条直线上的点不能被看到 求能看到的树的数目

题解:

可以发现gcd(x,y)!=1的点都不能被看到 所以用容斥定理来做

AC代码

#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+5;
int prime[N],phi[N];
vector<int > yinzi[N];
int tot = 0;
void init(){
    phi[1] = 1;
    for (int i = 2; i < N; ++i){
        if (!phi[i]){
            phi[i]=i-1;
            prime[tot++]=i;
        }
        for (int j = 0; j < tot&&1ll*prime[j]*i<N; ++j){
            if (i%prime[j]) phi[i*prime[j]] = phi[i]*(prime[j]-1);
             else {
                phi[i*prime[j]] = phi[i]*prime[j];
                break;
             }
        }
    }
    for (int i = 2; i < N; ++i){
        int tmp = i;
        for (int j = 0;(LL)prime[j]*prime[j]<=tmp; ++j){
            if (tmp%prime[j]==0){
                yinzi[i].push_back(prime[j]);
                tmp/=prime[j];
                while(tmp%prime[j]==0) tmp/=prime[j];
            }
            if (tmp == 1) break;
        }
        if (tmp>1) yinzi[i].push_back(tmp);
    }
}
LL dfs(int x,int n,int now){
    LL res = 0;
    for (int i = x; i < yinzi[now].size(); ++i){
        res += n/yinzi[now][i]-dfs(i+1,n/yinzi[now][i],now);
    }
    return res;
}
int main(){
    init();
    int t;
    scanf("%d",&t);
    while (t--){
        int n,m;
        scanf("%d%d",&n,&m);
        if (n>m) swap(n,m);
        LL ans = 0;
        /*欧拉函数是小于n所有与n互质的数目 这个包括1 但这一题1 3互质与 3 1互质是两个点*/
        for (int i = 1; i <= n; ++i){
            if (i!=1) ans+=phi[i]*2;
            else ans+=phi[i];
        }/*对于求1~n与1~n之间的互质的种数(与点对数不同 因为gcd(x,y)==1和gcd(y,x)==1算是一种) 所以要看清问的是什么 虽然欧拉函数是问的小于n所有与n互质的数目 但由于前面所说 所以1~n欧拉函数求和即可算出互质的种数*/ 
        for (int j = n+1; j <= m; ++j){
            ans += n-dfs(0,n,j);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

/*比如对于phi[6] = 2  1 6互质 5 6互质 题目问题是求互质的点对数 那么除了1 6 实际还有6 1这个点 1 1点只有一对*/

纯容斥写法 之前一直wa在1e5这个边界上

#include <cstdio>
#include <map>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
int phi[N];
int prime[N];
vector<int> yinzi[N];
int tot = 0;

void init(){
    phi[1] = 1;
    for (int i = 2; i < N; ++i){
        if (!phi[i]){
            prime[tot++] = i;
            phi[i] = i-1;
        }
        for (int j = 0;j < tot&&1ll*i*prime[j]<N; ++j){
            if (i%prime[j]) phi[i*prime[j]] = phi[i]*(prime[j]-1);
            else {
                phi[i*prime[j]] = phi[i]*prime[j];
                break;
            }
        }
    }
    for (int i = 1; i <= 1e5; ++i){
        int tmp = i;
        for (int j = 0; (LL)prime[j]*prime[j]<=tmp; ++j ){
            if (tmp%prime[j]==0){
                yinzi[i].push_back(prime[j]);
                while (tmp%prime[j]==0) tmp/=prime[j];
            }
            if (tmp==1) break;
        }if (tmp>1) yinzi[i].push_back(tmp);
    }
}

LL ronc(int a,int b,int now){
    LL res = 0;
    for (int i = a; i < yinzi[now].size(); ++i){
        res += b/yinzi[now][i]-ronc(i+1,b/yinzi[now][i],now);
    }
    return res;
}
int main(){
    init();
    int t;
    scanf("%d",&t);
    while (t--){
        LL n,m;
        scanf("%lld%lld",&n,&m);
        if (n>m) swap(n,m);
        LL ans = 0;
        for (int i = 1;i <= m; ++i){
            ans += n-ronc(0,n,i);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

??这题还可以莫比乌斯反演啊啊啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值