# Codeforces Round #663 (Div. 2)
阿新 • • 發佈:2020-08-10
比賽連結:https://codeforces.com/contest/1391
A. Suborrays
題意
輸出長度為n的好排列
思路
從小到大輸出一定滿足。
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; int t,n; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>t; while(t--){ cin>>n; for(int i=1;i<=n;i++){ if(i==1) cout<<i; else cout<<' '<<i; } cout<<'\n'; } return 0; }
B. Fix You
題意
求更改最小的格子數使每個格子的東西都能到達終點。
思路
只需要更改最後一行和最後一列即可。
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int N = 200; int t,n,m; string s[N]; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>t; while(t--){ cin>>n>>m; for(int i=0;i<n;i++) cin>>s[i]; int ans=0; for(int i=0;i<s[n-1].length();i++){ if(s[n-1][i]=='D') ans++; } for(int i=0;i<n;i++){ if(s[i][m-1]=='R') ans++; } cout<<ans<<'\n'; } return 0; }
C. Cyclic Permutations
題意
求長度為n的序列中有環的個數。
思路
模擬發現 \(ans=n!-2^{n-1}\)
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int N = 1e6+5; const ll mod = 1e9+7; int t,n,m; ll s[N]; ll ksm(ll a,ll b){ ll res=1; while(b){ if(b&1) res=res*a%mod; b>>=1; a=a*a%mod; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n; s[0]=s[1]=1; for(int i=2;i<=n;i++) s[i]=(i*s[i-1])%mod; ll ans=s[n]-ksm(2,n-1); cout<<(ans%mod+mod)%mod<<'\n'; return 0; }