HDU 2841 Visible Trees 解题报告(筛 or 容斥原理)

96 篇文章 0 订阅

Visible Trees

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


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
 

    解题报告:题目意思很简单,(0, 0)点能与(1, 1)到(n, m)的矩形点阵中的格点连接成多少条不同的直线。
    容斥原理专题里做到的,但是感觉没有用的容斥定理。直接筛就好了。
    首先用线(0, 0) - (n, m)将所有的点分成上下两部分,一部分一部分计算。如果发现2位置有一个可见点,那么4,6,8,...直到n位置都将减少一个可见点。求和即为解。
    注意(0, 0) - (n, m)线上的点计算了两次,减去1即可。代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
const int maxn = 100010;
int deg[maxn];

LL cal(int n, int m)
{
    memset(deg, 0, sizeof(deg));

    LL ans = 0;
    for(int i=1;i<=n;i++)
    {
        deg[i]+=((long long)m*i/n);
        ans+=deg[i];

        for(int j=2;i*j<=n;j++)
            deg[i*j]-=deg[i];
    }

    return ans;
}

void work()
{
    int n,m;
    scanf("%d%d", &n, &m);

    printf("%I64d\n", cal(n, m)+cal(m, n)-1);
}

int main()
{
    int T;
    scanf("%d\n", &T);
    while(T--)
        work();
}

    好吧,看了一下其他人的解题报告,发现也可以用容斥原理来做。能被看到的树的位置(x,y),x与y一定是互质的。

    这样题目就可以这么理解:求[1,n]区间与[1,m]区间互质数的对数。

    预处理欧拉函数求[1, min(n,m)]内互质数的对数,容斥原理求[1, min(n,m)]与[min(n,m)+1, max(n,m)]间互质数的对数。

    时间都是15MS,代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

typedef long long LL;
const int maxn = 100010;
LL sum[maxn];
int phi[maxn];

int deg[maxn],cnt[maxn];
bool good[maxn];
int cas = 1;

void all_phi()
{
    phi[1] = sum[1] = 1;
    for(int i=2;i<maxn;i++)
    {
        if(phi[i]==0)
        {
            for(int j=i;j<maxn;j+=i)
            {
                if(phi[j]==0) phi[j]=j;
                phi[j]=phi[j]/i*(i-1);    // 先除后乘,不会溢出且不影响结果
            }
        }

        sum[i] = sum[i-1]+phi[i];
    }
}

LL cal(int n, int m)
{
    for(int i=0;i<=n;i++) deg[i]=0,cnt[i]=0,good[i]=true;

    LL ans = 0;
    for(int i=2;i<=n;i++)
    {
        if(good[i])
        {
            if(deg[i]==0) deg[i]=1;
            int add = (m/i-n/i)*(deg[i]&1?1:-1);
            cnt[i]+=add;

            for(int j=2;i*j<=n;j++)
            {
                if(deg[i]==1)
                    if(j%i==0)
                        good[i*j]=false;
                    else
                        deg[i*j]++;
                cnt[i*j]+=add;
            }
        }
        ans+=cnt[i];
    }

    return (LL)(m-n)*n - ans;
}

void work()
{
    int m,n;
    scanf("%d%d", &n, &m);
    if(n>m) swap(n, m);

    printf("%I64d\n", sum[n]*2-1+cal(n,m));
}

int main()
{
    all_phi();

    int T;
    scanf("%d\n", &T);
    while(T--)
        work();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值