Visible Trees

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

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
题意:从(0,0)往一个矩阵看,看矩阵中的那些点,问能看到多少个,如果有多个点与(0,0)在一条直线上那么只能看到最前端的点,在这个点后面是看不到的。
思路:欧拉函数+容斥原理;
可以把矩阵分成三块,一个上三角一个下三角,还有一个对角线,先单独讨论对角线,就只有一种(对角线指的是斜率为负一的那一条)
上三角(x,y)---(x<y);关于每一个横坐标,我们的目的是找所有的不同种的斜率,那么假如取(x,y)(x与y不互质,那么(x/(gcd(x,y),y/gcd(x,y)))));得到新坐标
(a,b)(a,b互质),所以我们要求的斜率的不同种类就是求oula[x];下三角=上三角(矩阵为正方形)直接欧拉函数;
由于给的不一定是正方形;n,m;
所以在min(n,m)中的所有点我们可以直接oula函数,ans=sum(oula[i])*2( 1<=i<=min(n,m) );
所以在另一个比较长的边,还剩下(min(n,m)+1,max(n,m))没有处理这里就转换成在(1,min(n,m))中求(min(n,m)+1,max(n,m))中每个数在(1,min(n,m))
区间有多少个数与之互素的个数的和;这里用容斥原理;求(min(n,m)+1,max(n,m))每个数的素因子,然后容斥求在(1,min(n,m))与之不互质的数的个数,再区间长度减去这个就是与之互素的个数;因为每个数的不同素因子不超过9个,所以复杂度为n*512
  1. 1 #include<stdio.h>
  2. 2 #include<algorithm>
  3. 3 #include<iostream>
  4. 4 #include<stdlib.h>
  5. 5 #include<string.h>
  6. 6 #include<queue>
  7. 7 int oula[100005];
  8. 8 bool prime[100005];
  9. 9 int ans[100005];
  10. 10 long long bns[100005];
  11. 11 int id[100005];
  12. 12 using namespace std;
  13. 13 typedef long long LL;
  14. 14 queue<int>que;
  15. 15 int main(void)
  16. 16 {
  17. 17 int i,j,k;
  18. 18 for(i=0; i<100005; i++)
  19. 19 oula[i]=i;
  20. 20 oula[1]=0;
  21. 21 memset(prime,0,sizeof(prime));
  22. 22 for(i=2; i<1000; i++)
  23. 23 {
  24. 24 if(!prime[i])
  25. 25 {
  26. 26 for(j=i; i*j<=100000; j++)
  27. 27 {
  28. 28 prime[i*j]=true;
  29. 29 }
  30. 30 }
  31. 31 }
  32. 32 int cnt=0;
  33. 33 for(i=2; i<=100000; i++)
  34. 34 if(!prime[i])
  35. 35 ans[cnt++]=i;
  36. 36 for(i=0; i<cnt; i++)
  37. 37 {
  38. 38 for(j=1; j*ans[i]<=100000; j++)
  39. 39 {
  40. 40 oula[j*ans[i]]/=ans[i];
  41. 41 oula[j*ans[i]]*=(ans[i]-1);
  42. 42 }
  43. 43 }
  44. 44 scanf("%d",&k);
  45. 45 int n,m;
  46. 46 while(k--)
  47. 47 {
  48. 48 scanf("%d %d",&n,&m);
  49. 49 memset(id,0,sizeof(id));
  50. 50 int xx=max(n,m);
  51. 51 int cc=min(n,m);
  52. 52 if(cc==1)
  53. 53 printf("%d\n",xx);
  54. 54 else
  55. 55 {
  56. 56 long long sum=0;
  57. 57 for(i=2; i<=cc; i++)
  58. 58 {
  59. 59 sum+=oula[i];
  60. 60 }
  61. 61 sum*=2;
  62. 62 for(i=cc+1; i<=xx; i++)
  63. 63 {
  64. 64 int vv=i;
  65. 65 int t=0;
  66. 66 int flag=0;
  67. 67 while(vv>1)
  68. 68 {
  69. 69 if(vv%ans[t])
  70. 70 {
  71. 71 t++;
  72. 72 flag=0;
  73. 73 }
  74. 74 else if(vv%ans[t]==0&&flag==0)
  75. 75 {
  76. 76 que.push(ans[t]);
  77. 77 flag=1;
  78. 78 vv/=ans[t];
  79. 79 }
  80. 80 else if(vv%ans[t]==0&&flag)
  81. 81 {
  82. 82 vv/=ans[t];
  83. 83 }
  84. 84 if(vv==1)
  85. 85 break;
  86. 86 }
  87. 87 int ak=0;
  88. 88 while(!que.empty())
  89. 89 {
  90. 90 id[ak++]=que.front();
  91. 91 que.pop();
  92. 92 }
  93. 93 LL ff=0;
  94. 94 int ii;
  95. 95 for(ii=1; ii<=(1<<ak)-1; ii++)
  96. 96 {
  97. 97 int tt=0;
  98. 98 LL pp=1;
  99. 99 for(j=0; j<ak; j++)
  100. 100 {
  101. 101 if(ii&(1<<j))
  102. 102 {
  103. 103 tt++;
  104. 104 pp*=id[j];
  105. 105 }
  106. 106 }
  107. 107 if(tt%2==0)
  108. 108 {
  109. 109 ff-=cc/pp;
  110. 110 }
  111. 111 else ff+=cc/pp;
  112. 112 }
  113. 113 sum+=cc-ff;
  114. 114
  115. 115 }
  116. 116 sum+=1;
  117. 117 printf("%lld\n",sum);
  118. 118 }
  119. 119 }
  120. 120 return 0;
  121. 121 }

