1. 程式人生 > >ZOJ Monthly, March 2018 Solution

ZOJ Monthly, March 2018 Solution

ger tip color lse 兩種 solution $2 scan bbf

A - Easy Number Game

水。

技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define ll long long
 5 #define N 100010
 6 ll arr[N];
 7 int n, m;
 8 
 9 int main()
10 {
11     int t; scanf("%d", &t);
12     while (t--)
13     {
14         scanf("%d%d", &n, &m);
15         for (int
i = 1; i <= n; ++i) scanf("%lld", arr + i); 16 sort(arr + 1, arr + 1 + n); 17 ll res = 0; 18 for (int i = 1; i <= m; ++i) 19 res += arr[i] * arr[2 * m - i + 1]; 20 printf("%lld\n", res); 21 } 22 return 0; 23 }
View Code

B - Lucky Man

題意:判斷大數開根後的奇偶性

思路:牛頓叠代法

技術分享圖片
 1 import java.io.BufferedInputStream;
 2 import java.util.Scanner;
 3 import java.math.*;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         Scanner in = new Scanner(new BufferedInputStream(System.in));
 9         int t = in.nextInt();
10         BigInteger a, x, two; String n;
11 two = BigInteger.valueOf(2); 12 while (t-- != 0) 13 { 14 n = in.next(); 15 a = new BigInteger(n); 16 x = new BigInteger(n.substring(0, n.length() / 2 + 1)); 17 while (a.compareTo(x.multiply(x)) < 0) 18 x = x.add(a.divide(x)).divide(two); 19 if (x.mod(two).compareTo(BigInteger.ZERO) == 0) System.out.println(0); 20 else System.out.println(1); 21 } 22 in.close(); 23 } 24 }
View Code

C - Travel along the Line

題意:一維坐標系中,剛開始位於原點,有$\frac{1}{4}$的概率 坐標 +1 和 -1 有$\frac {1}{2} 的概率 不動$ 求在第n秒的時候恰好到達第m個位置的概率

思路:考慮把一個0拆成兩個0,變成四種操作,這樣四種操作是等概率的,那麽所有的可能性就是 $4^n$ 再考慮符合條件的方案數

可以考慮將m通過坐標變換轉化成正直,那麽一個滿足題意的操作序列肯定是 1 的個數 減去 -1的 個數 恰好為m

那麽我們只需要枚舉1的個數,排列組合一下即可

16說 假如用a 表示 1 的個數 b 表示 -1 的個數 c 表示 0的個數

那麽有$\frac {n!} {a! \cdot b! \cdot c!}$ 但是這裏要考慮 多乘上$2^c$ 因為每個0都有兩種選擇 ,可以是$0_1 或者 是 0_2$

技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define ll long long
 5 #define N 100010
 6 ll MOD = (ll)1e9 +7;
 7 
 8 ll fac[N], Bit[N];
 9 ll qmod(ll base, ll n)
10 {
11     ll res = 1;
12     while (n)
13     {
14         if (n & 1) res = res * base % MOD;
15         base = base * base % MOD;
16         n >>= 1;
17     }
18     return res;
19 }
20 
21 void Init()
22 {
23     fac[0] = 1;
24     Bit[0] = 1;
25     for (int i = 1; i < N; ++i) fac[i] = fac[i - 1] * i % MOD;
26     for (int i = 1; i < N; ++i) Bit[i] = Bit[i - 1] * 2 % MOD;
27 }
28 
29 int n, m;
30 
31 int main()
32 {
33     Init();
34     int t; scanf("%d", &t);
35     while (t--)
36     {
37         scanf("%d%d", &n, &m);
38         if (m < 0) m = -m;
39         ll p = 0, q = qmod(4, n); 
40         for (int i = 0; 2 * i + m <= n; ++i)
41             p = (p + (fac[n] * qmod(fac[i], MOD - 2) %MOD * qmod(fac[i + m], MOD - 2) % MOD * qmod(fac[n - 2 * i - m], MOD - 2) % MOD * Bit[n - 2 * i - m] % MOD)) % MOD;    
42         ll res = p * qmod(q, MOD - 2) % MOD;
43         printf("%lld\n", res);
44     }
45     return 0;
46 }
View Code

D - Machine Learning on a Tree

留坑。

E - Yet Another Tree Query Problem

留坑。

F - And Another Data Structure Problem

留坑。

G - Neighboring Characters

留坑。

H - Happy Sequence ZOJ

題意:用1-n的數,每個數可以用無限次,組成長度為m的序列,求有多少個序列滿足 $gcd(b_i, b_{i +1}) = b_{i}$

思路:考慮枚舉序列裏面不同的數的個數,根據題目範圍,最多有10個不同的數,然後隔板法求方案數

技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define ll long long
 5 long long f[15];
 6 long long C[15];
 7 long long MOD;
 8 int p[15];
 9 int n, m;
10 
11 void dp(int t) {
12     int j;
13     j = 2;
14     while (p[t - 1] * j <= n) {
15         p[t] = p[t - 1] * j;
16         f[t]++;
17         dp(t + 1);
18         j++;
19     }
20 }
21 
22 ll qmod(ll base, ll n)
23 {
24     ll res = 1;
25     while (n)
26     {
27         if (n & 1) res = res * base % MOD;
28         base = base * base % MOD;
29         n >>= 1;
30     }
31     return res;
32 }
33 
34 int main()
35 {
36     int i, j, t;
37     long long ans;
38     scanf("%d", &t);
39     MOD = 1000000007;
40     while (t--) {
41         scanf("%d %d", &n, &m);
42         memset(f, 0, sizeof f);
43         memset(p, 0, sizeof p);
44         ans = 0;
45         C[0] = 1;
46         for (i = 1; i <= 11 ; ++i)
47             C[i] = (C[i - 1] * (m - i) % MOD * qmod(i, MOD - 2)) % MOD;
48         for (i = 1; i <= n; ++i) {
49             p[1] = i;
50             ++f[1];
51             dp(2);
52         }
53         for (i = 1; i <= 11; ++i) {
54             ans = (ans + f[i] * C[i - 1] % MOD) % MOD;
55         }
56         printf("%lld\n",ans);
57     }
58     return 0;
59 }
View Code

I - Your Bridge is under Attack

留坑。

J - Super Brain

水。

技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define N 100010
 5 int n;
 6 int cnt[N * 10], a[N], b[N];
 7 
 8 int main()
 9 {
10     int t; scanf("%d", &t);
11     while (t--)
12     {
13         scanf("%d", &n);
14         for (int i = 1; i <= n; ++i) scanf("%d", a + i);
15         for (int i = 1; i <= n; ++i) scanf("%d", b + i);
16         memset(cnt, 0, sizeof cnt);
17         for (int i = 1; i <= n; ++i) ++cnt[a[i]];
18         int res = 0;
19         for (int i = 1; i <= n; ++i) if (cnt[b[i]] == 1) 
20         {
21             res = b[i];
22             break;
23         }
24         printf("%d\n", res);
25     }
26     return 0;
27 }
View Code

ZOJ Monthly, March 2018 Solution