Visible TreesTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2213    Accepted Submission(s): 908

Problem Description
There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
 
Input
The first line contains one integer t, represents the number of test cases. Then there are multiple test cases. For each test case there is one line containing two integers m and n(1 ≤ m, n ≤ 100000)
 
Output
For each test case output one line represents the number of trees Farmer Sherlock can see.
 
Sample Input
2
1 1
2 3
 
Sample Output
1
5
 
Source
 
其实感觉这个代码写搓了  但是确实是利用队列的一种方式  重点理解dfs容斥方式
传送门 http://www.cnblogs.com/hsd-/p/5008912.html
 
#include<bits/stdc++.h>
using namespace std;
__int64 n;
__int64 a,b;
__int64 slove( __int64 m,__int64 gg)
{
__int64 que[1000];
__int64 a[1000];
memset(que,0,sizeof(que));
memset(a,0,sizeof(a));
__int64 sum=0;
__int64 t=0;
__int64 ss=0;
for(__int64 i=2;i*i<=m;i++)
{
if(m%i==0)
{
que[ss++]=i;
while(m%i==0)
m=m/i;
}
}
if(m>1)
que[ss++]=m;
a[t++]=-1;
for(__int64 i=0;i<ss;i++)
{
int k=t;
for(__int64 j=0;j<k;j++)
{
a[t++]=que[i]*a[j]*(-1);
}
}
for(__int64 i=1;i<t;i++)
sum=sum+gg/a[i];
return sum;
}
int main()
{
while(scanf("%I64d",&n)!=EOF)
{
for(__int64 i=1;i<=n;i++)
{
__int64 re=0;
scanf("%I64d %I64d",&a,&b);
for(__int64 j=1;j<=a;j++)
re+=slove(j,b);
printf("%I64d\n",a*b-re);
}
}
return 0;
}
 
 
 

HDU2841 (队列容斥)的更多相关文章

  1. 【XSY1580】Y队列 容斥

    题目大意 给你\(n,r\),求第\(n\)个不能被表示为\(a^b(2\leq b\leq r)\)的数 \(n\leq 2\times {10}^{18},r\leq 62\) 题解 我们考虑二分 ...

  2. 【hdu4135】【hdu2841】【hdu1695】一类通过容斥定理求区间互质的方法

    [HDU4135]Co-prime 题意 给出三个整数N,A,B.问在区间[A,B]内,与N互质的数的个数.其中N<=10^9,A,B<=10^15. 分析 容斥定理的模板题.可以通过容斥 ...

  3. hdu 5664 Lady CA and the graph(树的点分治+容斥)

    题意: 给你一个有n个点的树,给定根,叫你找第k大的特殊链 .特殊的链的定义:u,v之间的路径,经过题给的根节点. 题解:(来自BC官方题解) 对于求第k大的问题,我们可以通过在外层套一个二分,将其转 ...

  4. HDU 4135 Co-prime (容斥+分解质因子)

    <题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. ...

  5. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. 15ecjtu校赛1006 (dfs容斥)

    Problem Description 在平面上有一个n*n的网格,即有n条平行于x轴的直线和n条平行于y轴的直线,形 成了n*n个交点(a,b)(1<=a<=n,1<=b<= ...

  7. POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)

      题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1 ...

  8. HDU 4059 容斥初步练习

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  9. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

随机推荐

  1. react项目总结

    1.基本框架 1.react+react-router4+redux3.7.2 2.css预编译使用sass 3.数据请求使用axios(原本是使用fetch,结果在ios10下报错) 4.ui组件库 ...

  2. 【Linux 运维】Centos7初始化网络配置

    设置网络 (1)动态获取一个IP地址 #dhclient        系统自动自动获取一个IP地址#ip addr         查看获取的ip地址(2)查看网关,子网掩码 虚拟机编辑>虚拟 ...

  3. mysql 按日期统计

    按年汇总,统计: select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col ...

  4. springMVC怎么改变form的提交方式为put或者delete

    想着练习一下创建restful风格的网站呢,结果发现在jsp页面上并不能灵活使用put和delete提交方式.下面我的解决办法 一. form 只支持post和get两种提交方式,只支持get提交方式 ...

  5. 理解Python中的__builtin__和__builtins__

    以Python 2.7为例,__builtin__模块和__builtins__模块的作用在很多情况下是相同的. 但是,在Python 3+中,__builtin__模块被命名为builtins. 所 ...

  6. leetcode个人题解——#8 string to integer

    第八题 class Solution { public: int myAtoi(string str) { ; ; ; while(str[i] == ' ')i++; if (str[i] == ' ...

  7. WebApi中利用Razor模板引擎来生成html

    在服务器端基于Razor来生成html的一个思路 using System.Web.Mvc; using System.IO; using System.Web.Routing; using Syst ...

  8. HADOOP docker(六):hive简易使用指南

    前言1.hive简介1.1 hive组件与相应功能:1.2 hive的表类型1.3 分区表1.3 分隔符1.4 hive的数据存储2.数据类型2.1 基本数据类型2.1 复杂数据类型2.3 NULL3 ...

  9. 基础数据类型-set

    Set是无序不重复元素的序列,基本功能是删除重复元素和测试成员关系, 创建一个集合可以用set()或者({}),但是创建一个空集合只能用set(): s1 = set() print("s1 ...

  10. centos配置iptables

    第一次配置前消除默认的规则 #这个一定要先做,不然清空后可能会悲剧 iptables -P INPUT ACCEPT #清空默认所有规则 iptables -F #清空自定义的所有规则 iptable ...