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