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.
InputThe 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)OutputFor each test case output one line represents the number of trees Farmer Sherlock can see.Sample Input
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
2 1 1 2 3Sample Output
1 5
解 :比如 (1,2) (2,4)(3,6) 他们三个中只有(1,2)能被看到 ,显然(2,4) 和(3,6)都是(1,2)的倍数关系,即(x,y)若x和y互质那么能被看到
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stack>
#include<vector>
#include<queue>
#include<map>
#define nn 500
#define mm 100001
#define inff 0x3fffffff
#define ll long long
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
int t;
ll fa[110],q[mm],num;
ll aa(ll u,ll p)
{
num=0;
for(ll i=2;i*i<=p;i++)
{
if(p%i==0)
{
while(p%i==0) p/=i;
fa[num++]=i;
}
}
if(p>1) fa[num++]=p;
ll k,ans,tt;
tt=ans=0;
q[tt++]=-1;
for(ll i=0;i<num;i++)
{
k=tt;
for(ll j=0;j<k;j++)
q[tt++]=-1*q[j]*fa[i];
}
for(ll i=1;i<tt;i++)
ans+=u/q[i];
return u-ans;
}
ll res,n,m;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&m);
res=0;
for(ll i=1;i<=n;i++)
res+=aa(m,i);
printf("%lld\n",res);
}
}