Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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)的位置,如果到两个点那个人的位置的斜率相等则无法看到远处的那个点;
思路:找出(1-m)与i互质的个数 1<=i<=n;复杂度o(nsqrt(n));
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define inf 1e18
ll p[],flag,x,ans;
ll getp(ll x)
{
flag=;
for(ll i=;i*i<=x;i++)
{
if(x%i==)
{
p[flag++]=i;
while(x%i==)
x/=i;
}
}
if(x!=)
p[flag++]=x;
}
ll gcd(ll x,ll y)
{
return y==?x:gcd(y,x%y);
}
void dfs(ll lcm,ll step,ll pos)
{
if(lcm>x)return;
if(pos==flag)
{
if(step&)
ans+=x/lcm;
else
ans-=x/lcm;
return;
}
dfs(lcm,step,pos+);
dfs(lcm/gcd(lcm,p[pos])*p[pos],step+,pos+);
}
int main()
{
ll y,z,i,t;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&x,&y);
ans=;
for(i=;i<=y;i++)
{
getp(i);
dfs(,,);
}
printf("%I64d\n",ans);
}
return ;
}
 
 

hdu 2841 Visible Trees 容斥原理的更多相关文章

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

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

  2. HDU 2841 Visible Trees(容斥定理)

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

  3. HDU 2841 Visible Trees(数论)

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

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

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

  5. hdu 2841 Visible Trees(容斥)

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

  6. hdu 2841 Visible Trees

    /** 大意: 求[1,m], [1,n] 之间有多少个数互素...做了 1695 ,,这题就so easy 了 **/ #include <iostream> #include < ...

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

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

  8. hdu2848 Visible Trees (容斥原理)

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

  9. HDU2841 Visible Trees (容斥原理)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意: 一个人在(0,0)点,然后前面有一个m*n的格子 ,每一个格子的节点上有一棵树.问这个人 ...

随机推荐

  1. 【BZOJ1594】[Usaco2008 Jan]猜数游戏 二分答案+并查集

    [BZOJ1594][Usaco2008 Jan]猜数游戏 Description 为了提高自己低得可怜的智商,奶牛们设计了一个新的猜数游戏,来锻炼她们的逻辑推理能力. 游戏开始前,一头指定的奶牛会在 ...

  2. [SQL] sql server中如何查看执行效率不高的语句

    sql server中,如果想知道有哪些语句是执行效率不高的,应该如何查看呢?下面就将为您介绍sql server中如何查看执行效率不高的语句,供您参考.在测量功能时,先以下命令清除sql serve ...

  3. linux下nproc的作用

    文章来源: http://blog.csdn.net/odailidong/article/details/50561257 nproc是操作系统级别对每个用户创建的进程数的限制,在Linux下运行多 ...

  4. Linux系统下Redis缓存安装配置

    Redis是一个高性能的key-value数据库,现时越来越多企业与应用使用Redis作为缓存服务器.楼主是一枚JAVA后端程序员,也算是半个运维工程师了.在Linux服务器上搭建Redis,怎么可以 ...

  5. 【php】---mysql---基本操作及使用---【巷子】

    1.数据库简介 (1).什么是数据库?     一个文件  一个文件夹  一个u盘   一个硬盘......都叫做数据库    存放数据的仓库   (2).常见的数据库?     mySql  sql ...

  6. Oracle涂抹oracle学习笔记第8章RMAN说,我能备份

    本次测试服务器为172.16.25.33 使用rman连接本地数据库 rman target / 在rman中执行启动与关闭的命令与sqlplus相同 在rman中执行sql语句 sql ‘需要执行的 ...

  7. Educational Codeforces Round 28

    A. Curriculum Vitae 题目链接:http://codeforces.com/contest/846/problem/A 题目意思:给你一个只包含0-1的数组,现在要求去可以去掉一些元 ...

  8. scrapy爬虫系列之六--模拟登录

    功能点:如何发送携带cookie访问登录后的页面,如何发送post请求登录 爬取网站:bilibili.github 完整代码:https://files.cnblogs.com/files/book ...

  9. 项目无法运行iPhone5模拟器

    公司没有iPhone5真机, 有人反馈iPhone5有bug, 只能用模拟器验证bug, 但是使用iPhone5项目编译不过,报错: 注释掉相关引用代码,去掉 库

  10. Rochambeau---poj2912||zoj2751(并查集类似于食物链)

    题目链接:http://poj.org/problem?id=2912  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1751 ...