ABC412题解A to F
A
点击查看代码
#include <stdio.h>
int n, ans;
int main() {
scanf("%d", &n);
for (int i = 1, a, b; i <= n; i++) {
scanf("%d %d", &a, &b);
if (a < b) ans++;
}
printf("%d", ans);
return 0;
}
B
点击查看代码
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char s[101], t[101];
bool vis[101];
int main() {
scanf("%s %s", s, t);
for (int i = 0; i < strlen(t); i++) vis[t[i] - 'A'] = 1;
for (int i = 1; i < strlen(s); i++)
if (s[i] >= 'A' && s[i] <= 'Z')
if (!vis[s[i - 1] - 'A']) {
printf("No");
return 0;
}
printf("Yes");
return 0;
}
C
点击查看代码
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char s[101], t[101];
bool vis[101];
int main() {
scanf("%s %s", s, t);
for (int i = 0; i < strlen(t); i++) vis[t[i] - 'A'] = 1;
for (int i = 1; i < strlen(s); i++)
if (s[i] >= 'A' && s[i] <= 'Z')
if (!vis[s[i - 1] - 'A']) {
printf("No");
return 0;
}
printf("Yes");
return 0;
}
D
点击查看代码
#include <stdio.h>
#include <set>
int n, m, d[10], ans = 1e9;
bool flag[10][10];
void dfs(int i, int u, int v, int cnt, int ecnt)
{
if (ecnt > n || ecnt + ((n * (n - 1) >> 1) - i) < n)
return;
if (v == n + 1)
v = 1 + ++u;
if (u == n)
{
for (int i = 1; i <= n; ++i)
if (d[i] != 2)
return;
if (cnt < ans)
ans = cnt;
return;
}
++d[u];
++d[v];
dfs(i + 1, u, v + 1, cnt + 1 - flag[u][v], ecnt + 1);
--d[u];
--d[v];
dfs(i + 1, u, v + 1, cnt + flag[u][v], ecnt);
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1, u, v; i <= m; ++i)
{
scanf("%d%d", &u, &v);
if (u > v)
u ^= v ^= u ^= v;
flag[u][v] = true;
}
dfs(0, 1, 2, 0, 0);
printf("%d\n", ans);
return 0;
}
E
点击查看代码
#include<bits/stdc++.h>
using namespace std;
using ll =long long;
const ll NM=1e7;
bool P[NM+1];
void init(){
for(int i=0;i<=NM;i++)P[i]=1;
P[0]=P[1]=0;
for(int i=0;i<=NM;i++){
if(P[i])for(int j=i*2;j<=NM;j+=i)P[j]=0;
}
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
init();
ll L,R;
cin>>L>>R;
if(L==R){
cout<<1<<"\n";
return 0;
}
L++;
vector<ll> C(R-L+1,0);
for(int i=0;i<=NM;i++){
if(P[i]){
ll f=((L-1)/i*i)+i;
while(f<=R){
ll id=f-L;
f+=i;
if(C[id]==-1){
continue;
}
else if(C[id]==0)C[id]=i;
else C[id]=-1;
}
}
}
ll an=1;
for(int i=0;i<=R-L;i++){
ll d=i+L;
if(C[i]==0){
an++;
}
else if(C[i]>0){
while(d%C[i]==0)d/=C[i];
if(d==1){
an++;
}
}
}
cout<<an<<"\n";
}
F
点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define rep(x,l,r) for(int x=l;x<=r;x++)
#define per(x,r,l) for(int x=r;x>=l;x--)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
const int MOD=998244353,INF=1e9;
const ll LNF=1e18;
int power(int a,int k,int mod=MOD) {
a%=mod,k%=mod-1; if(k<0) k+=mod-1;
int res=1; while(k) {
if(k&1) res=1ll*res*a%mod;
a=1ll*a*a%mod; k>>=1;
} return res;
}
void add(int &x,int y) {x=(x+y>=MOD?x+y-MOD:x+y);}
void sub(int &x,int y) {x=(x>=y?x-y:x-y+MOD);}
int gcd(int x,int y) {return y?gcd(y,x%y):x;}
void chkmax(int &x,int y) {x<y?x=y:0;}
void chkmin(int &x,int y) {x>y?x=y:0;}
const int N=300300;
int n,m,c;
int a[N],b;
int f[N];
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>c;
rep(i,1,n) cin>>a[i];
m=0; rep(i,1,n) m+=a[i];
b=++a[c];
sort(a+1,a+n+1);
rep(i,1,n) if(a[i]==b) c=i;
int tot=-1,sum=1;
per(i,n,1) {
tot+=a[i];
f[i]=1ll*sum*m%MOD*power(tot,-1)%MOD;
sum=(sum+1ll*f[i]*a[i]%MOD*power(m,-1))%MOD;
// cout<<' '<<i<<' '<<tot<<' '<<sum<<'\n';
}
cout<<f[c]<<'\n';
// rep(i,1,n) cout<<a[i]<<' '; cout<<'\t'<<'\t'<<c<<'\n';
// rep(i,1,n) cout<<f[i]<<' '; cout<<'\n';
return 0;
}
浙公网安备 33010602011771号