找规律 SGU 126 Boxes

 

题目地址:http://acm.sgu.ru/problem.php?contest=0&problem=126

 1 /*
 2     找规律,智商不够,看了题解
 3     详细解释:http://blog.csdn.net/ahfywff/article/details/7432524
 4 */
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <cstring>
 8 #include <string>
 9 #include <iostream>
10 #include <algorithm>
11 #include <queue>
12 #include <vector>
13 using namespace std;
14 
15 const int MAXN = 1e5 + 10;
16 const int INF = 0x3f3f3f3f;
17 
18 int GCD(int a, int b)
19 {
20     return b ? GCD (b, a % b) : a;
21 }
22 
23 int main(void)        //SGU 126 Boxes
24 {
25     //freopen ("C.in", "r", stdin);
26 
27     int n, m;
28 
29     while (~scanf ("%d%d", &n, &m))
30     {
31         int ans;
32         if (n == 0 || m == 0)
33         {
34             ans = 0;
35         }
36         else
37         {
38             if (n < m)  swap (n, m);
39             int k = GCD (n, m);
40             int x = n / k;  int y = m / k;
41             if ((x+y) & 1)
42             {
43                 ans = -1;
44             }
45             else
46             {
47                 int tmp = x + y;
48                 ans = 0;
49                 while (tmp > 1)
50                 {
51                     if (tmp % 2 == 0)
52                     {
53                         tmp /= 2;
54                         ans++;
55                     }
56                     else
57                     {
58                         ans = -1;    break;
59                     }
60                 }
61             }
62         }
63 
64         printf ("%d\n", ans);
65     }
66 
67     return 0;
68 }

 

posted @ 2015-04-01 20:29  Running_Time  阅读(190)  评论(0编辑  收藏  举报