# AtCoder Beginner Contest 280 题解

## A - Pawn on a Grid

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
int n,m,ans;
char c;
signed main(){
IOS;TIE;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>c;
if(c=='#') ans++;
}
}
cout<<ans<<endl;
return 0;
}


## B - Inverse Prefix Sum

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
int n,a[200005];
signed main(){
IOS;TIE;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cout<<a[i]-a[i-1]<<' ';
cout<<endl;
return 0;
}


## C - Extra Character

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
string s,t;
signed main(){
IOS;TIE;
cin>>s>>t;
for(int i=0;i<t.size();i++){
if(s[i]!=t[i]){
cout<<i+1<<endl;
return 0;
}
}
return 0;
}


## D - Factorial and Multiple

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
int k,n;
map<int,int> mp,mp2;
bool check(int mid){
for(auto i:mp){
int cnt=0,tmp=i.fi;
while(tmp<=mid) cnt+=mid/tmp,tmp*=i.fi;
if(cnt<i.se) return 0;
}
return 1;
}
signed main(){
IOS;TIE;
cin>>k;
int l=1,r=k,ans;
for(int i=2;i*i<=k;i++){
while(k%i==0) mp[i]++,k/=i;
}
if(k>1) mp[k]++;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)) r=mid-1,ans=mid;
else l=mid+1;
}
cout<<ans<<endl;
return 0;
}


## E - Critical Hit

• 扣两点血（ i=n-1 时只能照扣一点算）：

$f_i=f_i+(f_{i+1+(i!=n-1)}+1)\times p\times E$

• 扣一点血：

$f_i=f_i+(f_{i+1}+1)\times(1-p\times E)$

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
#define mod 998244353
using namespace std;
int n,p,f[200005];
int ksm(int x,int b){
int ans=1;
while(b){
if(b&1) ans=ans*x%mod;
x=x*x%mod;
b>>=1;
}
return ans;
}
signed main(){
IOS;TIE;
cin>>n>>p;
int E=ksm(100,mod-2);
f[n]=0;
for(int i=n-1;i>=0;i--){
f[i]+=(f[i+1+(i!=n-1)]+1)*p%mod*E%mod,f[i]%=mod;
f[i]+=(f[i+1]+1)*(1-p*E%mod)%mod,f[i]%=mod;
f[i]+=mod,f[i]%=mod;
}
cout<<f[0]<<endl;
return 0;
}


## F - Pay or Receive

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
#define mod 998244353
using namespace std;
int n,m,q,u,v,w,col[100005],cnt,f[100005];
bool mark[100005],vis[100005];
struct node{
int to,dis;
};
vector<node> a[100005];
void dfs(int x,int c){
col[x]=c,vis[x]=1;
for(int i=0;i<a[x].size();i++){
node tmp=a[x][i];
if(!vis[tmp.to]){
f[tmp.to]=f[x]+tmp.dis;
dfs(tmp.to,c);
}
}
}
signed main(){
IOS;TIE;
cin>>n>>m>>q;
for(int i=1;i<=m;i++){
cin>>u>>v>>w;
a[u].push_back({v,w}),a[v].push_back({u,-w});
}
for(int i=1;i<=n;i++){
if(!vis[i]) dfs(i,++cnt);
}
for(int i=1;i<=n;i++){
for(int j=0;j<a[i].size();j++){
node tmp=a[i][j];
if(f[tmp.to]!=f[i]+tmp.dis) mark[col[i]]=1;
}
}
while(q--){
cin>>u>>v;
if(col[u]!=col[v]) cout<<"nan"<<endl;
else if(mark[col[u]]) cout<<"inf"<<endl;
else cout<<f[v]-f[u]<<endl;
}
return 0;
}


//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
#define mod 998244353
using namespace std;
int n,m,q,u,v,w,fa[200005],f[200005];
bool mark[200005];
int find(int x){
if(x==fa[x]) return x;
int tmp=fa[x];
fa[x]=find(fa[x]);
f[x]+=f[tmp];
return fa[x];
}
void merge(int x,int y,int fx,int fy,int k){
fa[fx]=fy;
f[fx]=f[y]+k-f[x];
mark[fy]|=mark[fx];
}
signed main(){
IOS;TIE;
cin>>n>>m>>q;
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++){
cin>>u>>v>>w;
int fx=find(u),fy=find(v);
if(fx^fy) merge(u,v,fx,fy,w);
else if(f[u]^(f[v]+w)) mark[fx]=1;
}
while(q--){
cin>>u>>v;
int fx=find(u),fy=find(v);
if(fx^fy) cout<<"nan"<<endl;
else if(mark[fx]) cout<<"inf"<<endl;
else cout<<f[u]-f[v]<<endl;
}
return 0;
}

posted @ 2022-12-03 23:04  Binary_Lee  阅读(48)  评论(0编辑  收藏  举报
Title