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. c# 对数据库的操作

    1.首先需要引用 using System.Data.SqlClient; 2.创建连接 SqlConnection connection = new SqlConnection(); connect ...

  2. 输入一个网站地址到网站展现的过程以及APR协议(鬼知道中间经历了什么)

    以前只知道输入一个网站,然后看着返回琳琅满目的内容,其实中间经历的过程和步骤太多了.为了满足好奇心以及学习需要,特查阅了资料将其记录下来以备后续自己复习. 从我在地址栏输入www.zhihu.com ...

  3. 单机器启动多个tomcat的配置修改

    首先去apache下载一个tomcat,下载解压版的,比较方便   把这个tomcat(我下载的是tomcat7版本),解压两次,为了方便显示,我把解压出来的tomcat重命名成tomcat71和to ...

  4. centos7修改网卡名、密码重置

    修改网卡名称 编辑 /etc/sysconfig/grub   倒数第二行quiet 后加入 net.ifnames=0  biosdevname=0 执行 grub2-mkconfig -o /bo ...

  5. FZU 2098 刻苦的小芳(卡特兰数,动态规划)

    Problem 2098 刻苦的小芳 Accept: 42 Submit: 70 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Descr ...

  6. PHP关于函数的参数问题

    可能是自己以前写程序太规范了,今天发现个PHP函数参数个数的问题,定义的函数有三个参数,但是使用函数的时候竟然传了四个参数,更意外的是程序运行没有错误,甚至没有警告.于是依靠搜索引擎和PHP文档仔细查 ...

  7. python sort、sorted高级排序技巧(转)

    add by zhj: 没找到原文.可以按多个维度进行排序,而且可以指定他们的排序方向,如果维度都是数字,排序比较容易,用+/-号就可以 指定排序方向.否则,就调用多次sorted进行排序了,而且要按 ...

  8. Tomcat安装(linux系统)

    首先解压: 进入配置文件: 统一tomcat的编码,避免乱码的问题: 保存退出. 注意: 查看打开的端口: /etc/init.d/iptables status 开启端口: iptables -A ...

  9. MVC模式:python案例

    quotes = ('A man is not complete until he is married. Then he is finished.', 'As I said before, I ne ...

  10. Mybatis怎么在mapper中用多个参数

    原文地址:https://github.com/mybatis/mybatis-3/wiki/FAQ How do I use multiple parameters in a mapper? Jav ...