Hdu2841 Visible Trees
来源:互联网 发布:今日方知我是我的诗句 编辑:程序博客网 时间:2024/05/29 13:39
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.
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
21 12 3
Sample Output
15
Source
2009 Multi-University Training Contest 3 - Host by WHU
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质因数的分解,然后利用容斥定理计数
防止超时分解的质因数可以先打个表
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<string>#include<queue>#include<stack>#include<map>#include<set>using namespace std;#define LL long longconst int inf=0x3f3f3f3f;vector<LL>p[100010];int pr[100010];int a,b,x,y,k;LL ans;void init(){ memset(pr,0,sizeof pr); for(int i=1;i<=100005;i++) p[i].clear(); for(int i=2;i<=100005;i+=2) p[i].push_back(2); for(int i=3;i<=100005;i+=2) { if(!pr[i]) { for(int j=i;j<=100005;j+=i) { pr[j]=1; p[j].push_back(i); } } }}void dfs(int no,int pos,int xx,int cnt){ if(pos>=p[no].size()) { if(cnt==0) return; if(cnt%2) { ans+=y/xx; } else { ans-=y/xx; } return; } dfs(no,pos+1,xx*p[no][pos],cnt+1); dfs(no,pos+1,xx,cnt);}int main(){ init(); int T; int q=1; scanf("%d",&T); while(T--) { scanf("%d%d",&x,&y); if(x>y) swap(x,y); LL ans2=0; for(int i=1; i<=x; i++) { ans=0; dfs(i,0,1,0); ans2+=y-ans; } printf("%lld\n",ans2); } return 0;}
阅读全文
0 0
- Hdu2841 Visible Trees
- HDU2841 Visible Trees (容斥原理)
- HDU2841 Visible Trees【容斥原理】
- hdu2841 Visible Trees(容斥原理)
- 【容斥原理】HDU2841 Visible Trees
- hdu2841 Visible Trees(容斥原理)
- hdu2841 Visible Trees(容斥)
- 解题报告:HDU2841 Visible Trees 莫比乌斯反演
- HDU2841:Visible Trees(莫比乌斯函数)
- hdu2841(容斥原理+分解质因数)Visible Trees
- Visible Trees HDU2841(容斥定理的简单扩展)
- hdu2841 Visible Tree
- HDU 2841 Visible Trees
- HDU 2841 Visible Trees
- HDU 2841 Visible Trees
- HDOJ 2841 Visible Trees
- Visible Trees 【容斥】
- hdu 2841 Visible Trees
- ORACLE初学第四篇
- iOS处理导航栏遮挡视图问题
- 面试几点注意:需要提防的非语言陷阱
- TCP协议报文格式和三次握手,四次挥手
- 错误代码:0x80030001的一个好的解决办法
- Hdu2841 Visible Trees
- 网易编程题-圆上优雅点c++实现
- 【NOIP2017提高组模拟6.27】C
- 修改Android 的EditText的密码视觉回显
- java 线程池工作策略
- Java中利用Collections、HashMap、TreeSet混合使用Demo
- json转bean,并存进数据库
- 38 Python csv
- 第7课 面向对象的基本概念