HDU 2841 Visible Trees(容斥定理)
来源:互联网 发布:pvc地板知乎 编辑:程序博客网 时间:2024/06/04 19:57
Visible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1951 Accepted Submission(s): 792
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
题意:有一个n*m的矩阵,左下角坐标为(1,1),右上角坐标为(n,m);你现在的位置的坐标是(0,0),输出现在你能看到几个点.
思路:你将矩阵仔细研究一下会发现如果两个点A(x1,y1),B(x2,y2)如果x2,y2有一个公约数k,使得x2/k == x1 && y2/k == y1 这样的话就两个点就会形成一个线段y=kx,正好(0,0)在这条线段中,故在这条线段上的矩阵上的点就只能看到一个。所以我们就得出结论如果坐标(x,y)中x,y互质,那么就能过被看到,如果我们将矩阵的一边固定住,假设是n,那么题意就转化成求1--m与区间[1,n]求互质的个数的题了,这样的话就是一个很简单的容斥定理的问题了,具体看代码吧。
点击打开链接
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<vector>#include<queue>#include<stack>#include<map>#define N 1100using namespace std;int n,m,k;int prime[N],num[N];int t;__int64 IEP(int pn){ /// [n,m]区间求与k互质的个数 int pt = 0; __int64 s = 0; num[pt++] = -1; for(int i=0;i<t;i++){ int l = pt; for(int j=0;j<l;j++){ num[pt++] = num[j]*prime[i]*(-1); } } for(int i=1;i<pt;i++){ s += pn/num[i]; } return s;}int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); __int64 sum = 0; for(int i=1;i<=n;i++){ memset(prime,0,sizeof(prime)); memset(num,0,sizeof(num)); int pk = sqrt(i); int nn = i; t = 0; for(int j=2;j<=pk;j++){ if(nn%j == 0){ prime[t++] = j; while(nn%j == 0){ nn = nn / j; } } } if(nn!=1){ prime[t++] = nn; } sum += m - IEP(m); } printf("%I64d\n",sum); } return 0;}
0 0
- hdu 2841 Visible Trees(容斥定理)
- HDU 2841 Visible Trees(容斥定理)
- hdu 2841 Visible Trees 寻找互质 容斥定理
- hdu 2841 Visible Trees&& hdu 1695 GCD(容斥定理+数论)
- Hdu 2841 Visible Trees 容斥原理
- hdu 2841 Visible Trees(容斥原理)
- HDU 2841 - Visible Trees (容斥原理)
- hdu 2841 Visible Trees【容斥原理】
- HDU 2841 Visible Trees 容斥
- hdu 2841 Visible Trees (容斥原理)
- HDU 2841-Visible Trees(容斥)
- HDU 2841 Visible Trees(容斥原理)
- [HDU 2841]Visible Trees:容斥原理
- HDU 2841 Visible Trees (数论,容斥原理)
- HDU 2841-Visible Trees(容斥原理)
- 【HDU】2841 - Visible Trees(容斥原理)
- HDU 2841 Visible Trees (容斥原理好题)
- HDU 2841:Visible Trees (容斥原理)
- JDBC之调用存储过程
- 软件测试系列之软件测试过程模型V,W,H,X等
- 【欧拉函数算法总结&&运算原理&&模板】
- ARM寻址方式
- Location的关键点
- HDU 2841 Visible Trees(容斥定理)
- 上海外滩附近一日游
- 动画AnimatorSet
- Binary Tree SDUT 2499
- 关于Android ViewPager的实现
- chmod的作用
- 打印一个0到1之间的数的二进制表示
- Perl 学习(八)
- C++变量存储方式浅析