【TEST】8.21-Solution-by-wlz
解题报告
\text{T1}\ 数字碰撞 (number.pas\ /\ c\ /\ cpp)
解题思路
好傻逼的模拟题,完全没难度,只需要一位一位的比较,用两个bool数组分别记录两个数每一位上的消除情况,只需要记得特判n=0和BOOM的情况即可。
附上代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
string a, b;
bool visa[1003], visb[1003], flaga, flagb;
int main() {
freopen("number.in", "r", stdin);
freopen("number.out", "w", stdout);
cin>>a>>b;
for(int i=0; i<a.size(); i++) {
if(a[i] < b[i]) visa[i] = 1;
if(a[i] > b[i]) visb[i] = 1;
}
int x = 0, y = 0;
while (x < a.size() && visa[x] == 1) x++;
if(x == a.size()) printf("BOOM"), flaga = 1;
else {
while (x < a.size() && a[x] == '0') x++;
if(x == a.size()) printf("0");
for(int i=x; i<a.size(); i++)
if(!visa[i]) cout<<a[i];
flaga = 1;
}
printf("\n");
while (y < b.size() && visb[y] == 1) y++;
if(y == a.size()) printf("BOOM");
else {
while (y < b.size() && b[y] == '0') y++;
if(y == b.size()) printf("0");
for(int i=y; i<b.size(); i++)
if(!visb[i]) cout<<b[i];
}
fclose(stdin); fclose(stdout);
return 0;
}
\text{T2}\ 石子合并加强版 (merge.pas\ /\ c\ /\ cpp)
解题思路
解题思路
考虑合法方案数非常复杂,但考虑不合法的方案数相对简单,一本书不合法,两本书不合法,三本书不合法……显然要用容斥原理。
对于n本书,有n!种排列方式,对于一本书不和发的有(n-1)!种排列,对于已经放了的书,考虑还剩的n-m本书中有s本任然可能不合法,那么ans=(n-m)!-C(s,1)(n-m-1)!+C(s,2)(n-m-2)!-C(s,3)(n-m-3)!+…+(-1)sC(s,s)(n-m-s)! =Σ(k=0~s) (-1)kC(s,k)*(n-m-k)!
代码
#include <iostream>
#include <cstdio>
#include <cstdio>
#include <algorithm>
#define mod 1000000007
using namespace std;
int n,m,last[100000],now[100000];
bool vis[100000],flag[100000];
long long ans=(long long)0;
void dfs(int dep){
if(dep==n+1){
ans=(ans+1)%mod;return ;
}
if(vis[dep]) dfs(dep+1);
else{
for(int i=1;i<=n;i++){
if(flag[i] || i==dep) continue;
flag[i]=1;dfs(dep+1);flag[i]=0;
}
}
return ;
}
int main(){
//freopen("problem.in","r",stdin);
//freopen("problem.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) scanf("%d%d",&last[i],&now[i]),vis[last[i]]=1,flag[now[i]]=1;
dfs(1);
printf("%lld\n",ans);
return 0;
}