Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3131    Accepted Submission(s): 1387

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
  1. 2
  2. 1 1
  3. 2 3
 
Sample Output
  1. 1
  2. 5
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2837 2844 2843 2842 2840 

————————————————————————————————————
题目的 意思是从(1,1)开始有个m*n的矩阵每个整点有一颗树 问从(0,0)看过去可以看到多少克树

思路:说白了就是求[1,m],[1,n]有多少对数互质(不互质的肯定会被挡住) 可以枚举m,在n中找有多少互质的数,可以先把枚举的m质因数的分解,然后利用容斥定理计数
防止超时分解的质因数可以先打个表

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<string>
  7. #include<queue>
  8. #include<stack>
  9. #include<map>
  10. #include<set>
  11. using namespace std;
  12. #define LL long long
  13. const int inf=0x3f3f3f3f;
  14.  
  15. vector<LL>p[100010];
  16. int pr[100010];
  17. int a,b,x,y,k;
  18. LL ans;
  19.  
  20. void init()
  21. {
  22. memset(pr,0,sizeof pr);
  23. for(int i=1;i<=100005;i++) p[i].clear();
  24. for(int i=2;i<=100005;i+=2)
  25. p[i].push_back(2);
  26. for(int i=3;i<=100005;i+=2)
  27. {
  28. if(!pr[i])
  29. {
  30. for(int j=i;j<=100005;j+=i)
  31. {
  32. pr[j]=1;
  33. p[j].push_back(i);
  34. }
  35.  
  36. }
  37. }
  38. }
  39.  
  40. void dfs(int no,int pos,int xx,int cnt)
  41. {
  42. if(pos>=p[no].size())
  43. {
  44. if(cnt==0)
  45. return;
  46. if(cnt%2)
  47. {
  48. ans+=y/xx;
  49. }
  50. else
  51. {
  52. ans-=y/xx;
  53. }
  54. return;
  55. }
  56. dfs(no,pos+1,xx*p[no][pos],cnt+1);
  57. dfs(no,pos+1,xx,cnt);
  58. }
  59.  
  60. int main()
  61. {
  62. init();
  63. int T;
  64. int q=1;
  65. scanf("%d",&T);
  66. while(T--)
  67. {
  68. scanf("%d%d",&x,&y);
  69. if(x>y) swap(x,y);
  70. LL ans2=0;
  71. for(int i=1; i<=x; i++)
  72. {
  73. ans=0;
  74. dfs(i,0,1,0);
  75. ans2+=y-ans;
  76. }
  77. printf("%lld\n",ans2);
  78. }
  79. return 0;
  80. }

Hdu2841 Visible Trees 2017-06-27 22:13 24人阅读 评论(0) 收藏的更多相关文章

  1. Codeforces 768A Oath of the Night's Watch 2017-02-21 22:13 39人阅读 评论(0) 收藏

    A. Oath of the Night's Watch time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. 全方位分析Objcetive-C Runtime 分类: ios技术 2015-03-11 22:29 77人阅读 评论(0) 收藏

    本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 简介 与Runtime交互 ...

  3. java面试和笔试大全 分类: 面试 2015-07-10 22:07 10人阅读 评论(0) 收藏

    2.String是最基本的数据类型吗? 基本数据类型包括byte.int.char.long.float.double.boolean和short. java.lang.String类是final类型 ...

  4. Struts知识问答 分类: 面试 2015-07-10 22:01 4人阅读 评论(0) 收藏

    1. 简述Struts框架的初始化流程. 答案: 对于采用Struts框架的Web应用,在Web应用启动时就会加载并初始化控制器ActionServlet ActionServlet从struts-c ...

  5. hdu 1039 (string process, fgets, scanf, neat utilization of switch clause) 分类: hdoj 2015-06-16 22:15 38人阅读 评论(0) 收藏

    (string process, fgets, scanf, neat utilization of switch clause) simple problem, simple code. #incl ...

  6. magic矩阵 分类: 数学 2015-07-31 22:56 2人阅读 评论(0) 收藏

    魔方矩阵 魔方矩阵是有相同的行数和列数,并在每行每列.对角线上的和都相等.你能构造任何大小(除了2x2)的魔方矩阵. 1.历史       魔方又称幻方.纵横图.九宫图,最早记录于我国古代的洛书.据说 ...

  7. MATLAB符号运算 分类: 图像处理 2015-07-31 22:53 3人阅读 评论(0) 收藏

    1.符号运算 使用MATLAB可以进行多项式乘除运算,也可以进行因式分解. 例1. 多项式乘除运算(x+3)3 >> syms x; >> expand((x+3)^3) an ...

  8. ListView 分类: WinForm 2014-07-18 22:03 289人阅读 评论(0) 收藏

    一.ListView类(转载) 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLin ...

  9. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

随机推荐

  1. 《大道至简》第一章--编程的精意 读后感(JAVA伪代码)

    1. /*愚公移山 原始需求:惩山北之塞,出入之迂: 项目沟通:聚室而谋曰: 项目目标:毕力平险,指通豫南,达于汉阴: 技术方案:扣石垦壤,箕畚运于渤海之尾: 人员构成:愚公率子孙荷担者三夫,邻人京城 ...

  2. ListView的自定义适配器及其优化(listView序号混乱问题的处理)

    ListView是最常使用的android组件之一,关于listView的优化问题刚刚了解了一些,在这里做出总结. PS:如果想让ListView中的item根据数据内容显示item的大小,需要在it ...

  3. 代码UITableView点击cell跳转

    首先,在tableViewController中设置好 代理和数据源方法: @interface FirstTableViewController ()<UITableViewDataSourc ...

  4. Mac OS 10.12 - 在VMwear Workstation12.5.2中以两种方式进入恢复模式(Recovery)!!!

    注意:如果你打算安装Mac OS10.12 到虚拟机里面学习,那么我强烈建议你在没有安装任何其它软件之前,按照我这篇博客来进入恢复模式(Recovery),禁用Rootless机制!!!这样处理后,你 ...

  5. 20172306 《Java程序设计与数据结构》第七周学习总结

    20172306<Java程序设计>第七周学习总结 教材学习内容总结 这一章的标题是继承.主要学习了有关继承的相关知识.其中在这五节中,我学到了以下几点: 1.继承主要表达的是" ...

  6. CSharp陷阱1

    CSharp陷阱1 环境:sharpdevelop 3     .net 2.0 正确的        internal static readonly string[] string_2 = new ...

  7. SQL表两列取一列唯一值的记录

    问下SQL表两列取一列唯一值的 A列         B列       C列 1001      AA      2012-01-02 1001      BB      2012-02-05 100 ...

  8. SQL查找指定行的记录

    select top 1 * from (select top 4 * from T_GasStationPrice order by EnableTime) a order by EnableTim ...

  9. 核心一:IoC

    IoC 1.中文名称:控制反转 2.英文名称:(Inversion of Control) 3.IoC是什么? 3.1 IoC 完成的事情原先由程序员主动通过new 实例化对象事情,转交给Spring ...

  10. Educational Codeforces Round 54 E. Vasya and a Tree(树上差分数组)

    https://codeforces.com/contest/1076/problem/E 题意 给一棵树(n<=3e5),m(3e5)次查询,每次查询u,d,x,表示在u的子树中,给距离u&l ...