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.
 
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
 




         题意:有一个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]求互质的个数的题了,这种话就是一个非常easy的容斥定理的问题了,详细看代码吧。









#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 1100 using 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;
}

HDU 2841 Visible Trees(容斥定理)的更多相关文章

  1. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  2. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

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

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

  4. HDU 2841 Visible Trees(容斥)题解

    题意:有一块(1,1)到(m,n)的地,从(0,0)看能看到几块(如果两块地到看的地方三点一线,后面的地都看不到). 思路:一开始是想不到容斥...后来发现被遮住的地都有一个特点,若(a,b)有gcd ...

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

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

  6. HDU 2841 Visible Trees(数论)

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

  7. hdu 2841 Visible Trees 容斥原理

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

  8. hdu 2841 Visible Trees(容斥)

    原文链接 There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is stand ...

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

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可 ...

随机推荐

  1. html与JacaScript中的重要思想:预留后路、向后兼容、js分离

    以一个简单的web程序为例 详细设计模式请配合代码及凝视食用 <!DOCTYPE html> <!-- 1 预留退路:假设用户禁用了js.链接还能正常显示吗?(href) 2 分离j ...

  2. RecyclerView实现底部载入很多其它功能

    这两天在公司没有什么任务分配,就研究了下咱们Google在Android5.0 推出的一个用来取代ListView的列表控件----RecyclerView. 发现功能上确实比ListView强大了不 ...

  3. 2015.06.11,技术,关于Matlab中的Jbtest检验

    总体分布的正态性检验一般采取Jarque-Bera检验方法. 1. JBTest检验的定义: 在统计学中,Jarque-Bera检验是对样本数据是否具有符合正态分布的偏度和峰度的拟合优度的检验.该检验 ...

  4. 2015.04.16,外语,读书笔记-《Word Power Made Easy》 11 “如何辱骂敌人” SESSION 28

    TEASER PREVIEW (Teaser 片头,预告片,玩笑 Teaser trailer:预告片) 如何称呼这些人: 完全盲目的服从(obedience [әu'bi:diәns] n. 服从, ...

  5. 4. Median of Two Sorted Arrays[H]两个有序数组的中位数

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the midian of the ...

  6. dubbo 解决既是消费者又是提供者 Duplicate application configs 的问题

    首先  有应用A  是 提供者 应用B 来实现既是消费者又是提供者 在应用 B 这边新建两个xml dubbo-consumer.xml  消费者 <!-- 自动扫描注解:通过dubbo实现 - ...

  7. Java io 操作

    package tlistpackage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFou ...

  8. mysql学习 1

    1.数据库(Database)是按照数据结构来组织.存储和管理数据的仓库 2.RDBMS即关系数据库管理系统(Relational Database Management System)的特点: 1) ...

  9. LeetCode224. Basic Calculator (用栈计算表达式)

    解题思路 用两个栈分别存字符和数字. 顺序读入字符,处理方式分为字符和数字两种. 处理字符分为')'和非')'两种. 处理数字需要读取字符栈栈顶,分为'+'.'-'和非'+'.'-'. 代码 clas ...

  10. android webview一些注意事项(持续更新)

    1.loadUrl() 的参数必须“http://”开头: 2.如果用到内部类获取页面内容,此类不能混淆: 3.2中情况保持不混淆需要将webview所在的包都保持不混淆,常规的保持类不混淆不生效: ...