先补前面几题...之后再写题解
A.Good ol' Numbers Coloring
#include <bits/stdc++.h> using namespace std; int gcd(int a,int b){ return b==0? a:gcd(b,a%b); } int main(){ int n; int a,b; cin>>n; while(n--){ cin>>a>>b; if(gcd(a,b)==1) puts("Finite"); else puts("Infinite"); } return 0; }
B. Restricted RPS (这个地方真写的太乱了)
#include <bits/stdc++.h> using namespace std; //ch->int ; int trans(char ch){ if(ch=='R') return 0; else if(ch=='P') return 1; else if(ch=='S') return 2; } //int->ch ; char transs(int x){ if(x==0) return 'R'; else if(x==1) return 'P'; else if(x==2) return 'S'; } //ch (a) - ch (b) ; char kill(char ch){ if(ch=='R') return 'P'; else if(ch=='P') return 'S'; else if(ch=='S') return 'R'; } //int(a) - int(b) int kills(int x){ if(x==0) return 1; else if(x==1) return 2; else if(x==2) return 0; } int main(){ int t; cin>>t; string s; while(t--){ int n; cin>>n; int a[4] = {0}; int b[4] = {0}; int match[4] = {0}; int res[4] = {0}; //记录a的所有部分 for(int i=0;i<3;i++){ cin>>a[i]; } cin>>s; for(int i=0;i<(int)s.size();i++){ //对应b的石头尖子步的个数 b[trans(s[i])]++; } int ans = 0; //得到res与match值 for(int i=0;i<3;i++){ //获得a,b最多匹配个数 match[i] = min(b[i],a[kills(i)]); //未匹配个数 res[i] = a[kills(i)] - match[i]; ans += match[i]; } if(ans>=(n+1)/2){ puts("YES"); for(int i=0;i<(int)s.size();i++){ if(match[trans(s[i])]>0){ printf("%c",transs(kills(trans(s[i])))); match[trans(s[i])]--; }else{ for(int i=0;i<3;i++){ if(res[i]>0) { printf("%c",transs(kills(i))); res[i]--; break; } } } } printf(" "); }else{ puts("NO"); } } }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5+5; const int mod=1e9+7; long long dp[maxn+5]; void init(){ dp[1] = 1; dp[2] = 2; for(int i=3; i<=maxn; i++){ dp[i] = (dp[i-1] + dp[i-2])%mod; //cout<<dp[i]<<endl; } //cout<<dp[maxn-1]<<endl; } string str; int main() { ios::sync_with_stdio(false); cin.tie(0); init(); cin>>str; int len=str.length(); long long ans=1; int i=0; while(i<len){ if(str[i]=='m' || str[i]=='w'){ cout<<"0"<<endl; return 0; } if(str[i]=='n'){ int cntn=0; while(str[i]=='n'){ cntn++; i++; } ans = ans*dp[cntn]%mod; } else if(str[i]=='u'){ int cntu=0; while(str[i]=='u'){ cntu++; i++; } ans = ans*dp[cntu]%mod; } else i++; } cout<<ans<<endl; }
D.D - Shichikuji and Power Grid
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 3000; const int maxm = 5e6+5; int x[maxn],y[maxn],c[maxn],k[maxn]; int px[maxm],py[maxm],p[maxm]; int n,cnt,cnt0,cnt1,num;//边下标,非超级点连接边数,生成树连接边数 int pre[maxn]; struct Edge{ int u,v; ll w; }edge[maxm]; bool cmp(Edge a,Edge b){ return a.w<b.w; } int find(int x){ if(x!=pre[x]) return pre[x] = find(pre[x]); else return pre[x]; } void inse(int u,int v,int w){ edge[++cnt].u = u; edge[cnt].v = v; edge[cnt].w = (ll)w; } bool unite(int x,int y){ int fx = find(x); int fy = find(y); if(fx!=fy){ pre[fx] = fy; num++; return true; } else return false; } ll kruskal(int n){ //先对所有边cost进行排序 sort(edge+1,edge+cnt+1,cmp); int u,v; ll w,ans = 0; for(int i=1;i<cnt+1;i++){ u =edge[i].u,v=edge[i].v,w=edge[i].w; if(unite(u,v)){ if(u==0||v==0) p[++cnt1] = u+v; else px[++cnt0] = u,py[cnt0] = v; ans += w; } if(num==n-1) break; } if(num<n-1) return -1; return ans; } int main(){ cin>>n; for(int i=1; i<=n; i++) cin>>x[i]>>y[i]; for(int i=1; i<=n; i++) cin>>c[i]; for(int i=1; i<=n; i++) cin>>k[i]; for(int i=0; i<=n; i++) pre[i] = i; num = cnt = cnt0 = cnt1 = 0; //对每个结点建立边 for(int i=1; i<=n; i++) for(int j=1;j<=n;j++){ if(i==j) continue; ll w =(ll) (k[i]+k[j])*(abs(x[i]-x[j])+abs(y[i]-y[j])); edge[++cnt] = (Edge){i,j,w}; } //将所有点与超级点建立边,且花费为建站的费用 for(int i=1; i<=n; i++) { inse(0,i,c[i]); inse(i,0,c[i]); } cout<<kruskal(n+1)<<endl; cout<<cnt1<<endl; for(int i=1; i<=cnt1; i++){ if(i==cnt1) cout<<p[i]<<endl; else cout<<p[i]<<" "; } cout<<cnt0<<endl; for(int i=1; i<=cnt0; i++) cout<<px[i]<<" "<<py[i]<<endl; }