isible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2207 Accepted Submission(s): 905
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.
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,晕。。。。数据会爆int,注意
ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define fab(a) (a)>0?(a):(-a)
#define LL long long
#define MAXN 100100
#define INF 0xfffffff
using namespace std;
int aprime[MAXN];
int k;
void findprime(int x)
{
k=0;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
aprime[k++]=i;
while(x%i==0)
x/=i;
}
}
if(x>1)
aprime[k++]=x;
}
LL fun(int num,int t)
{
int i;
LL ans=0;
for(i=t;i<k;i++)
ans+=num/aprime[i]-fun(num/aprime[i],i+1);
return ans;
}
int main()
{
int t;
int a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
if(a>b)
swap(a,b);
LL ans=b;
for(int i=2;i<=a;i++)
{
findprime(i);
ans+=(b-fun(b,0));
}
printf("%lld\n",ans);
}
return 0;
}