#1584 : Bounce

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

For Argo, it is very interesting watching a circle bouncing in a rectangle.

As shown in the figure below, the rectangle is divided into N×M grids, and the circle fits exactly one grid.

The bouncing rule is simple:

1. The circle always starts from the left upper corner and moves towards lower right.

2. If the circle touches any edge of the rectangle, it will bounce.

3. If the circle reaches any corner of the rectangle after starting, it will stop there.

Argo wants to know how many grids the circle will go through only once until it first reaches another corner. Can you help him?

输入

The input consists of multiple test cases. (Up to 105)

For each test case:

One line contains two integers N and M, indicating the number of rows and columns of the rectangle. (2 ≤ N, M ≤ 109)

输出

For each test case, output one line containing one integer, the number of grids that the circle will go through exactly once until it stops (the starting grid and the ending grid also count).

样例输入
2 2
2 3
3 4
3 5
4 5
4 6
4 7
5 6
5 7
9 15
样例输出
2
3
5
5
7
8
7
9
11
39

题目链接:

  http://hihocoder.com/problemset/problem/1584

题目大意:

  一个n*m的格子图,一个球从(1,1)开始延右下45°走,遇到边就反弹,直到走到四个角之一停止,问一路上只经过1次的格子数。

  (n,m<=109)

题目思路:

  【数学规律】

  首先,由于反弹特性,我把球从网格中移到网格格线的交点上,于是图成了(n-1)*(m-1)的网格,球为点,左上角为(0,0)右下角为(n-1,m-1)

  然后将反弹操作变换成将网格对称折叠复制,如下图,这样就能知道,最后球停下来的时候一定位于([n-1,m-1],[n-1,m-1])([]为最小公倍数)

  所以我们知道了球一路上经过的格子总数为[n-1,m-1]+1((0,0)也要考虑),令d=gcd(n-1,m-1)

  再看重复经过的点,发现分别是d,2d,3d,...,其中还要扣掉位于边缘处的点(边缘处的点只可能经过一次)

  也就是扣掉(n-1),2(n-1),...,和(m-1),2(m-1),..

  所以总共经过多次的格子数为[n-1,m-1]/d-(n-1)/d-(m-1)/d+1(+1是因为(n-1,m-1)这个点不能算,不会出现重复扣是因为第一次重复的点就是终点)

  最终答案就是[n-1,m-1]+1-([n-1,m-1]/d-(n-1)/d-(m-1)/d+1)

 /****************************************************

     Author : Coolxxx
Copyright 2017 by Coolxxx. All rights reserved.
BLOG : http://blog.csdn.net/u010568270 ****************************************************/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
const double EPS=0.00001;
const int J=;
const int MOD=;
const int MAX=0x7f7f7f7f;
const double PI=3.14159265358979323;
const int N=;
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
LL gcd(LL a,LL b)
{
if(!b)return a;
return gcd(b,a%b);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// for(scanf("%d",&cass);cass;cass--)
// init();
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
while(~scanf("%d",&n))
{
scanf("%d",&m);
n--,m--;
LL d=gcd(n,m);
aans=1LL*n*m/d;
aans=aans-aans/d+n/d+m/d;
printf("%lld\n",aans);
}
return ;
}
/*
// //
*/

hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)的更多相关文章

  1. hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1582 : Territorial Dispute 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In 2333, the C++ Empire and the Ja ...

  2. hihoCoder 1578 Visiting Peking University 【贪心】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1578 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for ...

  3. hihoCoder 1586 Minimum 【线段树】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1586 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2 ...

  4. hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】

    https://hihocoder.com/problemset/problem/1586 线段树操作,原来题并不难..... 当时忽略了一个重要问题,就是ax*ay要最小时,x.y可以相等,那就简单 ...

  5. 【分类讨论】【计算几何】【凸包】hihocoder 1582 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 E. Territorial Dispute

    题意:平面上n个点,问你是否存在一种黑白染色方案,使得对于该方案,无法使用一条直线使得黑色点划分在直线一侧,白色点划分在另一侧.如果存在,输出一种方案. 如果n<=2,显然不存在. 如果所有点共 ...

  6. 【线段树】hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I. Minimum

    题意:给你一个序列(长度不超过2^17),支持两种操作:单点修改:询问区间中最小的ai*aj是多少(i可以等于j). 只需要线段树维护区间最小值和最大值,如果最小值大于等于0,那答案就是minv*mi ...

  7. 【最短路】【Heap-dijkstra】hihocoder 1587 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 J. Typist's Problem

    题意:给你一个串,仅含有a~g,且每个字母只出现最多一次.和一个光标初始位置,以及一个目标串,问你最少要多少的代价变化成目标串. 有五种操作:在光标前添加一个未出现过的字母,代价1. 删除光标前或者光 ...

  8. hihoCoder #1586 : Minimum-结构体版线段树(单点更新+区间最值求区间两数最小乘积) (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1586 : Minimum Time Limit:1000ms Case Time Limit:1000ms Memory Limit:256MB Description You are give ...

  9. ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛

    编号 名称 通过率 通过人数 提交人数 A√水题(队友写的 Visiting Peking University 91% 1122 1228 B— Reverse Suffix Array 57% 6 ...

随机推荐

  1. BZOJ 2720 [Violet 5]列队春游 ——期望DP

    很喵的一道题(我可不是因为看了YOUSIKI的题解才变成这样的) $ans=\sum_{x<=n}\sum_{i<=n} iP(L=i)$ 其中P(x)表示视线为x的概率. 所以只需要求出 ...

  2.  为什么上传文件的表单里要加个属性enctype----摘录

    上传文件的表单中<form>要加属性enctype="multipart/form-data",很多人只是死记硬背知道上传表单要这么写,知其然而不知其所以然.那到底为什 ...

  3. 【bzoj4320】【ShangHai2006 Homework】【并查集+离线处理】

    ShangHai2006 Homework Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 918  Solved: 460[Submit][Statu ...

  4. hdu4035 Maze (树上dp求期望)

    dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) 2.找到出口,走出迷宫 ...

  5. 线程池的使用。好文。mark【http://blog.csdn.net/rwecho/article/details/21157289】

    介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...

  6. SPOJ 4060 A game with probability

    博弈论+dp+概率 提交链接- 题意不是很好懂 Ai 表示剩 i 个石头. A 先手的获胜概率. Bi 表示剩 i 个石头. B先手的获胜概率. 如果想选,对于 Ai: 有 p 的概率进入 Bi−1 ...

  7. Perl语言入门--1--perl的包安装

    A.  手工安装 从CPAN下载了DBI模块1.13版的压缩文件DBI-1.13.tar.gz.假设放在/usr/local/src(一般需要编译安装的软件,都放在此目录下安装). # cd /usr ...

  8. R语言入门视频笔记--2--一些简单的命令

    一.对象 1.列举当前内存中的对象 ls() 2.删除不需要的对象 rm(某对象名称) 3.查看向量长度 length(某向量名称) 4.查看向量类型 mode(某向量名称) 二.函数 1.seq函数 ...

  9. Mac 快速修改 hosts 文件

    sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

  10. Yii 安装学习

    (1)打开yii官方网站: http://www.yiichina.com (2)点击下载,跳转到下载页面: (3)找到从归档文件安装,新手学习,使用[ Yii2的基本应用程序模板]: (4)下载解压 ...