题意: 

  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. git(osChina上分支的使用)

    使用osChina分支的创建分为两种 1.直接在osChina上创建 需要pull否则查看git的状态是不包含改分支的; git pull <git地址/git简称> <分支名> ...

  2. .NET向WebService传值为decimal、double、int、DateTime等非string类型属性时,服务器端接收不到数据的问题

    最近在做CRM项目时,使用C#调用SAP PI发布的WebService服务时遇到的问题: 向WebService传值为decimal.double.int.DateTime等非string类型数据时 ...

  3. 查询删除的SAP凭证

    标准报表查询:RSSCD100 函数模块:CHANGEDOCUMENT_DISPLAY, Display Change Documents 数据表查询:CDHDR, Change document h ...

  4. mysql 1055 的错误

    1.Err1055,出现这个问题往往是在执行sql语句时候,在最后一行会出现这个问题. [Err] 1055 - Expression #1 of ORDER BY clause is not in ...

  5. python__基础 : 异常处理与自定义异常

    异常处理方法一般为: try: ------code----- except Exception as e: # 抛出异常之后将会执行 print(e) else: # 没有异常将会执行 print( ...

  6. B1071 小赌怡情 (15分)

    B1071 小赌怡情 (15分) 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计 ...

  7. POJ:3045-Cow Acrobats

    Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6253 Accepted: 2345 Descript ...

  8. [BZOJ1045] [HAOI2008] 糖果传递 (中位数)

    Description 题目链接 Solution 这题跟数列的中位数有关, 具体证明见刘汝佳的蓝皮书里 Code #include <cstdio> #include <algor ...

  9. pyplot基础图表函数概述

    pyplot饼图的绘制 pyplot直方图的绘制 极坐标图的绘制

  10. NumPy库入门

    ndarray数组的元素类型 ndarray数组的创建 ndarray数组的操作 ndarray数组的运算