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
题意 :给你一个(m,n)的矩阵,每个点上有一颗树,你站在(0,0)点看矩阵,前面的树会挡着后面的树,互质不会被挡,问你此时一共可以看到多少树!
题解: 本质就是求 【1-n】和【1-m】 中有多少的互质数对。可以用容斥
代码
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e5;
const int MAXM = 3000000;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8 ;
/*------------------------------*/
int p[100];
int ge;
void getn(int n){
ge=0;
for(int i=2;i*i<=n;i++){
if(n%i==0) {
p[ge++]=i;
while(n%i==0){
n/=i;
}
}
}
if(n>1) p[ge++]=n;
}
LL nop(LL m){
LL que[MAXN];
LL top=0;
que[top++]=-1;
for(int i=0;i<ge;i++){
LL temp=top;
for(int j=0;j<temp;j++){
que[top++]=que[j]*(LL)(p[i]*(-1));
}
}
LL sum=0;
for(LL i=1;i<top;i++){
sum+=m/que[i];
}
return sum;
}
int main(){
int t;cin>>t;
while(t--){
LL n,m;
cin>>n>>m;
LL cnt=n;
for(int i=2;i<=m;i++){
getn(i);
cnt+=(n-nop(n));
}
printf("%lld\n",cnt);
}
return 0;
}