Codeforces Round 952 (Div. 4) G. D-Function(思维)
思维题,推出公式用等比数列求和做一下。
1 #define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) 2 #define bug2(x,y) cout<<#x<<" is "<<x<<" "<<#y<<" is "<<y<<endl 3 #define bug(x) cout<<#x<<" is "<<x<<endl; 4 #include<bits/stdc++.h> 5 #define eb emplace_back 6 typedef long long ll; 7 using namespace std; 8 const ll mod = 1e9+7; 9 10 ll fp(ll x, ll y){ 11 ll res = 1; 12 while(y){ 13 if(y&1) res = res * x % mod; 14 x = x * x % mod; 15 y >>= 1; 16 } 17 return res; 18 } 19 20 void solve(){ 21 22 ll l, r, k, x; 23 24 cin >> l >> r >> k; 25 26 if(k >= 10) cout << 0 << endl; 27 else{ 28 29 x = 9 / k + 1; 30 31 ll res = fp(x, r - l); 32 33 ll a1 = fp(x, l); 34 35 res = (a1 * ((res - 1 + mod) % mod) % mod) * fp(x - 1, mod - 2) % mod; 36 37 res = res * (x - 1) % mod; 38 39 cout << res <<endl; 40 41 } 42 43 } 44 45 int main(){ 46 47 IO; 48 int T; 49 cin >> T; 50 while(T--) { 51 solve(); 52 } 53 54 55 }
H题思维很强,做不出来,贴个官方代码记录一下
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n, m, minR, maxR, minC, maxC, sz, ans; vector<int> R, C, freeR, freeC; 5 vector<vector<int>> RC; vector<vector<bool>> vis; vector<vector<char>> A; 6 7 void dfs(int i, int j){ 8 if (i <= 0 or i > n or j <= 0 or j > m or vis[i][j] or A[i][j] == '.') 9 return; 10 11 vis[i][j] = true; 12 13 sz++; 14 minR = min(minR, i); 15 maxR = max(maxR, i); 16 minC = min(minC, j); 17 maxC = max(maxC, j); 18 19 dfs(i - 1, j); 20 dfs(i + 1, j); 21 dfs(i, j - 1); 22 dfs(i, j + 1); 23 } 24 25 void solve(){ 26 cin >> n >> m; 27 28 R.assign(n + 5, 0); 29 C.assign(m + 5, 0); 30 freeR.assign(n + 5, 0); 31 freeC.assign(m + 5, 0); 32 RC.assign(n + 5, vector<int>(m + 5, 0)); 33 vis.assign(n + 5, vector<bool>(m + 5, false)); 34 A.assign(n + 5, vector<char>(m + 5, ' ')); 35 36 for (int i = 1; i <= n; i++){ 37 for (int j = 1; j <= m; j++){ 38 cin >> A[i][j]; 39 40 if (A[i][j] == '.'){ 41 freeR[i]++; 42 freeC[j]++; 43 } 44 } 45 } 46 47 for (int i = 1; i <= n; i++){ 48 for (int j = 1; j <= m; j++){ 49 if (vis[i][j] or A[i][j] == '.') 50 continue; 51 52 // Reset 53 sz = 0; 54 minR = 1e9; 55 maxR = -1e9; 56 minC = 1e9; 57 maxC = -1e9; 58 59 dfs(i, j); 60 61 // Expand by 1 since adjacent cells also connect 62 minR = max(minR - 1, 1); 63 maxR = min(maxR + 1, n); 64 minC = max(minC - 1, 1); 65 maxC = min(maxC + 1, m); 66 67 // Update prefix sums 68 R[minR] += sz; 69 R[maxR + 1] -= sz; 70 71 C[minC] += sz; 72 C[maxC + 1] -= sz; 73 74 RC[minR][minC] += sz; 75 RC[maxR + 1][minC] -= sz; 76 RC[minR][maxC + 1] -= sz; 77 RC[maxR + 1][maxC + 1] += sz; 78 } 79 } 80 81 for (int i = 1; i <= n; i++) 82 R[i] += R[i - 1]; 83 84 for (int i = 1; i <= m; i++) 85 C[i] += C[i - 1]; 86 87 for (int i = 1; i <= n; i++) 88 for (int j = 1; j <= m; j++) 89 RC[i][j] += RC[i - 1][j] + RC[i][j - 1] - RC[i - 1][j - 1]; 90 91 ans = 0; 92 93 for (int i = 1; i <= n; i++) 94 for (int j = 1; j <= m; j++) 95 ans = max(ans, (R[i] + C[j] - RC[i][j]) + (freeR[i] + freeC[j] - (A[i][j] == '.'))); 96 97 cout << ans << "\n"; 98 } 99 100 int main(){ 101 ios_base::sync_with_stdio(0); cin.tie(0); 102 int tc; 103 cin >> tc; 104 105 while (tc--) 106 solve(); 107 }