ACM-ICP 4000 C国际大学生程序设计竞赛北京赛区(2017)网络赛 Bounce
2017-10-10 14:20
330 查看
题目7 : 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
首先计算球经过的路径长度 为L= lcm(m-1,n-1)+1;再减去两倍的走过两遍的点的个数: 为2*( (L/(m-1)-1)*(L/(n-1)-1)/2) = (L/(m-1))*(L/(n-1));
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
using namespace std;
long long m,n;
long long gcd(long long a,long long b)
{
if(b==0)return a;
else return gcd(b,a%b);
}
long long lcm(long long a,long long b)
{
return a/gcd(a,b)*b;
}
int main()
{
while(cin>>m>>n)
{
long long l=lcm(m-1,n-1);
long long a=l/(m-1),b=l/(n-1);
cout<<(l-a*b+a+b)<<endl;
}
return 0;
}
相关文章推荐
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 G.Bounce
- hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
- hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
- hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 G Bounce (规律)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 G题Bounce
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 G题Bounce
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I.Minimum(线段树区间极值+分类讨论)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 A Visiting Peking University
- 【最短路】【Heap-dijkstra】hihocoder 1587 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 J. Typist's Problem
- ACM-ICPC北京赛区2017网络同步赛 (题目6 : Secret Poems)
- 周末训练笔记+ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛+2017 ACM-ICPC 亚洲区(南宁赛区)网络赛(9.23,9.24)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 Minimum
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 题目1 : Visiting Peking University
- 【线段树】hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I. Minimum
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 A. Visiting Peking University
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 A题 Visiting Peking University
- ACM-ICPC北京赛区(2017)网络赛-题目9 : Minimum(线段树)
- 【分类讨论】【计算几何】【凸包】hihocoder 1582 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 E. Territorial Dispute
- 2017 ACM-ICPC 亚洲区(北京赛区)网络赛 - A I