康拓展开-----两个排列的位置之差
#include <iostream> #include <cstring> #include <cstdio> #define LL long long using namespace std; const int maxn=105; const int mod=1e6+7; int T,n,a[maxn],b[maxn]; LL fac[maxn]; int vis[maxn]; void set(int high) { fac[0]=1; for(int i=1;i<=high;i++) fac[i]=(fac[i-1]*i)%mod; } void deal(int a[],LL &ans) { ans=0; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { int cnt=0; for(int j=1;j<a[i];j++) if(!vis[j]) cnt++; ans=(ans+((cnt*fac[n-i-1])%mod))%mod; vis[a[i]]=1; } } int main() { set(100); int Case=1; cin>>T; while(T--) { cin>>n; for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n;i++) cin>>b[i]; LL p1=0,p2=0,ans=0; deal(a,p1);deal(b,p2); int i=0; for(i=0;i<n;i++) if(a[i]!=b[i]) break; if(a[i]>b[i]) ans=(p1-p2)%mod; else ans=(p2-p1)%mod; cout<<"Case $"<<Case++<<":"<<endl<<(ans+mod)%mod<<endl; } return 0; } #include <iostream> #include <cstring> #include <cstdio> #define LL long long using namespace std; const int maxn=105; const int mod=1e6+7; int T,n,a[maxn],b[maxn]; LL fac[maxn]; int vis[maxn]; void set(int high) { fac[0]=1; for(int i=1;i<=high;i++) fac[i]=(fac[i-1]*i)%mod; } void deal(int a[],LL &ans) { ans=0; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { int cnt=0; for(int j=1;j<a[i];j++) if(!vis[j]) cnt++; ans=(ans+((cnt*fac[n-i-1])%mod))%mod; vis[a[i]]=1; } } int main() { set(100); int Case=1; cin>>T; while(T--) { cin>>n; for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n;i++) cin>>b[i]; LL p1=0,p2=0,ans=0; deal(a,p1);deal(b,p2); int i=0; for(i=0;i<n;i++) if(a[i]!=b[i]) break; if(a[i]>b[i]) ans=(p1-p2)%mod; else ans=(p2-p1)%mod; cout<<"Case $"<<Case++<<":"<<endl<<(ans+mod)%mod<<endl; } return 0; }