hdu 2841 树围成矩阵,人在(0,0)点,最多可看到几棵树

题意:有一个n*m的矩阵上布满了树(矩阵从(1,1)开始),现在有一个农夫站在(0,0)点,问农夫可以看到多少棵树,其中如果这些树在一条线上那么只能看到最前面的那棵树,这个一开始看到确实蒙了。。看了题解其实是挺简单的。首先考虑只能看到一条线上最前面的那棵树这个条件,对于坐标 比如 (2,3)(4,6)(6,9)。。等 这些坐标是在一条直线上的 可以看出其除了(2,3) 其他的都是由(2,3)的x坐标*k y坐标*k 得到的, 拓展出来就是对于 任意坐标 (x,y) 令a=x/gcd(x,y) b=y/gcd(x,y) 那么那些和(x,y) 在一条直线的点的坐标可以表示为 (x+(-)a*k,y+(-)b*k) ,显然(a,b) 是这条线上的第一个点,即农夫可以看到的点,所以总的问题就可以转换为求x∈(1,n)y∈(1,m)范围内满足 x,y互质的坐标的个数。枚举(1,n)内的x坐标 ,求x与(1,m)内互质的数个数。

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
 

#include<stdio.h>
#include<iostream>
using namespace std;
int s[100020],k;
void init(int m)
{
    k=0;
    for(int i=2;i*i<=m;i++)
    {
        if(m%i==0)
            {s[k++]=i;
        while(m%i==0)
            m/=i;
            }
    }
    if(m>1)
        s[k++]=m;
}
int quc(int m)
{
    int t=0,p[100020],z;
    long long sum=0;
    p[t++]=-1;
    for(int i=0;i<k;i++)
    {
         z=t;
        for(int j=0;j<z;j++)
        {
            p[t++]=p[j]*s[i]*(-1);
        }
    }
    for(int i=1;i<t;i++)
    {
        sum+=m/p[i];
    }
    return sum;
}
int main()
{
    int n,a,b;
    long long sum;
    scanf("%d",&n);
    while(n--)
    {sum=0;
  scanf("%d %d",&a,&b);
    for(int i=1;i<=a;i++)
           {init(i);
           sum+=b-quc(b)+quc(0);
           }
           printf("%I64d\n",sum);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值