HDU 2841 Visible Trees(容斥)题解
题意:有一块(1,1)到(m,n)的地,从(0,0)看能看到几块(如果两块地到看的地方三点一线,后面的地都看不到)。
思路:一开始是想不到容斥...后来发现被遮住的地都有一个特点,若(a,b)有gcd(a,b)!= 1,那么就会被遮住。因为斜率k一样,后面的点会被遮住,如果有gcd,那么除一下就会变成gcd = 1的那个点的斜率了。所以问题转化为求gcd不为1有几个点,固定一个点,然后容斥。
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = + ;
const int seed = ;
const int MOD = + ;
const int INF = 0x3f3f3f3f;
int prime[maxn], p[maxn], pn;
int a[maxn], an;
void get(){
memset(p, , sizeof(p));
pn = ;
for(ll i = ; i < maxn; i++){
if(!p[i]){
prime[pn++] = i;
for(ll j = i * i; j < maxn; j += i)
p[j] = ;
}
}
}
int main(){
int n, m;
int T;
get();
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
ll ans = m;
for(int i = ; i <= n; i++){
ll cnt = ;
//质因数分解
an = ;
int x = i;
for(int j = ; prime[j] * prime[j] <= x && j < pn; j++){
if(x % prime[j] == ){
a[an++] = prime[j];
while(x % prime[j] == ){
x /= prime[j];
}
}
}
if(x > ) a[an++] = x;
//求gcd不为1
for(int j = ; j < ( << an); j++){
int num = ;
ll val = ;
for(int k = ;k < an; k++){
if(j & ( << k)){
num++;
val *= a[k];
}
}
if(num & ) cnt += m / val;
else cnt -= m / val;
}
ans += m - cnt;
}
printf("%lld\n", ans);
}
return ;
}
HDU 2841 Visible Trees(容斥)题解的更多相关文章
- C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...
- HDU 2841 Visible Trees(容斥定理)
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 2841 Visible Trees 数论+容斥原理
H - Visible Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 2841 Visible Trees(数论)
标题效果:给你个m*n方格,广场格从(1,1)开始. 在树中的每个点,然后让你(0,0)点往下看,问:你能看到几棵树. 解题思路:假设你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊.挡 ...
- hdu 2841 Visible Trees 容斥原理
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...
- hdu 2841 Visible Trees(容斥)
原文链接 There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is stand ...
- HDU 2841 Visible Trees(莫比乌斯反演)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可 ...
- hdu 2841 Visible Trees
/** 大意: 求[1,m], [1,n] 之间有多少个数互素...做了 1695 ,,这题就so easy 了 **/ #include <iostream> #include < ...
- [SDOI2009]Bill的挑战——全网唯一 一篇容斥题解
全网唯一一篇容斥题解 Description Solution 看到这个题,大部分人想的是状压dp 但是我是个蒟蒻没想到,就用容斥切掉了. 并且复杂度比一般状压低, (其实这个容斥的算法,提出来源于y ...
随机推荐
- CDN工作过程(第二种版本)
作者:代希刚链接:https://www.zhihu.com/question/36514327/answer/121026637来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- Core Data with Mantle
Mantle makes it easy to write a simple model layer for your Cocoa or Cocoa Touch application. Mantl ...
- matlab常用方法
1:matlab进行符号的虚数运算 直接使用符号 a+b*j运算,结果是一个角度值,不是复数. 可以使用 a+b*(1j) 进行运算. 如下 position(index,)=radius; ...
- tomcat访问
1:html页面或者需要访问的对象需要放置到webapps/ROOT下面既可以 http://localhost:8080/直接访问 2:
- SparkSQL UDF使用方法与原理详解
UDF是SQL中很常见的功能,但在Spark-1.6及之前的版本,只能创建临时UDF,不支持创建持久化的UDF,除非修改Spark源码.从Spark-2.0开始,SparkSQL终于支持持久化的UDF ...
- [LeetCode] 721. Accounts Merge_Medium tag: DFS recursive
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- BCB TLable控件透明背景属性
当我们希望一个Label适应它父窗口的背景时,设置Tranparent属性值就OK Transparent:true 透明 false 不透明
- Fiddler过滤指定域名
Fiddler过滤指定域名的方法一 切换到fiddler右侧窗口的Filters选项卡,勾选顶部的“Use Filters”,找到Hosts区域,设置以下三个选项: 1.第一项有三个选项,不做更改: ...
- UVM中的sequence使用(一)
UVM中Driver,transaction,sequence,sequencer之间的关系. UVM将原来在Driver中的数据定义部分,单独拿出来成为Transaction,主要完成数据的rand ...
- yii2之增加省市字段
第一步,利用数据库迁移文件改表 修改一下迁移文件: https://bitbucket.org/ysxy/zijiu.git