牛客小白月賽98 A~D
A-骰子魔術
簽到不多說
// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
int n,x; cin>>n>>x;
bool ok = false;
for(int i = 1;i <= n; i++)
{
int t; cin>>t;
if(x==t)ok = true;
}
cout<<(ok?"YES":"NO")<<"\n";
return 0;
}
B-最少剩幾個?
思路:奇數+偶數=奇數,奇數*奇數=奇數。那麼優先用奇數和偶數配對,剩下的奇數兩兩配對。
// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
int n; cin>>n;
int even = 0,odd = 0;
for(int i = 1;i <= n; i++)
{
int x; cin>>x;
even += (x%2==0);
odd += (x%2!=0);
}
int res = min(even,odd);
odd -= res;
res += odd/2*2;
cout<<n-res<<'\n';
return 0;
}
C-兩個函式
思路:\(\sum_{i = 1}^{x-1}f(f(i)) = \sum_{i = 1}^{x-1}a(ax) = a^2\sum_{i = 1}^{x-1}x = a^2\dfrac{x(x-1)}{2}\)。取模小心點就行了。
// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 2e5 + 10;
ll qmi(ll a, ll b, ll mod)
{
ll ans = 1 % mod;
while(b)
{
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
int t; cin>>t;
ll inv = qmi(2, mod - 2, mod);
while(t--)
{
ll a,x;
cin>>a>>x;
if(x==1){
cout<<a*x%mod<<"\n";
continue;
}
ll ans = a*a%mod;
ans = ans*x%mod;
ans = ans*(x-1)%mod;
if(ans<0) ans += mod;
ans %= mod;
ans = ans*inv%mod;
cout<<ans%mod<<"\n";
}
return 0;
}
D-切割 01 串 2.0
思路:發現資料範圍很小呀,直接爆搜,加個記憶化。
// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 5e3 + 10;
int n,L,R;
string s;
ll s0[N],s1[N],dp[N][N];
ll dfs(ll l,ll r)
{
ll& res = dp[l][r];
if(res != -1)
return res;
res = 0;
for(int i = l;i < r; i++)
{
ll t = 0;
ll c0 = s0[i]-s0[l-1];
ll c1 = s1[r]-s1[i];
if(abs(c0-c1)>=L && abs(c0-c1)<=R)
t = 1ll + dfs(l,i)+dfs(i+1,r);
res = max(res,t);
}
return res;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
cin>>n>>L>>R;
cin>>s;
s = "?"+s;
for(int i = 1;i <= n; i++)
{
s0[i] = s0[i-1]+(s[i]=='0');
s1[i] = s1[i-1]+(s[i]=='1');
}
memset(dp,-1,sizeof(dp));
cout<<dfs(1,n)<<"\n";
return 0;
}
好像很多人用區間dp。不過也差不多
// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 5e3 + 10;
int n,L,R;
string s;
ll s0[N],s1[N],dp[N][N];
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
cin>>n>>L>>R;
cin>>s;
s = "?"+s;
for(int i = 1;i <= n; i++)
{
s0[i] = s0[i-1]+(s[i]=='0');
s1[i] = s1[i-1]+(s[i]=='1');
}
for(int len = 1;len <= n; len++){
for(int l = 1;l+len-1<=n;l++)
{
int r = l+len-1;
for(int k = l;k < r; k++){
int t1 = s0[k]-s0[l-1];
int t2 = s1[r]-s1[k];
if(abs(t1-t2)>=L && abs(t1-t2)<=R)
{
dp[l][r] = max(dp[l][r],dp[l][k]+dp[k+1][r]+1);
}
}
}
}
cout<<dp[1][n];
return 0;
}