The blocks in the city of Fishburg are of square form.Navenues running south to north andMstreets running east to west bound them. A helicopter took off in the most southwestern
crossroads and flew along the straight line to the most northeastern crossroads. How many blocks did it fly above?
Note.A block is a square of minimum area (without its borders).
Input
The input containsNandMseparated by one or more spaces. 1 <N,M< 32000.
Output
The number of blocks the helicopter flew above.
Samples
Hint
Reference - The blog below explaine in detail, but I seem like not quite get it that much:
http://flickeringtubelight.net/blog/2006/08/a-diagonal-through-a-rectangular-grid-of-squares/
But anyway, it's a good explanation. And it should be benefit to read it, maybe you can come up with your own way to understand this approach.
This is a problem that can be approach by many ways. I want to post it because it is very mathematics-related.
I , too, use that formula to solve it.
My suggestion is: if you really have a hard time to deduct that formula, just check the small examples, like n = 2, m = 3, and n = 5, m = 4 and so on.
Below is my C++ solution:
#include <iostream>
using namespace std;
int GCDcityBlocks(int n, int m)
{
while (m)
{
int a = n % m;
n = m;
m = a;
}
return n;
}
void CityBlocks1139()
{
int n = 0, m = 0;
cin>>n>>m;
n--, m--;
int k = GCDcityBlocks(n, m);
int ans = n/k + m/k - 1;
cout<<ans * k;
}
分享到:
相关推荐
原来只有sqlserver的oracleMicrosoft.ApplicationBlocks.Data 现在oracle版的也有了
Microsoft.ApplicationBlocks.Data.dll
在GotDotNet.ApplicationBlocks.Data AdoHelper 源码的基础上进行封装处理使调用更方便。欢迎访问http://121.18.78.216
包括Microsoft.ApplicationBlocks.Data ,包括数据库。
GotDotNet.ApplicationBlocks.Data应该有不少的人使用吧,对于数据库的访问操作方便不少。提供了SqlServer, Oracle, OleDB, ODBC不同数据库类型的操作。
Intel.Threading.Building.Blocks
ApplicationBlocks是微软开发的一款访问数据库的很好用的程序
专门讲解drupal扩展模块cck和views的英文书籍
脑瘫的康复疗法,适用于儿童脑瘫治疗。Building Blocks.dotxBuilding Blocks.dotxBuilding Blocks.dotxBuilding Blocks.dotxBuilding Blocks.dotxBuilding Blocks.dotx
GodotSharp.BuildingBlocks 用于启动新Godot项目的GitHub模板: 设计与Godot master分支兼容从Visual Studio运行之前,请在编辑器中打开project.godot用法通过Godot项目设置重命名游戏重命名sln和csproj(通过Visual...
Built-In Building Blocks.dotx office2016 .
Building Blocks.dotx为word2007的页码模板,亲测有效,安装方法百度下即可
将.i3blocks.conf的块放置在您自己的.i3blocks.conf配置文件中的.i3blocks.conf 。 将来自config的bindsym命令放入您自己的i3 config并记下mod组合键-它可能会覆盖当前组合! 在主目录中创建(如果尚不存在)名为...
blocks 各种用法 1.使用blocks作为参数 2.全局Blocks 3.函数一样直接调用Blocks 4.作为方法的参数 5.通过typedef提高代码快的可读性
blocks.css :hot_beverage: 用块向页面添加一些尺寸 :rocket: 您可以在查看示例并开始使用blocks.css 。 文档中的blocks.css UI如下所示。 首先将blocks.css样式表添加到页面的<head> : < link rel =...
blocks加密解密.rar,blocks加密解密.rar