HDOJ 2841 Visible Trees

Visible Trees

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


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
 

Source
 

Recommend
gaojie
 

大意是一个人站在(0,0)点看一个n*m大小的树林,问能看到多少棵树。


题解:如果两个点重合的话(指后面的点被前面的点挡住,在(0,0)点看不见),他们的斜率一定是相同的,也就是X1/X2=Y1/Y2。那么X1,Y1肯定有公约数。所以这个题就是算到底有多少个点的横纵坐标互质。

还是容斥原理,我感觉横纵坐标的那个思路很难想,但想出来这题就比较裸。


#include<stdio.h>
#include<string.h>
int p[100],k;
void getp(int n)
{
	int i;
	k=0;
	for(i=2;i*i<=n;i++)
	{
		if(n%i==0)
			p[k++]=i;
		while(n%i==0)
			n/=i;
	}
	if(n>1)
		p[k++]=n;
}
int nop(int m)
{
	int que[111111],i,j,sum,t,top=0;
	que[top++]=-1;
	for(i=0;i<k;i++)
	{
		t=top;
		for(j=0;j<t;j++)
			que[top++]=que[j]*p[i]*(-1);
	}
	sum=0;
	for(i=1;i<top;i++)
		sum+=m/que[i];
	return sum;
}
int main()
{
	int T;
	scanf("%d",&T);
	int n,m;
	while(T--)
	{
		scanf("%d%d",&n,&m);
		__int64 ans=n;
		for(int i=2;i<=m;i++)
		{
			getp(i);
			ans+=n-nop(n);
		}
		printf("%I64d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值