# HDU2841 一个关于互质的容斥

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

re无数次

#include <iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<memory.h>
using namespace std;
int sushu[100001];
int sus[100001];
int susu=0;
int n,m;
int ss[200];
int he[200];
int uu=0;
void dfs(int zuobiao,int ceng,int zong)
{
if(ceng>uu)return ;
if(zong>m)return;
else
{
int tem=zong*ss[zuobiao];
he[ceng]+=m/tem;
for(int a=zuobiao+1;a<=uu;a++)dfs(a,ceng+1,tem);
}
}
int main()
{
for(int a=2;a*a<=100000;a++)
{
if(sushu[a]==1)continue;
sus[++susu]=a;
for(int b=a+a;b<=100000;b+=a)sushu[b]=1;
}
int T;
cin>>T;
while(T--)
{
cin>>n>>m;
long long sum=m;
for(int a=2;a<=n;a++)
{
memset(ss,0,sizeof(ss));
memset(he,0,sizeof(he));
uu=0;
int y=a;
for(int b=1;sus[b]<=y&&b<=susu;b++)
{
if(a%sus[b]==0)
{
ss[++uu]=sus[b];
while(y%sus[b]==0)y/=sus[b];
}
}
if(y!=1)ss[++uu]=y;
for(int q=1;q<=uu;q++)dfs(q,1,1);
for(int q=1;q<=uu;q++)if(q%2)he[q]=-he[q];
sum+=m;
for(int q=1;q<=uu;q++)sum+=he[q];
}
cout<<sum<<endl;
}
return 0;
}

• 本文已收录于以下专栏：

## 杭电2841 容斥定理求1到n之间和x互质的数的总数

• wmn_wmn
• 2012年02月25日 11:18
• 1307

## HDU 4135：Co-prime 容斥原理求(1,m)中与n互质的数的个数

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...

## HDU 2841 Visible Trees(容斥原理，数论)

Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...
• perAres
• 2016年04月02日 17:20
• 144

## hdu2841 Visible Trees（容斥原理）

http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意：有一个n*m的格点矩形，除坐标轴上都有格点，求原点可以看见的格点个数。 思路：刚开始...

## HDU 2841 Visible Trees 解题报告（筛 or 容斥原理）

Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...
• kbdwo
• 2014年03月09日 14:50
• 596

## HDU 4135-Co-prime(容斥求区间内与N互质的个数(队列||位运算))

举报原因： 您举报文章：HDU2841 一个关于互质的容斥 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)