题目传送门
1 /*
2 杭电一题(ACM_steps 2.2.4)的升级版,使用到高精度;
3 这次不是简单的猜出来的了,求的是GCD (n, k) == 1 最大的k(1, n/2);
4 1. 若n是奇数,则k = (n-1) / 2;
5 2. 若n是偶数,讨论(n-1)/2 的奇偶性,若不是奇数,则是n/2-2;
6 详细解释(证明):http://www.xuebuyuan.com/1552889.html
7 */
8 #include <cstdio>
9 #include <iostream>
10 #include <algorithm>
11 #include <cstring>
12 #include <string>
13 #include <cmath>
14 using namespace std;
15
16 const int MAXN = 2e3 + 10;
17 const int INF = 0x3f3f3f3f;
18 string s;
19 string ans;
20
21 void div_2(void)
22 {
23 int len = s.length ();
24 int tmp = s[0] - '0';
25 if (tmp > 1) ans += char (tmp/2 + '0');
26 tmp &= 1;
27 for (int i=1; i<len; ++i)
28 {
29 tmp = tmp * 10 + (s[i] - '0');
30 ans += char (tmp/2 + '0');
31 tmp &= 1;
32 }
33 }
34
35 void sub_1(void)
36 {
37 int i = ans.length () - 1;
38 while (ans[i] == '0')
39 {
40 ans[i] = '9'; i--;
41 }
42 ans[i] -= 1;
43 }
44
45 void print(void)
46 {
47 int i = 0;
48 while (ans[i] == '0') i++;
49 for (; i<ans.length (); ++i) cout << ans[i];
50
51 cout << endl;
52 }
53
54 int main(void) //ZOJ 2313 Chinese Girls' Amusement
55 {
56 //freopen ("ZOJ_2313.in", "r", stdin);
57
58 int t;
59 cin >> t;
60 while (t--)
61 {
62 s = ""; ans = "";
63 cin >> s;
64 div_2 ();
65 if ((s[s.length ()-1]-'0') & 1) cout << ans << endl;
66 else
67 {
68 sub_1 ();
69 if ((ans[ans.length ()-1]-'0') & 1) print ();
70 else
71 {
72 sub_1 (); print ();
73 }
74 }
75 if (t) puts ("");
76 }
77
78 return 0;
79 }