比赛链接: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<<'
';
}
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<<'
';
}
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<<'
';
return 0;
}