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#IIS网站应用程序池启动回收停止 .

    //添加应用程序池空间引用using System.DirectoryServices;using System.Text; using System.Text.RegularExpressions; ...

  2. 百度地图api添加自定义控件

    官网栗子:http://lbsyun.baidu.com/jsdemo.htm#b0_6 <!DOCTYPE html><html><head> <meta ...

  3. Hibernate--快速上手

    一.初识 Hibernate 经典的软件应用体系结构有三层:表示层(提供了与用户交互的接口,实现用户操作界面,展示用户需要的数据).业务逻辑层(完成业务流程,处理表示层提交的数据请求,并将要保存的数据 ...

  4. 善用缓存提高你的Spring工程效率

    欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...

  5. SVN创建主干,分支、合并分支

    1.创建主干(trunk) 本文承接上文部分内容:http://www.cnblogs.com/dadonggg/p/8383696.html:部分不明,可以访问这篇文章. 当我们创建完代码仓库后,创 ...

  6. SpringCloud--Ribbon负载均衡

    Ribbon实现客户端负载均衡 负载均衡:是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一. 硬件负载均衡:主要通过在服务器节点之间安装专门用于负载均衡的设备: 软件负载均衡:通过在服务器 ...

  7. 徐州网络赛J-Maze Designer【最小生成树】【LCA】

    After the long vacation, the maze designer master has to do his job. A tour company gives him a map ...

  8. Linux下使用iotop检测磁盘io使用情况

    介绍 Linux下的IO统计工具如iostat, nmon等大多数是只能统计到per设备的读写情况, 如果你想知道每个进程是如何使用IO的就比较麻烦. iotop 是一个用来监视磁盘 I/O 使用状况 ...

  9. windows 文件查找 大小:>250M

    win7怎么快速查找大文件_百度经验 https://jingyan.baidu.com/article/acf728fd299ffff8e510a333.html

  10. In ZeroDB, the client is responsible for the database logic. Data encryption, decryption, and compression also happen client side. Therefore, the server never has any knowledge about the data, its str

    zerodb/index.rst at master · zerodb/zerodb https://github.com/zerodb/zerodb/blob/master/docs/source/ ...