HDU - 2814 Visible Trees
题意:
m*n(1<=m,n<=100000)的森林里,起始点在(1,1),某人从(0,0)点开始看,问能看到多少棵树。
题解:
求出1~x中的每个数与1~y的数中互质的数的总和。用素数筛筛出1e5以内的素数。在用这些素数筛出1e5以内每个数的素数因子。最后通过容斥算出与每个数互质的个数。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
int t;
int x, y;
int len;
int pnum;
int p[maxn];
bool e[maxn];
ll res, ans;
vector<int> g[maxn];
void prime() {
e[] = e[] = ; pnum = ;
for(int i = ; i < maxn; i++) {
if(e[i]==) p[++pnum] = i;
for(int j = ; j<=pnum && p[j]*i<maxn; j++) {
e[p[j]*i] = ;
if(i%p[j]==) break;
}
}
}
int gcd(int x, int y) {
return y==?x:gcd(y, x%y);
}
int lcm(int x, int y) {
return x/gcd(x, y)*y;
}
void dfs(int cur, int tol, int sum, int id) {
if(cur >= len) return ;
int p = lcm(g[id][cur], sum);
if(tol&) res -= y/p;
else res += y/p;
dfs(cur+, tol+, p, id);
dfs(cur+, tol, sum, id);
}
int main() {
prime();
for(int i = ; i <= pnum; i++) {
int cnt = ;
while(cnt*p[i] <= ) {
g[cnt*p[i]].push_back(p[i]);
cnt++;
}
}
scanf("%d", &t);
while(t--) {
scanf("%d%d", &x, &y);
if(x > y) swap(x, y);
ans = y;
for(int i = ; i <= x; i++) {
res = ;
len = g[i].size();
dfs(, , , i);
ans += y-res;
}
printf("%lld\n", ans);
}
}
HDU - 2814 Visible Trees的更多相关文章
- 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(容斥定理)
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 2841 Visible Trees(莫比乌斯反演)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可 ...
- 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
/** 大意: 求[1,m], [1,n] 之间有多少个数互素...做了 1695 ,,这题就so easy 了 **/ #include <iostream> #include < ...
- HDU 2841 Visible Trees(容斥)题解
题意:有一块(1,1)到(m,n)的地,从(0,0)看能看到几块(如果两块地到看的地方三点一线,后面的地都看不到). 思路:一开始是想不到容斥...后来发现被遮住的地都有一个特点,若(a,b)有gcd ...
- C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...
随机推荐
- .Net Core爬虫爬取妹子网图片
现在网上大把的Python的爬虫教程,很少看见有用C#写的,正好新出的.Net Core可以很方便的部署到Linux上,就用妹子图做示范写个小爬虫 在C#下有个很方便的类库 HtmlAgilityPa ...
- scrapy--dbmeinv
第一次将自己的爬虫文件与大家分享.豆瓣美女网页图片爬取.比较简单,但很实用.给大家提供思路为主,增强个人的爬虫能力.希望能帮助到大家!!! 好了,让我们进入正题. 先给大家看下成果!!!激励大家赶快行 ...
- POJ:2010-Moo University - Financial Aid
Moo University - Financial Aid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10894 Acce ...
- IIS发布错误记录
1.HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息模块 IIS Web Core 通知 BeginRequ ...
- 分布式爬虫:使用Scrapy抓取数据
分布式爬虫:使用Scrapy抓取数据 Scrapy是Python开发的一个快速,高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘. ...
- Python 探测图片文件类型
Table of Contents 1. 探测图片类型 1.1. python magic 1.2. imghdr 1.3. PIL.Image 探测图片类型 今天遇到一个小问题,如何探测图片的文件类 ...
- 3 破解密码,xshell连接
1.破解root密码 (1)启动电脑,按上下键进入启动菜单界面,选择第二个Red Hat Enterprise Linux Server, with Linux 0-rescue-* (2)按 'e' ...
- BZOJ 3027: [Ceoi2004]Sweet
容斥 #include<cstdio> using namespace std; int a,b,n,m[15]; long long ans=0,mod=2004; long long ...
- 编译IOS下 libpcap1.7.2
1.修改build-libpcap.sh libpcap版本号 2.执行脚本编译 出现问题: 2.发现configure过后,Makefile存在问题 实际configure的-arch armv7s ...
- Eclipse 创建 Java 接口---Eclipse教程第11课
打开新建 Java 接口向导 新建 Java 接口向导可以创建新的 Java 接口.打开向导的方式有: 点击 File 菜单并选择 New > Interface 在 Package Explo ...