hdu 2841 树围成矩阵,人在(0,0)点,最多可看到几棵树
2016-05-06 21:08
447 查看
题意:有一个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
比如 (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); } }
相关文章推荐
- Java中的位运算
- 一个未解决的samba问题
- Unix网络编程--卷二:进程间通信
- Unix网络编程--卷二:FAQ
- [Android]意图回传数据2
- 卷积神经网络
- 随便说说
- 高精度求N!
- github如何上传iOS源代码(最精炼版)
- 基于gibbsLDA的文本分类
- 多态
- [LeetCode] Design Tic-Tac-Toe 设计井字棋游戏
- iOS 开发查看应用的沙盒文件
- Scrum 项目1.0
- 题目1 : 打折机票(hihoCoder挑战赛20)
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- 第9周项目二-我的数组类
- Java中的位运算
- 碰撞检测算法:点和矩形碰撞、点和圆形碰撞、矩形碰撞、圆形碰撞
- selenium 学习-js执行,事件模拟,页面加载等待