Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2737    Accepted Submission(s): 1196

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
思路:若点(x,y)中x与y互质则点(x,y)可以看见,否则被挡住。那么题意就转化为1~m与i互质的点的个数之和,其中(1<=i<=n)。
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN=;
vector<int> divisor[MAXN];
void prep()
{
for(int e=;e<MAXN;e++)
{
int x=e;
for(int i=;i*i<=x;i++)
{
if(x%i==)
{
divisor[e].push_back(i);
while(x%i==) x/=i;
}
}
if(x>) divisor[e].push_back(x);
}
}
LL sieve(LL m,LL n)
{
LL ans=;
for(LL mark=;mark<(<<divisor[n].size());mark++)
{
LL mul=;
LL odd=;
for(LL i=;i<divisor[n].size();i++)
{
if(mark&(<<i))
{
mul*=divisor[n][i];
odd++;
}
}
LL cnt=m/mul;
if(odd&) ans+=cnt;
else ans-=cnt;
}
return m-ans;
}
int n,m;
int main()
{
int T;
prep();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
LL res=;
for(int i=;i<=n;i++)
{
res+=sieve(m,i);
}
printf("%lld\n",res);
}
return ;
}
 

HDU2841(容斥原理)的更多相关文章

  1. HDU2841 Visible Trees(容斥原理)

    题目..大概就是有个m*n个点的矩形从(1,1)到(m,n),问从(0,0)出发直线看过去最多能看到几个点. 如果(0,0)->(x,y)和(0,0)->(x',y')两个向量平行,那后面 ...

  2. HDU2841 Visible Trees (容斥原理)

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

  3. 容斥原理——hdu2841

    记得要开ll /* 莫比乌斯反演模板题,也可以直接算phi来做 容斥的解法 求x[1..m],在[1,n]中和其互质的数的个数即可 那么就是n-和x不互质的数个数即可 */ #include<b ...

  4. 容斥原理应用(求1~r中有多少个数与n互素)

    问题:求1~r中有多少个数与n互素. 对于这个问题由容斥原理,我们有3种写法,其实效率差不多.分别是:dfs,队列数组,位运算. 先说说位运算吧: 用二进制1,0来表示第几个素因子是否被用到,如m=3 ...

  5. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  6. hdu2848 Visible Trees (容斥原理)

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

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

  9. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

随机推荐

  1. Go 语言为Fibonacci函数实现Read方法

    Go语言非常灵活,只要为对象实现了相应的方法就可以把他看成实现了某个接口,类似于Durk Type, 为Fibonacci实现Read方法,就可以像读取文件一样,去读取下一个Fibonacci值. 示 ...

  2. linux 查看各目录(文件夹)下文件大小

    # 显示总大小(/下全部文件占用大小) du -sh /* | sort -nr # 显示各文件夹的大小(当前文件夹下各文件夹的大小) du --max-depth=1

  3. 【atcoder】All Your Paths are Different Lengths[arc102D](乱搞)

    题目传送门:https://arc102.contest.atcoder.jp/tasks/arc102_b 这道题有点毒瘤啊,罚时上天.. 显然若$ l=2^n $那么就可以直接二进制拆分,但是如果 ...

  4. 描述一下你最常用的编程风格---JAVA

    描述一下你最常用的编程风格---JAVA     描述一下你最常用的编程风格---JAVA   (1)类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符,其中包含的所有单词都 ...

  5. Codeforces Round #374 (Div. 2) A , B , C 水,水,拓扑dp

    A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...

  6. org.hibernate.TypeMismatchException: Provided id of the wrong type for class cn.itcast.entity.User. Expected: class java.lang.String, got class java.lang.Integer at org.hibernate.event.internal.Defau

    出现org.hibernate.TypeMismatchException: Provided id of the wrong type for class cn.itcast.entity.User ...

  7. Qt5 窗口内的控件随窗口大小自动变化

    http://www.bubuko.com/infodetail-827151.html 讲的很详细

  8. Windows 运行中的命令

    辅助功能选项 access.cpl 添加硬件向导 hdwwiz.cpl 添加或删除程序 appwiz.cpl 管理工具 control admintools 自动更新 wuaucpl.cpl Blue ...

  9. C# 6.0 (VS2015 CTP6)

    /* C# 6.0 demo https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14 */ using ...

  10. 为mac编写swift脚本

    代码示例: #!/usr/bin/env xcrun swift print("Hello World") 可以用Sublime Text编写,安装Swift包后有语法着色功能.然 ...