# Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2796    Accepted Submission(s): 1222

Problem 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

AC代码：

#include <iostream>
#include<stdio.h>
using namespace std;
typedef __int64 LL;
int Prim[100005],num[100005][20];
//求素因子
void init()
{
int i,j;
for(i=2; i<=100005; i++)
if(Prim[i]==0)
{
num[i][1]=i;
num[i][0]++;
for(j=i*2; j<=100000; j+=i)
{
num[j][++num[j][0]]=i;  //num[i][0]代表i的素因子个数，num[i][j]代表i的一个素因子。
Prim[j]=1;
}
}
}
LL dfs(int id,int b,int now)//求不大于b的数中,与now不互质的数的个数;
{
LL ans=0;
for(int i=id; i<=num[now][0]; i++)  //num[now][0]为now的素因子个数
ans+=b/num[now][i]-dfs(i+1,b/num[now][i],now);  //+奇数层集合-dfs(...)为偶数层集合 比如 + 16/2 - 16/2/3 + 16/3
return ans;
}

int main()
{
int m,n,t;
init();
cin>>t;
while(t--)
{
cin>>m>>n;
LL sum=0;
for(int i=2; i<=m; i++)
sum+=n-dfs(1,n,i);
printf("%I64d\n",sum+n);    //处理第一层
}
return 0;
}