题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2841

题目大意:

N*M的格点上有树,从0,0点可以看到多少棵树。

解题思路:

发现如果A1/B1=A2/B2那么就有一棵树看不到,所以就是找出Ai/Bi有多少种。

如果A,B有大于1的公约数,则A=A'*D B=B'*D,那么A/B=A'/B',也就是存在另外一组数和这种相等,则问题转换成有多少对互质的数。

求1-m中有多少个与1-n中互质的数目,

可以枚举m,每次求1-n中与m互质的数目。(HDU-4135简单版)

初始化:m = 1的时候,答案就是n

 #include<iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + ;
ll a[], tot;
ll gcd(ll a, ll b)
{
return b == ? a : gcd(b, a % b);
}
void init(ll n)//求出n的素因子
{
tot = ;
for(ll i = ; i * i <= n; i++)
{
if(n % i == )
{
a[tot++] = i;
while(n % i == )n /= i;
}
}
if(n != )a[tot++] = n;
}
ll sum(ll m)//求[1, m]中与n互质的个数
{
ll ans = ;
for(int i = ; i < ( << tot); i++)//a数组的子集
{
ll num = ;
for(int j = i; j; j >>= )if(j & )num++;//统计i的二进制中1的个数
ll lcm = ;
for(int j = ; j < tot; j++)
if(( << j) & i)
{
lcm = lcm / gcd(lcm, a[j]) * a[j];
if(lcm > m)break;
}
if(num & )ans += m / lcm;//奇数加上
else ans -= m / lcm;//偶数减去
}
return m - ans;
}
ll n, m;
int main()
{
int T, cases = ;
cin >> T;
while(T--)
{
cin >> n >> m;
//if(n < m)swap(n, m);
ll ans = n;
for(int i = ; i <= m; i++)
{
init(i);
ans += sum(n);
}
cout<<ans<<endl;
}
return ;
}

hdu-2841 Visible Trees---容斥定理的更多相关文章

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

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

  2. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  3. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

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

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

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

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

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

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

  7. HDU 2841 Visible Trees(数论)

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

  8. hdu 2841 Visible Trees 容斥原理

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

  9. hdu 2841 Visible Trees(容斥)

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

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

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

随机推荐

  1. vim命令汇总

    文章首发:http://www.cnblogs.com/sprying/p/3864631.html 上一次学习vim还是快一年了,倒腾了一个月之后就没碰过.现在重新汇总下vim命令. 1.有些命令回 ...

  2. step2: 爬取廖雪峰博客

    #https://zhuanlan.zhihu.com/p/26342933 #https://zhuanlan.zhihu.com/p/26833760 scrapy startproject li ...

  3. bzoj 4540: [Hnoi2016]序列

    Description 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,-,ar- 1,ar.若1≤l≤s≤t≤r≤n,则称 ...

  4. Git中.gitignore, 忽略追踪

    在目录下 创建: .gitignore文件,将不需要被追踪的文件地址, 写在该文件中, 此时git软件就不会追踪列出的文件进行版本同步: windows不允许创建没有文件名的文件,可以用编辑器创建.g ...

  5. [转]weui-wxss WeUI for 小程序 为微信小程序量身设计

    本文转自:https://github.com/weui/weui-wxss/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=tou ...

  6. wxss无法调用本地资源图片

    微信小程序中,wxss中不能调用本地资源图片作为背景图片,奇怪的是在微信开发者工具中可以调用,但是到了真机预览的时候发现并不行,有的电脑上的连微信开发者工具里也不可以调用. 原因在于小程序上传的是代码 ...

  7. C#导入Excel|读取Excel方法

    OleDbConnection读取 /// <summary>       /// 返回Excel数据源       /// </summary>       /// < ...

  8. django通用分页封装

    __author__ = 'Administrator'from django.utils.safestring import mark_safe class Page:    def __init_ ...

  9. 关于sql优化整理一下

    1.where 子句中可以对字段进行 null 值判断吗?        可以,比如 select id from t where num is null 这样的 sql 也是可以的.但是最好不要给数 ...

  10. mysql5.7忘记密码修改方法

    mysql5.7忘记密码修改方法 mysql是开发中最常用的关系数据库之一.一般在安装数据库到时候会自定义root密码,有时候会忘记该密码,这时候需要对数据库进行密码修改. 一.windows下更改m ...