Visible Trees

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

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
题意:在(0,0)点观察一个起始点为(1,1)点的n*m矩阵,矩阵每个位置都有树,问最多能看到多少棵树??
假设在(0,0)点可以看到点(A,B),那么(k*A,k*B)(k>1)都是看不到的,所以在这条斜率上能看到的两个点必然互质。所以题目就变成了在[1,m]内与x(1<=x<=n)互质的数有多少个了。这时我们的第一想法是欧拉函数,但是不行,因为是[1-m]范围内,只有当x==m时才可以用,所以我们将x分解质因数,假设 x=p1e1p2e2..那么在[1,m]与x不互质的数必然是pi的倍数,所以我们这里就要用容斥原理来解了。给出状压版本。
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long LL; int e[]; void devide(int n,int &id){
for(int i=;i*i<=n;i++){
if(n%i==){
e[id++] = i;
while(n%i==) n/=i;
}
}
if(n>) e[id++] = n;
}
int main()
{
int n,m;
int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d",&n,&m);
LL ans = m;
for(int i=;i<=n;i++){
int id = ;
devide(i,id);
int sum = ;
for(int j=;j<(<<id);j++){
int cnt = ,l=;
for(int k=;k<id;k++){
if((j>>k)&){
cnt++;
l*=e[k];
}
}
if(cnt&){
sum+=m/l;
}else{
sum-=m/l;
}
}
ans+=(LL)(m-sum);
}
printf("%lld\n",ans);
}
}

hdu 2841(容斥原理+状态压缩)的更多相关文章

  1. hdu 1796(容斥原理+状态压缩)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  3. hdu 5724 SG+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩)

    [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩) 题面 给出一棵树和一个图,点数均为n,问有多少种方法把树的节点标号,使得对于树上的任意两个节点u,v,若树上u ...

  5. hdu 4272 LianLianKan 状态压缩

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

  6. HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场

    题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...

  7. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

  8. hdu 2489 最小生成树状态压缩枚举

    思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

  9. LianLianKan - HDU 4272(状态压缩)

    题目大意:有一列数据,可以从最上面的开始连接下面相同的元素,然后消除,不过距离不能超过6,询问最后能不能消除完整个数列. 分析:首先讨论一点最远能消除的地方,比如点的位置是x,如若想要消除x+1位置处 ...

随机推荐

  1. NOIP2018 - 暑期博客整理

    暑假写的一些博客复习一遍.顺便再写一遍或者以现在的角度补充一点东西. 盛暑七月 初涉基环外向树dp&&bzoj1040: [ZJOI2008]骑士 比较经典的基环外向树dp.可以借鉴的 ...

  2. C++图书馆管理系统项目中部分功能代码实现(书籍推荐)

    bool UserServiceImpl::Compare1(Book b1,Book b2)//按照借阅次数比较{ if(b1.GetCnt() > b2.GetCnt()) { return ...

  3. Python模块(三)(正则,re,模块与包)

    1. 正则表达式 匹配字符串 元字符 .   除了换行 \w  数字, 字母, 下划线 \d  数字 \s  空白符 \n 换行符 \t  制表符 \b  单词的边界 \W  \D \S 非xxx [ ...

  4. 用\r做出进度条

    在做ftp作业的时候,需要做一个上传和下载的进度条,做的时候发现用\r很容易就能做出来 def show_progress(self, has, total): rate = float(has) / ...

  5. hdu 4565

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  6. 像玩魔兽一样编程——谈VS2010键盘流

    早年在学校里的时候,经常玩War3,那时候很痴迷,也经常看sky.moon的一些第一视角,有的时候也会模仿模仿...好吧,往事不堪回首,现在工作了,谈一谈.Net程序猿使用VS的键盘流,如果你不知道s ...

  7. nuc 第二届山西省大学生程序设计大赛 魔力手环

    problem 很妙啊--发现状态转移矩阵每一行都可以由上一行平移得到,每次只算第一行然后平移,\(O(n^3)\) 就变成了 \(O(n^2)\). #include <iostream> ...

  8. loj2280 「FJOI2017」矩阵填数

    状压 dp.参考there #include <algorithm> #include <iostream> #include <cstring> #include ...

  9. luogu2158 [SDOI2008]仪仗队 欧拉函数

    点 $ (i,j) $ 会看不见当有 $ k|i $ 且 $ k|j$ 时. 然后就成了求欧拉函数了. #include <iostream> #include <cstring&g ...

  10. xcode各个版本下载 xcode7 xcode6 xcode5

    登录开发者帐号,选择 support,然后操作如下图: 登录开发者帐号,选择 support,然后操作如下图: 登录开发者帐号,选择 support,然后操作如下图: 登录开发者帐号,选择 suppo ...