hdu 2841 Visible Trees (容斥原理)

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.
InputThe 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)OutputFor 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

解 :比如 (1,2) (2,4)(3,6) 他们三个中只有(1,2)能被看到 ,显然(2,4) 和(3,6)都是(1,2)的倍数关系,即(x,y)若x和y互质那么能被看到

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stack>
#include<vector>
#include<queue>
#include<map>
#define nn 500
#define mm 100001
#define inff 0x3fffffff
#define ll long long
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
int t;
ll fa[110],q[mm],num;
ll aa(ll u,ll p)
{
    num=0;
    for(ll i=2;i*i<=p;i++)
    {
        if(p%i==0)
        {
            while(p%i==0) p/=i;
            fa[num++]=i;
        }
    }
    if(p>1) fa[num++]=p;
    ll k,ans,tt;
    tt=ans=0;
    q[tt++]=-1;
    for(ll i=0;i<num;i++)
    {
        k=tt;
        for(ll j=0;j<k;j++)
            q[tt++]=-1*q[j]*fa[i];
    }
    for(ll i=1;i<tt;i++)
        ans+=u/q[i];
    return u-ans;
}
ll res,n,m;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&m);
        res=0;
        for(ll i=1;i<=n;i++)
            res+=aa(m,i);
        printf("%lld\n",res);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值