咕咕咕
int main(){
for(scanf("%d",&T);T;--T){
scanf("%d%d",&l,&r);
if(l==r)printf("%d %d %d %d\n",l,l,l,l);
else printf("%lld %lld %lld %lld\n",1ll*r*(r-1),1ll*l,1ll*r,1ll);
// printf("%lld %lld %lld %lld\n",l==r?1ll*l:1ll*r*(r-1),)
}
return 0;
}
分类讨论一下就行了
int main(){
fp(i,1,4)scanf("%d",&a[i]);
fp(i,1,4)if(a[i]==1){
fp(j,1,i-1)a[4+j]=a[j];
fp(j,1,4)a[j]=a[j+i-1];
}
if(a[2]==2&&a[3]==3&&a[4]==4)puts("0");
else if(a[2]==4&&a[3]==3&&a[4]==2)puts("2");
else puts("1");
return 0;
}
我可能是个\(zz\)
枚举哪些数刚好处在不合法位置,然后计算此时的贡献,大力容斥即可
//minamoto
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
typedef long long ll;
const int N=25,M=(1<<16)+5;
ll res,fac[N];int p[N],sz[M],vis[N],Pre[N],suf[N],n,T,lim;
ll calc(R int s){
fp(i,0,n-1)if(s>>i&1)fp(j,i+1,n-1)if((s>>j&1)&&p[i]==p[j])return false;
fp(i,0,n-1)if(s>>i&1)vis[p[i]]=1;
fp(i,1,n)if(vis[i]){
Pre[i]=suf[i]=0;
fp(j,1,i-1)if(!vis[j])Pre[i]+=i-j;
fp(j,i+1,n)if(!vis[j])suf[i]+=j-i;
}
ll res=0;int sum=0,ss=sz[s];
fp(i,1,n)if(!vis[i])fp(j,i+1,n)if(!vis[j])sum+=j-i;
fp(i,0,n-1)fp(j,i+1,n-1){
if((s>>i&1^1)&&(s>>j&1^1))res+=fac[n-ss-2]*sum*(j-i);
else if(s>>i&1^1)res+=fac[n-ss-1]*suf[p[j]]*(j-i);
else if(s>>j&1^1)res+=fac[n-ss-1]*Pre[p[i]]*(j-i);
else if(p[j]<p[i])res+=fac[n-ss]*(j-i)*(p[i]-p[j]);
}
fp(i,1,n)vis[i]=0;
return res;
}
int main(){
// freopen("testdata.in","r",stdin);
fp(i,1,65535)sz[i]=sz[i>>1]+(i&1);
fac[0]=1;fp(i,1,16)fac[i]=fac[i-1]*i;
for(scanf("%lld",&T);T;--T){
scanf("%d",&n),res=0,lim=(1<<n);
fp(i,0,n-1)scanf("%d",&p[i]);
// printf("%d\n",calc(3));
fp(i,0,lim-1)(sz[i]&1)?res-=calc(i):res+=calc(i);
printf("%lld\n",res);
}
return 0;
}
原文:https://www.cnblogs.com/yuanquming/p/11186893.html