本文主要是介绍cf Educational Codeforces Round 39 B. Weird Subtraction Process,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原题:
B. Weird Subtraction Process
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have two variables a and b. Consider the following sequence of actions performed with these variables:
If a = 0 or b = 0, end the process. Otherwise, go to step 2;
If a ≥ 2·b, then set the value of a to a - 2·b, and repeat step 1. Otherwise, go to step 3;
If b ≥ 2·a, then set the value of b to b - 2·a, and repeat step 1. Otherwise, end the process.
Initially the values of a and b are positive integers, and so the process will be finite.
You have to determine the values of a and b after the process ends.
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018). n is the initial value of variable a, and m is the initial value of variable b.
Output
Print two integers — the values of a and b after the end of the process.
Examples
input
12 5
output
0 1
input
31 12
output
7 12
Note
Explanations to the samples:
a = 12, b = 5 a = 2, b = 5 a = 2, b = 1 a = 0, b = 1;
a = 31, b = 12 a = 7, b = 12.
中文:
给你两个数,a和b,如果a和b都是0,结束。否则,如果a大于等于2b,那么a=a-2b,如果b大于等于2a,那么b=b-2a
让你算出最后结果
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;void solve(ll a,ll b)
{if(a==0||b==0){cout<<a<<" "<<b<<endl;return;}if(a>=2*b)solve(a-(a/(2*b)*(2*b)),b);else{if(b>=2*a)solve(a,b-(b/(2*a)*(2*a)));else{cout<<a<<" "<<b<<endl;return;}}}int main ()
{ios::sync_with_stdio(false);ll a,b;while(cin>>a>>b){solve(a,b);}return 0;
}
思路:
简单粗暴的直接做了就完事-_-
这篇关于cf Educational Codeforces Round 39 B. Weird Subtraction Process的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!