题意:有一块(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(容斥)题解的更多相关文章

  1. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

  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 数论+容斥原理

    H - Visible Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  4. HDU 2841 Visible Trees(数论)

    标题效果:给你个m*n方格,广场格从(1,1)开始. 在树中的每个点,然后让你(0,0)点往下看,问:你能看到几棵树. 解题思路:假设你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊.挡 ...

  5. hdu 2841 Visible Trees 容斥原理

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pr ...

  6. hdu 2841 Visible Trees(容斥)

    原文链接 There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is stand ...

  7. HDU 2841 Visible Trees(莫比乌斯反演)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可 ...

  8. hdu 2841 Visible Trees

    /** 大意: 求[1,m], [1,n] 之间有多少个数互素...做了 1695 ,,这题就so easy 了 **/ #include <iostream> #include < ...

  9. [SDOI2009]Bill的挑战——全网唯一 一篇容斥题解

    全网唯一一篇容斥题解 Description Solution 看到这个题,大部分人想的是状压dp 但是我是个蒟蒻没想到,就用容斥切掉了. 并且复杂度比一般状压低, (其实这个容斥的算法,提出来源于y ...

随机推荐

  1. Python描述器引导(转)

    原文:http://pyzh.readthedocs.io/en/latest/Descriptor-HOW-TO-Guide.html 1. Python描述器引导(翻译) 作者: Raymond ...

  2. json数据爬虫。requests实现

    get请求 import json import requests # url = "https://www.mamalaile.cn/mamalailegw/page/waiterList ...

  3. 简单mysql常用命令

    在命令行 输入 mysql -uroot -p123456 (-u账号 -p密码)登入mysql服务器 1.设置mysql密码set password for 'root'@'localhost' = ...

  4. 【java】System.out重定向到文件,并重定向会console上

    重定向到文件: System.setOut(new PrintStream(new File("data\\train.1.scale"))); 重定向回console: //把输 ...

  5. NuGet:自定义配置信息(2)

    有些类库项目打包后,如何让别人引用的时候自动添加上对应的配置项目呢,比如EntityFramework的NuGet包,打开后可以看到类似下面的结构: 空白处右击——Add Content Folder ...

  6. centos 专题-各种配置应有尽有

    你想要的在这里都能得到解决: http://www.linuxidc.com/topicnews.aspx?tid=14

  7. CUDA从入门到精通 - Augusdi的专栏 - 博客频道 - CSDN.NET

    http://blog.csdn.net/augusdi/article/details/12833235 CUDA从入门到精通 - Augusdi的专栏 - 博客频道 - CSDN.NET CUDA ...

  8. 2:5 视图控制器result的配置

    result:   1:其实底层还是使用原来servlet的转发和重定向的方法: 2:redirectAction:只能定位到 action (比如下面name属性为 *User 的Action ,但 ...

  9. 通俗理解RxJS(一)

    自学 Rx 快有一个周了, 它非常适合处理复杂的异步场景.结合自己所学,决定写系列教程. 我认为, Rx 中强大的地方在于两处 管道思想,通过管道,我们订阅了数据的来源,并在数据源更新时响应 . 强大 ...

  10. 20154312 曾林 Exp8 web基础

    1.基础问题回答 1.1.什么是表单 1.2.浏览器可以解析运行什么语言 1.3.WebServer支持哪些动态语言 2.实践总结与体会 3.实践过程记录 ----3.1.Web前端:HTML基础 - ...