题意: 

  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的更多相关文章

  1. HDU 2841 Visible Trees 数论+容斥原理

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

  2. HDU 2841 Visible Trees(数论)

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

  3. hdu 2841 Visible Trees 容斥原理

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

  4. HDU 2841 Visible Trees(容斥定理)

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

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

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

  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

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

  8. HDU 2841 Visible Trees(容斥)题解

    题意:有一块(1,1)到(m,n)的地,从(0,0)看能看到几块(如果两块地到看的地方三点一线,后面的地都看不到). 思路:一开始是想不到容斥...后来发现被遮住的地都有一个特点,若(a,b)有gcd ...

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

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

随机推荐

  1. 写给iOS小白的MVVM教程(序)

    这几天,需要重构下部分代码,这里简要记录下.但是涉及的技术要点还是很多,所以分为多个篇章叙述.此教程来源于,并将于应用于实践,不做过多的概念性阐释和争论.每个篇章都会附上实际的可执行的代码.因涉及的技 ...

  2. BZOJ1901: Zju2112 Dynamic Rankings(整体二分 树状数组)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9094  Solved: 3808[Submit][Status][Discuss] Descript ...

  3. dubbo 与Spring Cloud 对比

    链接:https://www.zhihu.com/question/45413135/answer/242224410 近期也看到一些分享Spring Cloud的相关实施经验,这对于最近正在整理Sp ...

  4. python基础数据类型之列表,元组操作

    一.列表的索引和切片1.列表的索引列表和字符串一样样拥有索引 lst = ["a","b","c"] print(lst[0]) # 获取第 ...

  5. Linux实战教学笔记15:用户管理初级(下)

    第十四节 用户管理初级(下) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,用户查询相关命令id,finger,users,w,who,last,lastlog,gr ...

  6. Docker自学纪实(二)Docker基本操作

    安装docker 以CentOS7为例: 安装:yum -y install docker 启动:systemctl start docker 设置开机自启:systemctl enable dock ...

  7. Gson转Map时,Int会变成double解决方法

    package com.cdy.demo; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; ...

  8. ElasticSearch 之 Client

    在使用ElasticSearch的时候,我们需要与Cluster通信,Java版本的API提供了几种方式来构造Client,进而通过Client操作Cluster.   1)使用Node与clusto ...

  9. [原]sencha touch之表单二(注册页面)

    接着上一篇的登陆页面,来一个最简单的注册页面,几乎包含了常用的field Ext.application({ id:'itKingApp', launch:function(){ var formPa ...

  10. SpringMVC---web.xml配置详解

    web.xml中需要配置的内容 1.配置监听器<listener> 它有两个监听器: 1). <!--配置文件加载监听器--> <listener> <lis ...