Visible Trees(hdu2841)
Visible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2462 Accepted Submission(s): 1032
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.
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)
1 1
2 3
5
- 1 #include<stdio.h>
- 2 #include<algorithm>
- 3 #include<iostream>
- 4 #include<stdlib.h>
- 5 #include<string.h>
- 6 #include<queue>
- 7 int oula[100005];
- 8 bool prime[100005];
- 9 int ans[100005];
- 10 long long bns[100005];
- 11 int id[100005];
- 12 using namespace std;
- 13 typedef long long LL;
- 14 queue<int>que;
- 15 int main(void)
- 16 {
- 17 int i,j,k;
- 18 for(i=0; i<100005; i++)
- 19 oula[i]=i;
- 20 oula[1]=0;
- 21 memset(prime,0,sizeof(prime));
- 22 for(i=2; i<1000; i++)
- 23 {
- 24 if(!prime[i])
- 25 {
- 26 for(j=i; i*j<=100000; j++)
- 27 {
- 28 prime[i*j]=true;
- 29 }
- 30 }
- 31 }
- 32 int cnt=0;
- 33 for(i=2; i<=100000; i++)
- 34 if(!prime[i])
- 35 ans[cnt++]=i;
- 36 for(i=0; i<cnt; i++)
- 37 {
- 38 for(j=1; j*ans[i]<=100000; j++)
- 39 {
- 40 oula[j*ans[i]]/=ans[i];
- 41 oula[j*ans[i]]*=(ans[i]-1);
- 42 }
- 43 }
- 44 scanf("%d",&k);
- 45 int n,m;
- 46 while(k--)
- 47 {
- 48 scanf("%d %d",&n,&m);
- 49 memset(id,0,sizeof(id));
- 50 int xx=max(n,m);
- 51 int cc=min(n,m);
- 52 if(cc==1)
- 53 printf("%d\n",xx);
- 54 else
- 55 {
- 56 long long sum=0;
- 57 for(i=2; i<=cc; i++)
- 58 {
- 59 sum+=oula[i];
- 60 }
- 61 sum*=2;
- 62 for(i=cc+1; i<=xx; i++)
- 63 {
- 64 int vv=i;
- 65 int t=0;
- 66 int flag=0;
- 67 while(vv>1)
- 68 {
- 69 if(vv%ans[t])
- 70 {
- 71 t++;
- 72 flag=0;
- 73 }
- 74 else if(vv%ans[t]==0&&flag==0)
- 75 {
- 76 que.push(ans[t]);
- 77 flag=1;
- 78 vv/=ans[t];
- 79 }
- 80 else if(vv%ans[t]==0&&flag)
- 81 {
- 82 vv/=ans[t];
- 83 }
- 84 if(vv==1)
- 85 break;
- 86 }
- 87 int ak=0;
- 88 while(!que.empty())
- 89 {
- 90 id[ak++]=que.front();
- 91 que.pop();
- 92 }
- 93 LL ff=0;
- 94 int ii;
- 95 for(ii=1; ii<=(1<<ak)-1; ii++)
- 96 {
- 97 int tt=0;
- 98 LL pp=1;
- 99 for(j=0; j<ak; j++)
- 100 {
- 101 if(ii&(1<<j))
- 102 {
- 103 tt++;
- 104 pp*=id[j];
- 105 }
- 106 }
- 107 if(tt%2==0)
- 108 {
- 109 ff-=cc/pp;
- 110 }
- 111 else ff+=cc/pp;
- 112 }
- 113 sum+=cc-ff;
- 114
- 115 }
- 116 sum+=1;
- 117 printf("%lld\n",sum);
- 118 }
- 119 }
- 120 return 0;
- 121 }
Visible Trees(hdu2841)的更多相关文章
- 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 ...
- HDU-2841 Visible Trees(莫比乌斯反演)
Visible Trees 传送门 解题思路: 实际上的答案就是1~n与1~m之间互质的数的对数,写出式子就是 \(ans=\sum^{n}_{i=1}\sum^{m}_{j=1}[gcd(i,j)= ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- HDU 2841 Visible Trees 数论+容斥原理
H - Visible Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 2841 Visible Trees(数论)
标题效果:给你个m*n方格,广场格从(1,1)开始. 在树中的每个点,然后让你(0,0)点往下看,问:你能看到几棵树. 解题思路:假设你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊.挡 ...
- Visible Trees HDU - 2841
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...
- hdu 2841 Visible Trees 容斥原理
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...
- Visible Trees
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...
随机推荐
- 年底巩固下 CS 知识「GitHub 热点速览 v.21.49」
作者:HelloGitHub-小鱼干 期末到了!是时候来一波 CS 复习资料了,从本科基础知识开始到实用编程技术.本周 GitHub 热点趋势榜给你提供了最全的复习资料:清华的 CS 四年学习资料.W ...
- 普通用户iptables规则持久化,开机自动恢复
本文档对于docker环境下并不适用,docker环境的iptables持久化请参考https://www.cnblogs.com/wiseo/p/15000745.html 添加iptables规则 ...
- HelloWorldDynamic
package mbeanTest; import java.lang.reflect.Method; import javax.management.Attribute; import javax. ...
- oc中调用c函数 实现将字符串转换成unsigned char
帮助码友解决问题,从而复习了一下oc中调用c函数的方式 1,新建c 头文件 test.h 定义 c 函数 #ifndef test_h #define test_h void verificatio ...
- GCD的补充
1-1 关于GCD中的创建和释放 在iOS6.0之前,在GCD中每当使用带creat单词的函数创建对象之后,都应该对其进行一次release操作. 在iOS6.0之后,GC ...
- 【Java基础】方法调用机制——MethodHandle
MethodHandle是Java7引入的一种机制,主要是为了JVM支持动态语言. 一个MethodHandle调用示例 共有方法调用 首先,演示一下最基本的MethodHandle使用. 第一步:创 ...
- 使用jquery刷新页面以及javascript的一些基本函数
如何使用jquery刷新当前页面 下面介绍全页面刷新方法:有时候可能会用到 1.window.location.reload()刷新当前页面. 2.parent.location.reload()刷新 ...
- Non-terminating decimal expansion; no exact representable decimal result.
Non-terminating decimal expansion; no exact representable decimal result. 翻译为:非终止十进制扩展; 没有确切的可表示的小数 ...
- Redis - 2 - 聊聊Redis的RDB和AOF持久化 - 更新完毕
1.RDB 1.1).RDB是什么? RDB,全称Redis Database RDB是Redis进行持久化的一种方式,当然:Redis默认的持久化方式也是RDB 1.2).Redis配置RDB 1. ...
- 利用Windbg分析Magicodes.IE一次错误编写导致内存剧增
由于这近一年时间一直忙于写书和工作,一直没有水文,但是近期有几位朋友使用我们的Magicodes.IE反馈在导出过程中内存暴涨...好吧,不管怎样,不能苦了我们朋友,接下来我们通过windbg来看一下 ...