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. 网络下载功能实现(downloader ) ---- HTML5+

    模块:downloader Downloader模块管理网络文件下载任务,用于从服务器下载各种文件,并支持跨域访问操作.通过plus.downloader获取下载管理对象.Downloader下载使用 ...

  2. 三维凸包求重心到面的最短距离(HDU4273)

    http://acm.hdu.edu.cn/showproblem.php?pid=4273 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  3. 编程中,static的用法详解

    C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用.一.面向过程设计中的sta ...

  4. 170607、SQL Select语句完整的执行顺序

    SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...

  5. codeforces#510 Div2

    pre过了三题 后来A题被hack了 B题终测挂了 两题其实都是有一个小细节没有处理好 中间C还因为cinT了一次 唉本来打的还不错的 还是太菜了 继续加油吧 A-Benches 有n张椅子 原来第i ...

  6. 焦作网络赛L-Poor God Water【矩阵快速幂】

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  7. Process Monitor分析某个应用行为

    1.打开Process Mointor 2.点击filter-->filter   在弹出的对话框中Architecture 下拉框,选择Process Name 填写要分析的应用程序名字. 点 ...

  8. StartUML-类图

  9. stark - 注册表、生成url

    一.配置 stark组件开发,仿django自带得admin组件. 1. startapp stark 2. settings: 'stark.apps.StarkConfig' 3. 启动就执行 f ...

  10. PAT 1061 Dating[简单]

    1061 Dating(20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4 ...