# 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;
}

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

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