Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3131    Accepted Submission(s): 1387

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
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2837 2844 2843 2842 2840 

————————————————————————————————————
题目的 意思是从(1,1)开始有个m*n的矩阵每个整点有一颗树 问从(0,0)看过去可以看到多少克树

思路:说白了就是求[1,m],[1,n]有多少对数互质(不互质的肯定会被挡住) 可以枚举m,在n中找有多少互质的数,可以先把枚举的m质因数的分解,然后利用容斥定理计数
防止超时分解的质因数可以先打个表

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int inf=0x3f3f3f3f; vector<LL>p[100010];
int pr[100010];
int a,b,x,y,k;
LL ans; void init()
{
memset(pr,0,sizeof pr);
for(int i=1;i<=100005;i++) p[i].clear();
for(int i=2;i<=100005;i+=2)
p[i].push_back(2);
for(int i=3;i<=100005;i+=2)
{
if(!pr[i])
{
for(int j=i;j<=100005;j+=i)
{
pr[j]=1;
p[j].push_back(i);
} }
}
} void dfs(int no,int pos,int xx,int cnt)
{
if(pos>=p[no].size())
{
if(cnt==0)
return;
if(cnt%2)
{
ans+=y/xx;
}
else
{
ans-=y/xx;
}
return;
}
dfs(no,pos+1,xx*p[no][pos],cnt+1);
dfs(no,pos+1,xx,cnt);
} int main()
{
init();
int T;
int q=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
LL ans2=0;
for(int i=1; i<=x; i++)
{
ans=0;
dfs(i,0,1,0);
ans2+=y-ans;
}
printf("%lld\n",ans2);
}
return 0;
}

Hdu2841 Visible Trees 2017-06-27 22:13 24人阅读 评论(0) 收藏的更多相关文章

  1. Codeforces 768A Oath of the Night's Watch 2017-02-21 22:13 39人阅读 评论(0) 收藏

    A. Oath of the Night's Watch time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. 全方位分析Objcetive-C Runtime 分类: ios技术 2015-03-11 22:29 77人阅读 评论(0) 收藏

    本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 简介 与Runtime交互 ...

  3. java面试和笔试大全 分类: 面试 2015-07-10 22:07 10人阅读 评论(0) 收藏

    2.String是最基本的数据类型吗? 基本数据类型包括byte.int.char.long.float.double.boolean和short. java.lang.String类是final类型 ...

  4. Struts知识问答 分类: 面试 2015-07-10 22:01 4人阅读 评论(0) 收藏

    1. 简述Struts框架的初始化流程. 答案: 对于采用Struts框架的Web应用,在Web应用启动时就会加载并初始化控制器ActionServlet ActionServlet从struts-c ...

  5. hdu 1039 (string process, fgets, scanf, neat utilization of switch clause) 分类: hdoj 2015-06-16 22:15 38人阅读 评论(0) 收藏

    (string process, fgets, scanf, neat utilization of switch clause) simple problem, simple code. #incl ...

  6. magic矩阵 分类: 数学 2015-07-31 22:56 2人阅读 评论(0) 收藏

    魔方矩阵 魔方矩阵是有相同的行数和列数,并在每行每列.对角线上的和都相等.你能构造任何大小(除了2x2)的魔方矩阵. 1.历史       魔方又称幻方.纵横图.九宫图,最早记录于我国古代的洛书.据说 ...

  7. MATLAB符号运算 分类: 图像处理 2015-07-31 22:53 3人阅读 评论(0) 收藏

    1.符号运算 使用MATLAB可以进行多项式乘除运算,也可以进行因式分解. 例1. 多项式乘除运算(x+3)3 >> syms x; >> expand((x+3)^3) an ...

  8. ListView 分类: WinForm 2014-07-18 22:03 289人阅读 评论(0) 收藏

    一.ListView类(转载) 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLin ...

  9. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

随机推荐

  1. Error starting daemon: error initializing graphdriver: driver not supported

    Error starting daemon: error initializing graphdriver: driver not supported systemctl stop docker rm ...

  2. Python 字符串 (isdigit, isalnum,isnumeric)转

    Python isdigit() 方法检测字符串是否只由数字组成. 语法 isdigit()方法语法: str.isdigit() 参数 无. 返回值 如果字符串只包含数字则返回 True 否则返回 ...

  3. mybatis入门--mybatis和hibernate比较

    mybatis和hibernate的比较 Mybatis和hibernate不同,它不完全是一个ORM框架,因为MyBatis需要程序员自己编写Sql语句,不过mybatis可以通过XML或注解方式灵 ...

  4. spring配置遇到的问题

    1.文档根元素 "beans" 必须匹配 DOCTYPE 根 "null" 这个原因是因为我自动扫描mapping.xml的文件路径设置错误,把它设置成spri ...

  5. LaTeX数学公式大全

    原写于我的洛谷博客(传送门),现搬到这个博客上. 建议去洛谷博客上看,因为两边的编辑器在有些功能上不能通用,所以之后若有删改或新增内容只在洛谷博客上弄,这边就懒得改了. 原本是针对洛谷的编辑器,不过懒 ...

  6. 基于内存,redis,mysql的高速游戏数据服务器设计架构 ZT

    zt  http://www.cnblogs.com/captainl1993/p/4788236.html 1.数据服务器详细设计 数据服务器在设计上采用三个层次的数据同步,实现玩家数据的高速获取和 ...

  7. js中的find(),filter(),has()的用法和区别

    filter():操作当前元素集,删除不匹配的元素,得到一个新的集合 <ul> <li class="a">list item 1</li> & ...

  8. 华为QOS原理及配置

    http://www.tudou.com/programs/view/GWCiHfWC9FI/ FLASH : http://www.tudou.com/v/GWCiHfWC9FI/&reso ...

  9. Likecloud—吃、吃、吃(P1508)

    题目链接:Likecloud-吃.吃.吃 这题的状态非常的自然. 就是ans[i][j]表示从(i,j)出发,能得到的最大能量值. 那么对应每一个点,我们只要选出他能到达的点的最大值,加上自己就行了. ...

  10. NOIP训练测试3(2017081601)

    上一波题还是比较水的吧?[?????] 也许吧! 但时间还是比较紧的,所以我从2.5个小时延长至3个小时了. 不管了,做题不能停,今天继续测试. 水不水自己看,我什么也不说(zhe shi zui h ...