1. 程式人生 > 其它 >江西省賽題解 A B H J K L

江西省賽題解 A B H J K L

連結

A、Mio visits ACGN Exhibition

 1 /*
 2 f[i][j][k] : 從(1, 1) -> (i, j)的經過k個0的方案數:
 3 {
 4 a[i][j] = 0 : f[i][j - 1][k - 1] + f[i - 1][j][k - 1]
 5 a[i][j] = 1 : f[i][j - 1][k] + f[i - 1][j][k]
 6 }
 7 至多經過 n + m - 1個 0 或 1,
 8 所以只用一維就可以記錄0的個數,1的個數可以反推
 9 */
10 #include <bits/stdc++.h>
11 using namespace
std; 12 13 const int N = 510; 14 const int M = 1010; 15 const int mod = 998244353; 16 int n, m, p, q; 17 int a[N][N]; 18 int f[2][N][M]; 19 20 void accept(){ 21 cin >> n >> m >> p >> q; 22 for(int i = 1; i <= n; ++i){ 23 for(int j = 1; j <= m; ++j){ 24 cin >> a[i][j];
25 } 26 } 27 28 if(a[1][1] == 1) f[1][1][0] = 1; 29 else f[1][1][1] = 1; 30 31 for(int i = 1; i <= n; ++i){ 32 for(int j = 1; j <= m; ++j){ 33 if(i == 1 && j == 1) continue; 34 if(a[i][j]){ 35 for(int k = 0; k < M; ++k){
36 f[1][j][k] = (f[1][j - 1][k] + f[0][j][k]) % mod; 37 } 38 }else{ 39 f[1][j][0] = 0; // 這裡需要清0 因為會重複計算貢獻 40 for(int k = 1; k < M; ++k){ 41 f[1][j][k] = (f[1][j - 1][k - 1] + f[0][j][k - 1]) % mod; 42 } 43 } 44 } 45 for(int j = 1; j <= m; ++j){ 46 for(int k = 0; k < M; ++k){ 47 f[0][j][k] = f[1][j][k]; 48 } 49 } 50 } 51 52 int ans = 0; 53 for(int i = p; i <= n + m - 1 - q; ++i){ 54 ans = (ans + f[0][m][i]) % mod; 55 } 56 57 cout << ans; 58 } 59 60 signed main(){ 61 ios::sync_with_stdio(false); 62 cin.tie(nullptr); 63 accept(); 64 65 return 0; 66 }

B、 Continued Fraction

 1 #include <bits/stdc++.h>
 2 
 3 #define int long long
 4 using namespace std;
 5 
 6 signed main() {
 7     int t;
 8     cin >> t;
 9     while (t--) {
10         int m, n;
11         cin >> m >> n;
12         int pre ;
13         queue<int> q;
14 
15         while (n > 0 && m >= 0) {
16             pre = m / n;
17             q.push(pre);
18             m = m - pre * n;
19             swap(m, n);
20 
21         }
22         cout << q.size() - 1 << ' ';
23         while (!q.empty()) {
24             cout << q.front() << ' ';
25             q.pop();
26         }
27         cout << endl;
28     }
29     return 0;
30 }

H、Hearthstone So Easy

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
signed main() {
    int t;
    cin >> t;
    while(t--) {
        int a, b;
        cin >> a >> b;
        if(a == 1) {
            cout << "freesin" << endl;
            continue;
        }
        if(a - b <= 1) {
            cout << "pllj" << endl;
        } else {
            cout << "freesin" << endl;
        };
    }
    return 0;
}

J、 LRU 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 1e5 + 10;
 5 map<int, int> mp;
 6 set<int> but;
 7 int a[N], n, k;
 8 
 9 bool check(int len) {
10     mp.clear();
11     but.clear();
12     int cnt = 0;
13     queue<int> q; //使用佇列維護存在時間最長的快取
14     for(int i = 1; i <= n; ++i) {
15         q.push(a[i]);
16         if(mp[a[i]]) {
17             cnt++;
18             mp[a[i]]++;
19         } else {
20             mp[a[i]]++;
21             if(but.size() < len) but.insert(a[i]);
22             else {
23                 while(!q.empty()) {
24                     int now = q.front();
25                     q.pop();
26                     mp[now]--;
27                     //只有出現次數為0了,才是要刪除的快取塊
28                     if(mp[now] == 0) {
29                         but.erase(now);
30                         break;
31                     }
32                 }
33             }
34             but.insert(a[i]);
35         }
36     }
37     return cnt >= k;
38 }
39 
40 void accept() {
41     //二分查詢
42     int l = 1, r = n;
43     while(l < r) {
44         int mid = l + r >> 1;
45         if(check(mid)) r = mid;
46         else l = mid + 1;
47     }
48     cout << l;
49 }
50 
51 signed main() {
52     ios::sync_with_stdio(false);
53     cin.tie(nullptr);
54 
55     cin >> n >> k;
56     for(int i = 1; i <= n; ++i) {
57         cin >> a[i];
58         mp[a[i]]++;
59     }
60     
61     //計算最大的擊中數
62     int res = 0;
63     for(int i = 1; i <= n; ++i) {
64         if(mp[a[i]] > 1) res += mp[a[i]] - 1;
65         mp[a[i]] = 0;
66     }
67     if(res < k) {
68         cout << "cbddl";
69         return 0;
70     }
71 
72     accept();
73     return 0;
74 }

K、Many Littles Make a Mickle

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define int long long
 4 #define endl "\n"
 5 
 6 signed main() {
 7     int t;
 8     cin >> t;
 9     while(t--) {
10         int a, b;
11         cin >> a >> b;
12         int sum = 0;
13         for(int i = 1; i <= a; i++) {
14             sum += i * i;
15         }
16         cout << b*sum << endl;
17     }
18     return 0;
19 }

L、It Rains Again

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define int long long
 4 #define endl "\n"
 5 
 6 int a[234567];
 7 signed main() {
 8     int t;
 9     cin >> t;
10     int x, y, x1, y1;
11     int maxx = -1;
12     for(int i = 0; i < t; i++) {
13 
14         cin >> x >> y >> x1 >> y1;
15         maxx = max(maxx, x1);
16         a[x + 1] = 1;
17         a[x1 + 1] = -1;
18     }
19     int cnt = 0;
20 
21     for(int i = 1; i <= maxx; i++) {
22         a[i] += a[i - 1];
23     }
24 
25     for(int i = 1; i <= maxx; i++) {
26         if(a[i]) {
27             cnt++;
28         }
29     }
30 
31     cout << cnt << endl;
32     //cout<<a[100000]<<endl;
33     return 0;
34 }