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.

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

题解:在之前集训队纳新的时候做过一个小于1000的 那个直接暴力找即可 而1e5显然不行,我们是要找该范围内能找出多少个i / j的形式,所以我们可以分别找出1-m 内与小于等于n互质的数有多少个,因数n最大为1e5 所以最多可以取6个质数

2*3*5*7*11*13=39270  复杂度最多64*1e5 可以过

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=12;

ll n,m;
int prime[7],len;
int solve(int x)
{
	len=0;
	for(int i=2;(ll)i*i<=x;i++)
	{
		if(x%i==0)
		{
			prime[len++]=i;
			while(x%i==0) x=x/i;	
		} 
	}
	if(x>1) prime[len++]=x;
	ll res=0;
	for(int i=1;i<(1<<len);i++)
	{
		int tmp=i,cnt=0;
		int ans=1;
		while(tmp)
		{
			if(tmp&1) cnt++;
			tmp/=2;
		}
		for(int j=0;j<len;j++)
		{
			if(i&(1<<(j)))
				ans*=prime[j];
		}
		if(cnt&1) res+=n/ans;
		else res-=n/ans;
	}
	return n-res;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
    	scanf("%lld%lld",&n,&m);
        ll ans=0;
        for(int i=1;i<=m;i++)
        	ans+=solve(i);
        cout<<ans<<endl;
    }
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值