解题思路:
自己想了好久,没想出什么规律,这也是百度得到的。
比如 用一个矩形来切割,其实应该是圆的。这里边界也得加上,因为首尾其实是相连的
比如4 ,6 把一个矩形切成4份,需要4刀(加上边界),,6份需要6刀
但是有2刀是重复的,就应该把它减去。而2又是4 ,6的最小公约数。
所以就是m+n-gcd(m,n)
ac:
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <string> #include <algorithm> #include <stdlib.h> #include <set> #include <sstream> using namespace std; int gcd(int a,int b){ while(b){ int temp = b; b = a%b; a = temp; } return a; } int main() { int m,n; while(cin>>m>>n){ cout<<m+n-gcd(m,n)<<endl; } }
