AtCoder Beginner Contest 300
A - N-choice question
#include<bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
const int N = 1e5+5;
bool vis[N];
vector<int> e[N] ;
int d[N];
int main() {
int n = read() , a = read() , b = read();
for( int x , i = 1 ; i <= n ; i ++ ){
x = read();
if( x == a + b ) cout << i , exit(0);
}
return 0;
}
B - Same Map in the RPG World
看起来似乎很复杂,其实可以直接枚举横着移动多少次,竖着移动多少次就行了。
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);
int n , m;
cin >> n >> m;
vector<string> a(n) , b(n);
for( auto & i : a ) cin >> i;
for( auto & i : b ) cin >> i;
for( int r = 0 ; r < n ; r ++ )
for( int c = 0 ; c < m ; c ++ ){
int f = 1;
for( int i = 0 ; f && i < n ; i ++ )
for( int j = 0 ; f && j < m ; j ++ ){
if( a[i][j] != b[(i+r)%n][ (j+c) % m ] ) f = 0;
}
if( f ) cout << "Yes\n" , exit(0);
}
cout << "No\n";
return 0;
}
C - Cross
暴力枚举点的坐标,暴力枚举大小,然后暴力判断即可。
#include<bits/stdc++.h>
using namespace std;
const int dx[] = {1, 1, -1, -1};
const int dy[] = {-1, 1, -1, 1};
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> g(n);
vector<int> cnt(min(n, m) + 1);
for (auto &i: g) cin >> i;
for (int i = 0, t; i < n; i++)
for (int j = 0; j < m; j++) {
if (g[i][j] == '.') continue;
t = 0;
for (int k = 1, f; k; k++) {
f = 1;
for (int l = 0; l < 4 && f; l++)
if (g[i + dx[l] * k][j + dy[l] * k] == '.') f = 0;
if (f) t = k;
else break;
}
cnt[t]++;
}
for (int i = 1; i < cnt.size(); i++) cout << cnt[i] << " ";
return 0;
}
D - AABCC
首先求一下素数,然后枚举\(a,b\)二分\(c\)就好了。
#include<bits/stdc++.h>
using namespace std;
#define int __int128
typedef long long ll;
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int32_t main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
const int N = 300000;
vector<int> not_prime(N + 1, 0);
vector<int> prime;
not_prime[0] = not_prime[1] = true;
for (int i = 2; i <= N; i++) {
if (not_prime[i]) continue;
prime.push_back(i);
for (int j = i * 2; j <= N; j += i)
not_prime[j] = 1;
}
int n = read(), res = 0;
for (int i = 0, a, b; i < prime.size(); i++) {
a = prime[i] * prime[i];
if (a > n) break;
for (int j = i + 1; j < prime.size(); j++) {
b = prime[j] * a;
if (b > n) break;
int l = j + 1, r = prime.size() - 1, mid, ans = -1;
while (l <= r) {
mid = (l + r) >> 1;
if (b * prime[mid] * prime[mid] <= n) ans = mid, l = mid + 1;
else r = mid - 1;
}
if (ans == -1) break;
res += ans - j;
}
}
cout << (ll) res << "\n";
return 0;
}
E - Dice Product 3
有题可知
\(f(i)=\frac 1 6(f(i)+f(\frac i 2 ) + f(\frac i 3 ) + f(\frac i 4) + f(\frac i 5) + f(\frac i 6) )\)
\(\frac 5 6f(i)=\frac 1 6(f(\frac i 2 ) + f(\frac i 3 ) + f(\frac i 4) + f(\frac i 5) + f(\frac i 6) )\)
\(f(i)=\frac 1 5(f(\frac i 2 ) + f(\frac i 3 ) + f(\frac i 4) + f(\frac i 5) + f(\frac i 6) )\)
其中\(f(1)=1\),如果不能整除的话就是\(0\)
然后直接记忆化搜索一下?
#include<bits/stdc++.h>
using namespace std;
#define int long long
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
const int mod = 998244353, inv = 598946612;
map<int, int> f;
int dp(int n) {
if (f.count(n)) return f[n];
int ans = 0;
for (int i = 2; i <= 6; i++)
if (n % i == 0) ans = (ans + dp(n / i)) % mod;
return f[n] = ans * inv % mod;
}
int32_t main() {
int n;
cin >> n;
f[1] = 1;
cout << dp(n);
return 0;
}
F - More Holidays
记录\(S\)中每一个x
的位置,很容易可以想到相邻的x
一定是最优的,所以直接枚举一下起点就好。但是枚举的是时候实际上只用枚举第一个\(S\)中的位置就可以。然后特判第一个和最后一个的特殊情况。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);
int n , m , k;
string s;
cin >> n >> m >> k >> s;
vector<int> p;
for( int i = 0 ; i < n ; i ++ )
if( s[i] == 'x' ) p.push_back(i);
int cnt = p.size() , t = cnt * m;
if( cnt * m == k ) cout << n * m , exit(0);
int res = 0;
res = max( res , n * ( k / cnt ) + p[k%cnt] );
res = max( res , n * ( k / cnt ) + n - 1 - p[cnt - 1 - k % cnt] );
for( int i = 0 ; i < cnt ; i ++ ){
if( k + i + 1 < t )
res = max( res , p[(k+i+1)%cnt] + n * ((k+i+1) / cnt) - p[i] - 1 );
}
cout << res;
return 0;
}