Visible Trees(hdu2841)的更多相关文章

  1. Hdu2841 Visible Trees 2017-06-27 22:13 24人阅读 评论(0) 收藏

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. HDU-2841 Visible Trees(莫比乌斯反演)

    Visible Trees 传送门 解题思路: 实际上的答案就是1~n与1~m之间互质的数的对数,写出式子就是 \(ans=\sum^{n}_{i=1}\sum^{m}_{j=1}[gcd(i,j)= ...

  3. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  4. HDU 2841 Visible Trees 数论+容斥原理

    H - Visible Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 2841 Visible Trees(数论)

    标题效果:给你个m*n方格,广场格从(1,1)开始. 在树中的每个点,然后让你(0,0)点往下看,问:你能看到几棵树. 解题思路:假设你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊.挡 ...

  6. Visible Trees HDU - 2841

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

  8. hdu 2841 Visible Trees 容斥原理

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pr ...

  9. Visible Trees

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pr ...

随机推荐

  1. 年底巩固下 CS 知识「GitHub 热点速览 v.21.49」

    作者:HelloGitHub-小鱼干 期末到了!是时候来一波 CS 复习资料了,从本科基础知识开始到实用编程技术.本周 GitHub 热点趋势榜给你提供了最全的复习资料:清华的 CS 四年学习资料.W ...

  2. 普通用户iptables规则持久化,开机自动恢复

    本文档对于docker环境下并不适用,docker环境的iptables持久化请参考https://www.cnblogs.com/wiseo/p/15000745.html 添加iptables规则 ...

  3. HelloWorldDynamic

    package mbeanTest; import java.lang.reflect.Method; import javax.management.Attribute; import javax. ...

  4. oc中调用c函数 实现将字符串转换成unsigned char

    帮助码友解决问题,从而复习了一下oc中调用c函数的方式 1,新建c 头文件  test.h 定义 c 函数 #ifndef test_h #define test_h void verificatio ...

  5. GCD的补充

    1-1 关于GCD中的创建和释放     在iOS6.0之前,在GCD中每当使用带creat单词的函数创建对象之后,都应该对其进行一次release操作.           在iOS6.0之后,GC ...

  6. 【Java基础】方法调用机制——MethodHandle

    MethodHandle是Java7引入的一种机制,主要是为了JVM支持动态语言. 一个MethodHandle调用示例 共有方法调用 首先,演示一下最基本的MethodHandle使用. 第一步:创 ...

  7. 使用jquery刷新页面以及javascript的一些基本函数

    如何使用jquery刷新当前页面 下面介绍全页面刷新方法:有时候可能会用到 1.window.location.reload()刷新当前页面. 2.parent.location.reload()刷新 ...

  8. Non-terminating decimal expansion; no exact representable decimal result.

    Non-terminating decimal expansion; no exact representable decimal result.  翻译为:非终止十进制扩展; 没有确切的可表示的小数 ...

  9. Redis - 2 - 聊聊Redis的RDB和AOF持久化 - 更新完毕

    1.RDB 1.1).RDB是什么? RDB,全称Redis Database RDB是Redis进行持久化的一种方式,当然:Redis默认的持久化方式也是RDB 1.2).Redis配置RDB 1. ...

  10. 利用Windbg分析Magicodes.IE一次错误编写导致内存剧增

    由于这近一年时间一直忙于写书和工作,一直没有水文,但是近期有几位朋友使用我们的Magicodes.IE反馈在导出过程中内存暴涨...好吧,不管怎样,不能苦了我们朋友,接下来我们通过windbg来看一下 ...