-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr1c_a_2014_part_elf.cpp
116 lines (95 loc) · 1.87 KB
/
r1c_a_2014_part_elf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// -------------------------------------
// STATUS: OK SMALL, OK LARGE, DONE
// Part Elf
// -------------------------------------
#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#define _USE_MATH_DEFINES
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf = 1 << 30;
FILE *fin, *fout;
inline char my_readChar() { char c; do { fscanf(fin, "%c", &c); } while(c == '\r' || c == '\n'); return c; }
// Add extra defines
//#define N 51
ll p, q;
ll sol;
ll euclid(ll a, ll b) {
ll c;
while (b) {
c = a % b;
a = b;
b = c;
}
return a;
}
void solve(int problemIdx) {
ll cmmdc = euclid(p, q);
p /= cmmdc;
q /= cmmdc;
while(p > 1ll && q % 2ll == 0) {
p /= 2ll;
q /= 2ll;
}
printf("problem: %d p/q=%lld/%lld\n", problemIdx, p, q);
sol = -1ll;
if(p == 1ll) {
ll cnt = 0ll;
while(q % 2ll == 0 && q != 1ll) {
q /= 2ll;
cnt ++;
}
if(q == 1ll && cnt <= 40ll) {
sol = cnt;
}
}
}
void read(int problemIdx) {
fscanf(fin, "%lld/%lld", &p, &q);
}
void write(int problemIdx) {
if(sol != -1ll) {
fprintf(fout, "%lld\n", sol);
}
else {
fprintf(fout, "impossible\n", sol);
}
}
int main(int argc, char** argv) {
fin = fopen("input.txt", "rt");
fout = fopen("output.txt", "wt");
//FILE *fout = stdout;
int problemCount;
fscanf(fin,"%d", &problemCount);
for(int problemIdx = 1; problemIdx <= problemCount; problemIdx++) {
read(problemIdx);
solve(problemIdx);
fprintf(fout, "Case #%d: ", problemIdx);
write(problemIdx);
}
fclose(fin);
fclose(fout);
return 0;
}