Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define N 100010 #define gc getchar() #define rep(i,a,b) for(int i=a;i<=b;i++) #define lint long long #define mod 1000000007 #define debug(x) cerr<<#x<<"="<<x #define sp <<" " #define ln <<endl using namespace std; inline int inn() { int x,ch;while((ch=gc)<'0'||ch>'9'); x=ch^'0';while((ch=gc)>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^'0');return x; } int fac[N],facinv[N],f[N],onc[N],sz[N],p[N],cnt[N]; int cyc_cnt,vis[N],l[N],pre[N],jhs[N],in[N],mi[N],it[N]; inline int fast_pow(int x,int k,int ans=1) { for(;k;k>>=1,x=(lint)x*x%mod) (k&1)?ans=(lint)ans*x%mod:0;return ans; } inline int prelude(int n) { rep(i,fac[0]=1,n) fac[i]=(lint)fac[i-1]*i%mod; facinv[n]=fast_pow(fac[n],mod-2); for(int i=n-1;i>=0;i--) facinv[i]=facinv[i+1]*(i+1ll)%mod; rep(i,mi[0]=1,n) mi[i]=mi[i-1]*2,(mi[i]>=mod?mi[i]-=mod:0); rep(i,f[0]=1,n/2) f[i]=f[i-1]*(2*i-1ll)%mod;return 0; } inline int C(int n,int m) { if(n<0||m<0||n<m) return 0; return (lint)fac[n]*facinv[m]%mod*facinv[n-m]%mod; } int main() { int n=inn();rep(i,1,n) p[i]=inn(),in[p[i]]++; //unsigned int QwQ=1,t=1;rep(i,1,n) QwQ+=p[i]*t,t*=998244353;cout<<QwQ<<endl; for(int i=1,x;i<=n;i++) { for(x=i;!vis[x];x=p[x]) vis[x]=i; if(vis[x]^i) continue; for(cyc_cnt++;!onc[x];x=p[x]) onc[x]=cyc_cnt,sz[cyc_cnt]++; } // debug(cyc_cnt)ln; // rep(i,1,cyc_cnt) debug(i)sp,debug(sz[i])ln; // rep(i,1,n) debug(i)sp,debug(onc[i])sp,debug(in[i])ln;cerr ln; memset(vis,0,sizeof(int)*(n+1)); for(int i=1,x,c;i<=n;i++) if(!in[i]) { for(x=i,c=0;!onc[x];vis[x]=1,x=p[x],c++) if(vis[x]) return !printf("0\n"); if(vis[x]) return !printf("0\n");vis[x]=1,l[x]=c; } rep(i,1,n) if(onc[i]&&l[i]) jhs[onc[i]]=1; rep(i,1,cyc_cnt) if(!jhs[i]) cnt[sz[i]]++; // rep(i,1,cyc_cnt) debug(i)sp,debug(jhs[i])sp,debug(sz[i])ln; // rep(i,1,n) debug(i)sp,debug(l[i])sp,debug(cnt[i])ln;cerr ln; prelude(n);int ans=1; for(int i=1,k,t,s,v;i<=n;i++) if(cnt[i]) { rep(j,it[0]=1,cnt[i]/2) it[j]=(lint)it[j-1]*i%mod; for(k=cnt[i],t=0,s=0;t<=cnt[i]/2;t++) v=(lint)C(k,2*t)*f[t]%mod*it[t]%mod, (((i&1)&&i>1)?v=(lint)v*mi[k-2*t]%mod:0), s+=v,(s>=mod?s-=mod:0); ans=(lint)ans*s%mod; } for(int i=1,x,c;i<=n;i++) if(onc[i]&&l[i]) { for(x=i,c=1;!l[p[x]];x=p[x],c++);pre[p[x]]=c; } // rep(i,1,n) debug(i)sp,debug(pre[i])ln; rep(i,1,n) if(onc[i]&&l[i]) { if(l[i]>pre[i]) return !printf("0\n"); else if(l[i]<pre[i]) ans=ans*2,(ans>=mod?ans-=mod:0); } return !printf("%d\n",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define mo 1000000007 using namespace std; int inn[1000100],a[1000100],num[1000100],wei[1000100],mp[1000100],f[1000100]; bool vis[1000100]; int work(int l,int n) { f[0]=1;f[1]=1+(l&1)-(l==1); for (int i=2;i<=n;i++) f[i]=((1+(l&1)-(l==1))*f[i-1]+(long long)l*(i-1)%mo*f[i-2])%mo; return f[n]; } int main() { //freopen("in.txt","r",stdin); int n;scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d",&a[i]);inn[a[i]]++; if (inn[a[i]]>2) {printf("0\n");return 0;} } for (int i=1;i<=n;i++) if (!inn[i]) { int js=0,now=i; while (inn[now]!=2) {vis[now]=true;now=a[now];js++;} num[now]=js;//cerr<<now<<' '<<js<<endl; } int ans=1; for (int i=1;i<=n;i++) if (!vis[i]) { int len=0,now=i; while (!vis[now]) {wei[++len]=now;vis[now]=true;now=a[now];}//cerr<<len<<endl; if (i!=now) {printf("0\n");return 0;} int lst=1;for (int j=1;j<=len;j++) if (num[wei[j]]) lst=j-len;cerr<<lst<<endl; if (lst==1) {mp[len]++;continue;} for (int j=1;j<=len;j++) if (num[wei[j]]) { if (j-lst>num[wei[j]]) ans=ans*2%mo; if (j-lst<num[wei[j]]) {printf("0\n");return 0;} lst=j; } } for (int i=1;i<=n;i++) if (mp[i]) ans=(long long)ans*work(i,mp[i])%mo; printf("%d\n",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #include<set> #include<bitset> #include<map> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) #define fi first #define se second using namespace std; typedef long long LL; typedef double db; int get(){ char ch; while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-'); if (ch=='-'){ int s=0; while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0'; return -s; } int s=ch-'0'; while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0'; return s; } const int N = 2e+5+5; const int mo = 1e+9+7; int cnt[N]; LL inv[N],js[N],miv[N]; int n; int a[N]; int fa[N]; struct edge{ int x,nxt; }e[N*2]; int h[N],tot,key[N],k; bool bz[N]; int pt[N],u; int siz[N]; int pre[N],suf[N]; bool dfs(int x){ bz[x]=1; siz[x]=1; int s=0; for(int p=h[x];p;p=e[p].nxt) if (!bz[e[p].x]){ s++; if (dfs(e[p].x))return 1; siz[x]+=siz[e[p].x]; } return s>1; } LL quickmi(LL x,LL tim){LL ans=1;for(;tim;tim/=2,x=x*x%mo)if (tim&1)ans=ans*x%mo;return ans;} int getfather(int x){return fa[x]==x?x:fa[x]=getfather(fa[x]);} void inse(int x,int y){ e[++tot].x=y; e[tot].nxt=h[x]; h[x]=tot; } int main(){ n=get(); inv[0]=js[0]=inv[1]=js[1]=miv[0]=1; miv[1]=(mo+1)/2; fo(i,2,n){ inv[i]=1ll*(mo-mo/i)*inv[mo%i]%mo; js[i]=js[i-1]*i%mo; miv[i]=miv[i-1]*miv[1]%mo; } fo(i,2,n)inv[i]=inv[i]*inv[i-1]%mo; fo(i,1,n)fa[i]=i; fo(i,1,n){ int x=a[i]=get(); inse(x,i); int tx=getfather(x),ti=getfather(i); if (tx!=ti)fa[ti]=tx; else key[++k]=i; } int ans=1; fo(i,1,k){ int x=key[i]; bz[pt[u=1]=x]=1; for(x=a[x];x!=key[i];x=a[x])bz[pt[++u]=x]=1; fo(j,1,u)if (dfs(pt[j])){ans=0;break;}if (!ans)break; int ps=0; fd(j,u,1){ps=max(0,ps-1);if (siz[pt[j]]>1){if (ps){ans=0;break;}ps=siz[pt[j]]-1;}}if (!ans)break; fd(j,u,1){ps=max(0,ps-1);if (siz[pt[j]]>1){if (ps){ans=0;break;}ps=siz[pt[j]]-1;}}if (!ans)break; int mx=0; fo(j,1,u)mx=max(mx,siz[pt[j]]); if (mx==1)cnt[u]++; fo(j,1,u)pre[j]=pre[j-1]+siz[pt[j]]-1; fo(j,1,u)pre[j+u]=pre[j+u-1]+siz[pt[j]]-1; fo(j,1,u) if (siz[pt[j]]>1){ if (pre[j+u-1]==pre[j+u-siz[pt[j]]])ans=ans*2%mo; } } if (!ans)return printf("0\n"),0; fo(i,1,n) if (cnt[i]){ LL now=1,tmp=0; fo(j,0,cnt[i]/2){ if (i%2==1&&i>1)tmp=(tmp+js[cnt[i]]*inv[j]%mo*inv[cnt[i]-j*2]%mo*miv[j]%mo*now%mo*quickmi(2,cnt[i]-j*2)%mo)%mo; else tmp=(tmp+js[cnt[i]]*inv[j]%mo*inv[cnt[i]-j*2]%mo*miv[j]%mo*now%mo)%mo; now=now*i%mo; } ans=tmp*ans%mo; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; #define ll long long char buf[1<<20],*p1,*p2; #define GC (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?0:*p1++) template<class T> inline void read(T &n){ char ch=GC;T w=1,x=0; while(!isdigit(ch)){if(ch=='-') w=-1;ch=GC;} while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=GC;} n=x*w; } const int maxn=200005; int n,a[maxn],deg[maxn]; int vis[maxn],isc[maxn],link[maxn]; int cnt[maxn],ans=1; const int mod=1e9+7; inline int add(int x,int y){ x+=y;return x>=mod?x-mod:x; } inline int mul(int x,int y){ return 1ll*x*y%mod; } void solve(int x){ int now=0,fir=0,firl=0,lst=0; for(;isc[x];x=a[x]){ now++,isc[x]=0; if(link[x]){ if(!fir) fir=lst=now,firl=link[x]; else{ int tmp=now-lst; if(tmp<link[x]) puts("0"),exit(0); if(tmp>link[x]) ans=add(ans,ans); lst=now; } } } if(!fir) cnt[now]++; else{ int tmp=fir+now-lst; if(tmp<firl) puts("0"),exit(0); if(tmp>firl) ans=add(ans,ans); } } int f[maxn]={1}; int main(){ read(n); for(int i=1;i<=n;i++) read(a[i]),deg[a[i]]++; for(int i=1,j;i<=n;i++){ if(vis[i]) continue; for(j=i;!vis[j];j=a[j]) vis[j]=i; if(vis[j]!=i) continue; for(;!isc[j];j=a[j]) isc[j]=1; } for(int i=1;i<=n;i++) if((isc[i]&&deg[i]>2)||(!isc[i]&&deg[i]>1)) puts("0"),exit(0); for(int i=1,j;i<=n;i++){ if(deg[i]) continue; int len=0; for(j=i;!isc[j];j=a[j]) len++; link[j]=len; } for(int i=1;i<=n;i++) if(isc[i]) solve(i); for(int i=1;i<=n;i++){ if(!cnt[i]) continue; int flag=((i>1)&&(i&1)); for(int j=1;j<=cnt[i];j++){ flag?f[j]=add(f[j-1],f[j-1]):f[j]=f[j-1]; if(j>1) f[j]=add(f[j],mul(f[j-2],mul(j-1,i))); } ans=mul(ans,f[cnt[i]]); } cout<<ans<<endl; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<cstring> #include<algorithm> #define N 200010 #define ll long long #define mod 1000000007 using namespace std; struct edge{int x, y, next;}a[N]; int n, p[N], x, l, flag[N], sum, s[N], cl, d[N], fa[N], ans, def[N], f[N], ff, last, g[N]; inline void add(int x, int y){a[++l].x=x; a[l].y=y; a[l].next=p[x]; p[x]=l;} inline void dfs(int x, int FA){ flag[x]=1; sum++; for(int i=p[x]; i; i=a[i].next)if(i!=(FA^1)){ if(flag[a[i].y]){cl=1; d[0]=x; d[1]=a[i].y;} else{fa[a[i].y]=x; dfs(a[i].y, i);} } } inline void dfs1(int x, int fa){ def[x]=0; int sum=0; for(int i=p[x]; i; i=a[i].next)if(fa!=a[i].y&&!f[a[i].y]){ sum++; dfs1(a[i].y, x); def[x]=def[a[i].y]+1; } if(sum>=2)ff=0; } int main(){ scanf("%d", &n); l=1; memset(p, 0, sizeof(p)); for(int i=1; i<=n; i++){scanf("%d", &x); add(x, i); add(i, x);} ans=1; memset(flag, 0, sizeof(flag)); memset(s, 0, sizeof(s)); memset(f, 0, sizeof(f)); for(int i=1; i<=n; i++)if(!flag[i]){ sum=0; dfs(i, 0); x=d[1]; while(x!=d[0]){x=fa[x]; d[++cl]=x;} if(sum==cl){s[sum]++; continue;} for(int j=p[d[1]]; j; j=a[j].next)if(a[j].y==d[0]) if(j&1){for(int k=1; k<=cl/2; k++)swap(d[k], d[cl-k+1]); d[0]=d[cl];} for(int j=1; j<=cl; j++)f[d[j]]=1; ff=1; for(int j=1; j<=cl&&ff; j++)dfs1(d[j], 0); if(!ff){printf("0"); return 0;} last=cl; while(!def[d[last]])last--; for(int j=1; j<=cl; j++)if(def[d[j]]){ x=(j-last-1+cl)%cl; if(def[d[j]]<=x)ans=ans*2%mod; if(def[d[j]]>=x+2){printf("0"); return 0;} last=j; } } for(int i=1; i<=n; i++)if(s[i]){ x=1; if((i&1)&&i>=3)x++; g[0]=1; g[1]=x; for(int j=2; j<=s[i]; j++)g[j]=((ll)x*g[j-1]%mod+(ll)(j-1)*i%mod*g[j-2]%mod)%mod; ans=(ll)ans*g[s[i]]%mod; } printf("%d", ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<algorithm> #include<cmath> #include<cstdlib> #include<cstring> #define gc getchar() using namespace std; const int N=3e5+10,mod=1e9+7; inline void qr(int &x) { x=0;char c=gc;int f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc;} while(c>='0'&&c<='9'){x=x*10+(c^48);c=gc;} x*=f; } void qw(int x) { if(x<0)x=-x,putchar('-'); if(x/10)qw(x/10); putchar(x%10+48); } struct edge{int y,next;}e[N<<1];int len,last[N]; void ins(int x,int y){e[++len]=(edge){y,last[x]};last[x]=len;} int n,fa[N]; int get(int x){return x==fa[x]?x:fa[x]=get(fa[x]);} int ring[N],tot,num[N],a[N];bool v[N]; bool fring(int x,int ed) { if(x==ed){v[x]=1;ring[++tot]=x;return 1;} for(int k=last[x],y;k;k=e[k].next) { y=e[k].y; if(fring(y,ed)){v[x]=1;ring[++tot]=x;return 1;} } return 0; } int cnt,c[N],P[N],Q[N],sum[N]; void dfs(int x) { int sz=0; for(int k=last[x],y;k;k=e[k].next) { y=e[k].y; if(!v[y])++sz,++cnt,dfs(y); } if(sz>1){puts("0");exit(0);} } int main() { //freopen("dayinf.in","r",stdin); //freopen("dayinf.out","w",stdout); qr(n); for(int i=1;i<=n;i++)qr(a[i]),fa[i]=i; for(int i=1;i<=n;i++) { int p=get(i),q=get(a[i]); if(p!=q)fa[p]=q,P[q]|=P[p],Q[q]|=Q[p],ins(a[i],i); else P[q]=i,Q[q]=a[i]; } int ans=1; for(int i=1;i<=n;i++)if(fa[i]==i) { tot=0;fring(P[i],Q[i]); for(int j=1;j<=tot;j++)cnt=0,dfs(ring[j]),num[j]=cnt; reverse(num+1,num+tot+1); int fi=-1,s1=1,s2=0,cal=0; for(int j=1;j<=tot;j++)if(num[j]){fi=j;break;} if(fi==-1){++sum[tot];continue;} if(tot==1) { if(num[fi]>1)return puts("0"),0; continue; } for(int j=tot+1;j<=3*tot;j++)num[j]=num[j-tot]; for(int j=fi+1;j<fi+num[fi];j++)if(num[j])return puts("0"),0; for(int j=fi+num[fi],nxt=j+1;j<fi+tot;j=nxt,nxt=j+1)if(num[j]) { if(j+num[j]-tot>fi){s1=0;return puts("0"),0;} for(;nxt<j+num[j];nxt++)if(num[nxt]){s1=0;return puts("0"),0;} if(num[j+num[j]])continue; s1=1ll*2*s1%mod; } cal=(cal+s1)%mod; if(!num[fi+num[fi]]) { s2=1; for(int j=fi+num[fi]+1,nxt=j+1;j<fi+tot;j=nxt,nxt=j+1)if(num[j]) { if(j+num[j]-tot>fi+1){s2=0;return puts("0"),0;} for(;nxt<j+num[j];nxt++)if(num[nxt]){s2=0;return puts("0"),0;} if(num[j+num[j]])continue; s2=1ll*2*s2%mod; } } cal=(cal+s2)%mod; ans=1ll*ans*cal%mod; } for(int i=1;i<=n;i++)if(sum[i]) { if((i&1)&&i!=1) { c[0]=1;c[1]=2; for(int j=2;j<=sum[i];j++) c[j]=(c[j-1]*2+1ll*c[j-2]*(j-1)%mod*i%mod)%mod; ans=1ll*ans*c[sum[i]]%mod;continue; } c[0]=1;c[1]=1; for(int j=2;j<=sum[i];j++) c[j]=(c[j-1]+1ll*c[j-2]*(j-1)%mod*i%mod)%mod; ans=1ll*ans*c[sum[i]]%mod; } qw(ans);puts(""); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #define ll long long #define P 1000000007 int a[100005],in[100005],col[100005],cnt[100005],n,foot[100005];bool cir[100005];long long dp[100005],ans=1; int main() { scanf("%d",&n); for (int i=1;i<=n;++i) scanf("%d",&a[i]),++in[a[i]]; for (int i=1;i<=n;++i) { if (col[i]) continue;int x=i; for(;!col[x];x=a[x]) col[x]=i; if (col[x]!=i) continue; for (;!cir[x];x=a[x]) cir[x]=1; } for (int i=1;i<=n;++i) if ((cir[i]&&in[i]>2)||(!cir[i]&&in[i]>1)) {printf("0");return 0;} for (int i=1;i<=n;++i) { if (in[i]) continue;int x=i,tmp=0; while(!cir[x]) x=a[x],tmp++;foot[x]=tmp; } for (int i=1;i<=n;++i) { if (!cir[i]) continue;int x=i,st=0,first=0,id=0,len=0; while (cir[x]) { ++id;cir[x]=0; if (foot[x]) { if (!first){st=first=id;len=foot[x];x=a[x];continue;} else { int numm=(foot[x]<(id-st))+(foot[x]<=(id-st)); (ans*=numm)%=P;if (!ans) {printf("0");return 0;}st=id;x=a[x];continue; } } x=a[x]; } if (first) { int numm=(len<(id+first-st))+(len<=(id+first-st)); (ans*=numm)%=P;if (!ans) {printf("0");return 0;} } else cnt[id]++; } for (int i=1;i<=n;++i) { dp[0]=1;if (!cnt[i]) continue; if (i>1&&(i%2)) for (int j=1;j<=cnt[i];++j) {dp[j]=dp[j-1]*2%P;if (j>1) (dp[j]+=dp[j-2]*(j-1)*i%P)%=P;} else for (int j=1;j<=cnt[i];++j){dp[j]=dp[j-1];if (j>1) (dp[j]+=dp[j-2]*(j-1)*i%P)%=P;} (ans*=dp[cnt[i]])%=P;if (!ans){printf("0");return 0;} } printf("%lld",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #define SF scanf #define PF printf #define MAXN 100010 #define MOD 1000000007 typedef long long ll; using namespace std; int a[MAXN],pre[MAXN]; int d[MAXN],n,cnt[MAXN]; ll ans=1; ll f[MAXN]; bool vis[MAXN]; int main(){ SF("%d",&n); for(int i=1;i<=n;i++){ SF("%d",&a[i]); d[a[i]]++; } for(int i=1;i<=n;i++){ if(d[i]>2){ PF("0"); return 0; } if(d[i]<2||vis[i]==1) continue; int p=a[i]; vis[i]=1; pre[p]=i; while(p!=i){ if(vis[p]==1){ PF("0"); return 0; } vis[p]=1; pre[a[p]]=p; p=a[p]; } } for(int i=1;i<=n;i++) if(d[i]==0){ int p=i,l1=0,l2=1; while(vis[p]==0){ vis[p]=1; p=a[p]; l1++; } p=pre[p]; while(d[p]!=2){ p=pre[p]; l2++; } if(l1<l2) ans=ans*2ll%MOD; else if(l1>l2){ PF("0"); return 0; } } for(int i=1;i<=n;i++) if(vis[i]==0){ int len=0; int p=i; while(vis[p]==0){ len++; vis[p]=1; p=a[p]; } cnt[len]++; } for(int i=1;i<=n;i++){ f[0]=1; ll mul=i%2+1; if(i==1) mul=1; f[1]=mul; for(int j=2;j<=cnt[i];j++) f[j]=(1ll*f[j-2]*(j-1)%MOD*i%MOD+f[j-1]*mul%MOD)%MOD; ans=ans*f[cnt[i]]%MOD; } PF("%lld",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
import java.io.*; import java.util.*; public class Main { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; int solve(int[] p) { int n = p.length; int[] in = new int[n]; for (int x : p) { in[x]++; } int[] vis = new int[n]; Arrays.fill(vis, -1); boolean[] onCycle = new boolean[n]; for (int i = 0; i < n; i++) { int v = i; while (vis[v] == -1) { vis[v] = i; v = p[v]; } if (vis[v] != i) { continue; } onCycle[v] = true; for (int u = p[v]; u != v; u = p[u]) { onCycle[u] = true; } } for (int i = 0; i < n; i++) { if ((!onCycle[i] && in[i] > 1) || (onCycle[i] && (in[i] < 1 || in[i] > 2))) { return 0; } } int[] lenIn = new int[n]; for (int i = 0; i < n; i++) { if (!onCycle[i] && in[i] == 0) { int cnt = 0, v = i; while (!onCycle[v]) { cnt++; v = p[v]; } lenIn[v] = cnt; } } int ans = 1; for (int i = 0; i < n; i++) { if (lenIn[i] != 0) { int v = p[i], len = 1; while (lenIn[v] == 0) { v = p[v]; len++; } if (len < lenIn[v]) { return 0; } if (len > lenIn[v]) { ans = 2 * ans % P; } } } int[] cnt = new int[n + 1]; for (int i = 0; i < n; i++) { if (onCycle[i]) { boolean justCycle = lenIn[i] == 0; int v = p[i], len = 1; onCycle[v] = false; while (v != i) { justCycle &= lenIn[v] == 0; v = p[v]; len++; onCycle[v] = false; } if (justCycle) { cnt[len]++; } } } for (int len = 1; len <= n; len++) { ans = (int)((long)ans * go(len, cnt[len]) % P); } return ans; } int go(int len, int cnt) { int[] dp = new int[cnt + 1]; dp[0] = 1; int mult = len % 2 == 1 && len > 1 ? 2 : 1; for (int i = 1; i <= cnt; i++) { dp[i] = mult * dp[i - 1] % P; if (i > 1) { dp[i] += (int)((long)dp[i - 2] * (i - 1) % P * len % P); if (dp[i] >= P) { dp[i] -= P; } } } return dp[cnt]; } static final int P = 1_000_000_007; static final Random rng = new Random(); void test() { int n = 9; for (int i = 0; i < Integer.MAX_VALUE; i++) { int[] a = new int[n]; for (int j = 0; j < n; j++) { a[j] = rng.nextInt(n); } if (solve(a) != stupid(a)) { throw new AssertionError(Arrays.toString(a)); } System.err.println(i); } // int[] a = {2, 0, 1, 2, 3}; // System.err.println(solve(a)); // System.err.println(stupid(a)); } int stupid(int[] need) { int n = need.length; int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = i; } int cnt = 0; do { boolean good = true; for (int i = 0; i < n; i++) { good &= (p[i] == need[i] || p[p[i]] == need[i]); } if (good) { System.err.println(Arrays.toString(p)); } cnt += good ? 1 : 0; } while (nextPermutation(p)); return cnt; } static boolean nextPermutation(int[] a) { int n = a.length; int ptr = n - 2; while (ptr >= 0 && a[ptr] > a[ptr + 1]) { ptr--; } if (ptr < 0) { return false; } for (int i = ptr + 1, j = n - 1; i < j; i++, j--) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } for (int i = ptr + 1;; i++) { if (a[ptr] < a[i]) { int tmp = a[ptr]; a[ptr] = a[i]; a[i] = tmp; return true; } } } void submit() throws IOException { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt() - 1; } out.println(solve(a)); } Main() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); submit(); // test(); out.close(); } public static void main(String[] args) throws IOException { new Main(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
JAVA
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <iostream> #include <algorithm> #include<cmath> #include<cstring> #include<cstdio> #include<cstdlib> #include<vector> #include<iomanip> #include<ctime> #include<set> #include<map> #include<queue> #include<stack> #define sqr(x) ((x)*(x)) #define fz1(i,n) for ((i)=1;(i)<=(n);(i)++) #define fd1(i,n) for ((i)=(n);(i)>=1;(i)--) #define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++) #define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--) #define fz0k(i,n) for ((i)=0;(i)<(n);(i)++) #define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--) #define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++) #define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--) #define fzin fz1(i,n) #define fzim fz1(i,m) #define fzjn fz1(j,n) #define fzjm fz1(j,m) #define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++) #define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;} #define inc(x,y) {x+=(y);if(x>=mod)x-=mod;} #define dec(x,y) {x-=(y);if(x<0)x+=mod;} #define spln(i,n) (i==n?'\n':' ') using namespace std; int n,m,i,j,mod=1e9+7,vis[100005],icr[100005],a[100005],cnt[100005],ans,f[100005],g[100005]; vector<int> bi[100005]; vector<int> seq,nxt; int qp(int x,int y) { int z=1; while(y){ if(y&1){ z=1ll*z*x%mod; } y/=2; x=1ll*x*x%mod; } return z; } void solve(int x) { int st=x; for(;;){ int y=x,z,lst=0,l1=0,l2=0; for(;;){ l1++;lst=y;y=a[y];vis[y]=1; if(bi[y].size()==2) break; } z=bi[y][0]^bi[y][1]^lst;vis[z]=1; l2=1; while(!bi[z].empty()){ z=bi[z][0];l2++;vis[z]=1; } if(l2>l1){ puts("0"); exit(0); } if(l2<l1) ans=2ll*ans%mod; if(y==st) return;x=y; } } int main() { scanf("%d",&n); fz1(i,n){ scanf("%d",&a[i]); bi[a[i]].push_back(i); } fz1(i,n)if(!vis[i]){ int x=i;while(!vis[x]){ vis[x]=i;x=a[x]; } if(vis[x]!=i) continue; while(!icr[x]){ icr[x]=1;x=a[x]; } } fz1(i,n){ if(icr[i]){ if(bi[i].size()>2){ puts("0"); return 0; } } else{ if(bi[i].size()>1){ puts("0"); return 0; } } } ans=1; memset(vis,0,sizeof(vis)); fz1(i,n)if(bi[i].size()==2&&!vis[i]){ solve(i); } fz1(i,n)if(!vis[i]){ int x=i,c=0;while(!vis[x]){ vis[x]=1;x=a[x];c++; } cnt[c]++; } fz1(i,n){ f[0]=1;if((i&1)&&i>1) f[1]=2; else f[1]=1; fz(j,2,cnt[i]){ if((i&1)&&i>1) f[j]=(f[j-1]+f[j-1]+1ll*f[j-2]*(j-1)%mod*i)%mod; else f[j]=(f[j-1]+1ll*f[j-2]*(j-1)%mod*i)%mod; } ans=1ll*ans*f[cnt[i]]%mod; } cout<<ans<<endl; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> #define rep(i,j,k) for (i=j;i<=k;i++) using namespace std; const int N=1e5+5,mod=1e9+7; int n,i,j,sz,cir; int a[N],vis[N],dgr[N],foot[N],cnt[N],dis[N],nc[N]; long long ans,g[N]; void dfs(int x) { vis[x]=1; sz++; if (dgr[a[x]]!=1 || nc[a[x]]) cir=0; if (vis[a[x]]) return ; dfs(a[x]); } void dfs2(int x,int step) { if (dgr[a[x]]>1) {foot[a[x]]=step; return ;} dfs2(a[x],step+1); } void dfs3(int x,int step) { if (dgr[a[x]]==2) {dis[a[x]]=step; return;} dfs3(a[x],step+1); } void dp(int n,int k) { int i; g[0]=1; rep(i,1,n) { if ((k%2) && (k>1)) g[i]=g[i-1]*2%mod; else g[i]=g[i-1]; if (i>1) g[i]=(g[i]+g[i-2]*(i-1)%mod *k%mod)%mod; } ans=ans*g[n]%mod; } void calc(int x) { if (foot[x]<dis[x]) ans=ans*2%mod; else if (foot[x]>dis[x]) ans=0; } int main() { //freopen("cycle.in","r",stdin); //freopen("cycle.out","w",stdout); scanf("%d",&n); ans=1; rep(i,1,n) scanf("%d",&a[i]),dgr[a[i]]++; rep(i,1,n) if (dgr[i]>2) { printf("0\n"); return 0; } rep(i,1,n) if (!vis[i]) { sz=0; cir=1; dfs(i); if (cir) cnt[sz]++; else nc[i]=1; } rep(i,1,n) if (!dgr[i]) dfs2(i,1); rep(i,1,n) if (dgr[i]==2) dfs3(i,1); rep(i,1,n) if (cnt[i]) dp(cnt[i],i); rep(i,1,n) if (dgr[i]==2) calc(i); printf("%lld\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> const int Mod=(int)1e9+7; using namespace std; int deg[100001],a[100001],num[100001],l[100001],d[100001]; bool vis[100001]; int Fact[100001],Inv[100001]; inline int Pow(int a,int x) { int res=1; for(;x;x>>=1,a=a*1ll*a%Mod) if(x&1)res=a*1ll*res%Mod; return res; } queue<int>Q; inline int C(int x,int y){return Fact[x]*1ll*Inv[y]%Mod*Inv[x-y]%Mod;} #define x first #define y second #define mp make_pair #define pb push_back vector<pair<int,int> >Cir; inline int pr(int n) { return Fact[n<<1]*1ll*Inv[n]%Mod*Pow(Mod+1>>1,n)%Mod; } int main() { Fact[0]=1; for(int i=1;i<=100000;i++)Fact[i]=Fact[i-1]*1ll*i%Mod; Inv[100000]=Pow(Fact[100000],Mod-2); for(int i=99999;~i;i--)Inv[i]=Inv[i+1]*(i+1ll)%Mod; int n; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",a+i),deg[a[i]]++; for(int i=1;i<=n;i++)if(deg[i]>2)return puts("0"),0; for(int i=1;i<=n;i++)if(!(d[i]=deg[i]))Q.push(i); while(!Q.empty()) { int u=Q.front();Q.pop(); vis[u]=1; if(!--d[a[u]])Q.push(a[u]); } for(int i=1;i<=n;i++)if(vis[i]&&deg[i]>1)return puts("0"),0; for(int i=1;i<=n;i++)if(vis[i]&&!deg[i]) { int x=i,cnt=0; for(;vis[x];x=a[x])cnt++; l[x]=cnt; } int ans=1; for(int i=1;i<=n;i++)if(!vis[i]) { int x=i,fl=0,tp=1,tot=0; for(;!vis[x];x=a[x])++tot,fl|=l[x],vis[x]=1; if(!fl){num[tot]++;continue;} Cir.clear(); int p=1;x=i; do { if(l[x])Cir.pb(mp(l[x],p)); x=a[x];p++; }while(x^i); Cir.pb(mp(Cir[0].x,Cir[0].y+tot)); for(int j=1;j<Cir.size();j++) if(Cir[j].x>Cir[j].y-Cir[j-1].y)tp=0; else if(Cir[j].x^(Cir[j].y-Cir[j-1].y))tp=tp*2ll%Mod; ans=ans*1ll*tp%Mod; } // printf("%d\n",ans); for(int i=1;i<=n;i++)if(num[i]) { int m=num[i],tp=0; for(int j=0;j<=m/2;j++) { int tp2=1ll*C(m,2*j)*pr(j)%Mod*Pow(i,j)%Mod; if(i>1&&(i&1))tp2=tp2*1ll*Pow(2,m-2*j)%Mod; tp=(tp+tp2)%Mod; } ans=ans*1ll*tp%Mod; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <queue> #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define N 100100 #define fi first #define se second #define pb push_back #define mp make_pair #define mod 1000000007 #define rep(x, a, b) for(int x=a; x<=b; x++) #define drp(x, a, b) for(int x=a; x>=b; x--) int deg[N], d[N], n, vis[N], a[N], tot, num[N], l[N], ans=1, fac[N], inv[N]; vector<pair<int, int> > fu; queue<int> q; int power(int a, int k){ int ret=1; while(k) { if(k&1) ret=1ll*ret*a%mod; a=1ll*a*a%mod; k>>=1; } return ret; } int C(int n, int m){ return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod; } int pr(int n){ return 1ll*fac[2*n]*inv[n]%mod*power((mod+1)>>1, n)%mod; } void init(){ fac[0]=1; rep(i, 1, n) fac[i]=1ll*fac[i-1]*i%mod; inv[n]=power(fac[n], mod-2); drp(i, n, 1) inv[i-1]=1ll*inv[i]*i%mod; } int st[N<<1]; void renew(int &x, const int y){ x += y; if(x < 0) x += mod; if(x >= mod) x -= mod; } int getdp(){ int fr = 0; rep(i, 1, tot) st[i + tot] = st[i]; rep(i, 1, tot) if(l[st[i]]){ fr = i; break; } reverse(st + fr + 1, st + fr + tot); int a = fr + l[st[fr]] - 1, b = a + 1, c = 1; rep(i, fr + 1, fr + tot - 1)if(l[st[i]]){ int na = i + l[st[i]] - 1, nb = na + 1, cc = 0; if(a < i) renew(cc, c); if(b < i) renew(cc, c); a = na; b = nb; c = cc; } int ans = 0; if(a < fr + tot) renew(ans, c); if(b < fr + tot) renew(ans, c); return ans; } int que[N], h, t; int main(){ scanf("%d", &n); init(); rep(i, 1, n) { scanf("%d", a+i); deg[a[i]]++; } rep(i, 1, n) if(deg[i]>2) return puts("0"), 0; rep(i, 1, n) d[i]=deg[i]; rep(i, 1, n) if(!d[i]) q.push(i); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=1; if(--d[a[u]]==0) q.push(a[u]); } rep(i, 1, n) if(vis[i] && deg[i]>1) return puts("0"), 0; rep(i, 1, n) if(vis[i] && !deg[i]) { int x=i, cnt=0; for(; vis[x]; x=a[x]) cnt++; l[x]=cnt; } rep(i, 1, n) if(!vis[i]) { int x=i, fl=0, ass=1; tot=0; for(; !vis[x]; x=a[x]) st[++tot]=x, fl|=bool(l[x]), vis[x]=1; if(!fl) num[tot]++; else {/* int p=0; fu.clear(); if(l[i]) fu.pb(mp(l[i], 0)); for(p++, x=a[i]; x!=i; x=a[x]) if(l[x]) fu.pb(mp(l[x], p)); fu.pb(mp(fu[0].fi, fu[0].se+tot)); int sz=fu.size(); rep(i, 1, sz-1) { if(fu[i].fi>fu[i].se-fu[i-1].se) ass=0; if(fu[i].fi<fu[i].se-fu[i-1].se) ass=2ll*ass%mod; }*/ ass=getdp(); } ans=1ll*ans*ass%mod; } rep(i, 1, n) if(num[i]) { int m=num[i], ass=0; rep(j, 0, m/2) { int tmp=1; tmp=1ll*tmp*power(i, j)%mod; tmp=1ll*tmp*C(m, 2*j)%mod; tmp=1ll*tmp*fac[2*j]%mod; tmp=1ll*tmp*inv[j]%mod; tmp=1ll*tmp*power((mod+1)>>1, j)%mod; if(i>1 && (i&1)) tmp=1ll*tmp*power(2, m-2*j)%mod; ass=(ass+tmp)%mod; } ans=1ll*ans*ass%mod; } printf("%d\n", ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+10,mod=1e9+7; int n,a[maxn],cnt[maxn]; int dfsclock,dfn[maxn],low[maxn],stk[maxn],top,scccnt,scc[maxn],size[maxn]; ll ans,fac[maxn],inv[maxn],f[maxn],pow2[maxn]; int type[maxn],weight[maxn]; vector<int> g[maxn]; inline ll qpow(ll a,ll n){ ll res=1; while(n){ if(n&1ll) res=res*a%mod; a=a*a%mod; n>>=1; } return res; } inline void init(){ fac[0]=1;for(int i=1;i<maxn;++i)fac[i]=fac[i-1]*i%mod; inv[maxn-1]=qpow(fac[maxn-1],mod-2);for(int i=maxn-1;i;--i)inv[i-1]=inv[i]*i%mod; f[0]=1;for(int i=1;i<maxn;++i)f[i]=f[i-1]*(2*i-1)%mod; pow2[0]=1;for(int i=1;i<maxn;++i)pow2[i]=pow2[i-1]*2%mod; } void dfs(int pos){ dfn[pos]=low[pos]=++dfsclock; stk[++top]=pos; if(!dfn[a[pos]]){ dfs(a[pos]); low[pos]=min(low[pos],low[a[pos]]); } else if(!scc[a[pos]]) low[pos]=min(low[pos],dfn[a[pos]]); if(dfn[pos]<=low[pos]){ ++scccnt; do{ scc[stk[top]]=scccnt; ++size[scccnt]; } while(stk[top--]!=pos); } } int main(){ init(); cin>>n; for(int i=1;i<=n;++i) cin>>a[i]; for(int i=1;i<=n;++i) if(!dfn[i]) dfs(i); for(int i=1;i<=n;++i) if(scc[a[i]]==scc[i]) type[scc[i]]=1; else g[a[i]].push_back(i),weight[scc[a[i]]]=true; for(int i=1;i<=n;++i) if(g[i].size()>1){ puts("0"); return 0; } for(int i=1;i<=scccnt;++i) if(type[i]==1){ if(weight[i]) type[i]=2; else ++cnt[size[i]]; } ans=1; for(int i=1;i<=n;++i) if(type[scc[i]]==2){ type[scc[i]]=0; static int ring[maxn]; int len;ll tmp=1; ring[len=1]=i; for(int p=a[i];p!=i;p=a[p]) ring[++len]=p; for(int j=1;j<=len;++j) if(g[ring[j]].size()){ int p=ring[j],val=0; while(g[p].size()) ++val,p=g[p][0]; int nxt=1; for(int k=j==1?len:j-1;g[ring[k]].empty();k=k==1?len:k-1) ++nxt; if(nxt>val) tmp=tmp*2%mod; if(nxt<val){ puts("0"); return 0; } } ans=ans*tmp%mod; } for(int i=1;i<=n;++i) if((i&1)&&i>1){ ll tmp=0; for(int j=0;2*j<=cnt[i];++j) tmp=(tmp+fac[cnt[i]]*inv[2*j]%mod*inv[cnt[i]-2*j]%mod*f[j]%mod*qpow(i,j)%mod*pow2[cnt[i]-2*j])%mod; ans=ans*tmp%mod; } else{ ll tmp=0; for(int j=0;2*j<=cnt[i];++j) tmp=(tmp+fac[cnt[i]]*inv[2*j]%mod*inv[cnt[i]-2*j]%mod*f[j]%mod*qpow(i,j))%mod; ans=ans*tmp%mod; } cout<<ans<<endl; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 100005, mod = 1e9 + 7; inline int gi() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum; } inline int add(int x, int y) {return x + y >= mod ? x + y - mod : x + y;} int n, ans, a[maxn], cir[maxn], flen[maxn], sum[maxn], f[maxn], deg[maxn], vis[maxn]; void solvecir(int x) { int len = 0, s = 0, lst = 0, slen = 0; while (cir[x]) { ++len; cir[x] = 0; if (flen[x]) { if (!s) s = lst = len, slen = flen[x]; else { ans = (ll)ans * ((flen[x] < len - lst) + (flen[x] <= len - lst)) % mod; lst = len; } } x = a[x]; } if (!s) ++sum[len]; else ans = (ll)ans * ((slen < len - lst + s) + (slen <= len - lst + s)) % mod; } void solve() { for (int i = 1; i <= n; ++i) { if (deg[i]) continue; int x = i, len = 0; while (!cir[x]) x = a[x], ++len; flen[x] = len; } ans = 1; for (int i = 1; i <= n; ++i) if (cir[i]) solvecir(i); for (int i = 1; i <= n; ++i) { if (!sum[i]) continue; f[0] = 1; for (int j = 1; j <= sum[i]; ++j) { if (i > 1 && (i & 1)) f[j] = add(f[j - 1], f[j - 1]); else f[j] = f[j - 1]; if (j > 1) f[j] = (f[j] + (ll)f[j - 2] * (j - 1) % mod * i) % mod; } ans = (ll)ans * f[sum[i]] % mod; } } int main() { n = gi(); for (int i = 1; i <= n; ++i) a[i] = gi(), ++deg[a[i]]; for (int i = 1; i <= n; ++i) { if (vis[i]) continue; int x = i; while (!vis[x]) vis[x] = i, x = a[x]; if (vis[x] != i) continue; while (!cir[x]) cir[x] = 1, x = a[x]; } for (int i = 1; i <= n; ++i) if ((cir[i] && deg[i] > 2) || (!cir[i] && deg[i] > 1)) return puts("0"), 0; solve(); printf("%d\n", ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long LL; const int N = 1e5 + 50; const LL mod = 1e9 + 7; vector <int> G[N], cir; int n, a[N], sz, pt; int vis[N], cur; int len[N], cnt[N]; LL fac[N], inv[N]; inline void Dfs(int x) { if (vis[x]) { if (vis[x] != cur) sz = 0; else pt = x; return; } vis[x] = cur; sz ++; Dfs(a[x]); } inline void Dfs1(int x) { if (vis[x] == cur) return; cir.push_back(x); vis[x] = cur; Dfs1(a[x]); } inline int iDfs(int x) { if (G[x].size() > 1u) return (int) -1e9; if (!G[x].size()) return 1; return 1 + iDfs(G[x][0]); } inline LL Pow(LL x, LL exp) { LL r = 1; for (; exp; exp >>= 1, x = x * x % mod) if (exp & 1) r = r * x % mod; return r; } inline void Init() { for (int i = fac[0] = 1; i < N; i ++) fac[i] = fac[i - 1] * i % mod; inv[N - 1] = Pow(fac[N - 1], mod - 2); for (int i = N - 1; i; i --) inv[i - 1] = inv[i] * i % mod; } inline LL C(int x, int y) { return fac[x] * inv[y] % mod * inv[x - y] % mod; } int main() { Init(); scanf("%d", &n); for (int i = 1; i <= n; i ++) { scanf("%d", &a[i]); G[a[i]].push_back(i); } LL res = 1; cir.reserve(n); for (int i = 1; i <= n; i ++) if (!vis[i]) { sz = 1; cur ++; Dfs(i); if (!sz) continue; cur ++; cir.clear(); Dfs1(pt); reverse(cir.begin(), cir.end()); int tot = 0, le = cir.size(); for (int j = 0; j < le; j ++) { vector <int> &Gi = G[cir[j]]; if (Gi.size() > 2u) return 0 * puts("0"); if (Gi.size() == 1u) len[j + 1] = 0; else tot += (len[j + 1] = iDfs(Gi[vis[Gi[0]] == cur])); if (len[j + 1] < 0) return 0 * puts("0"); } if (!tot) { cnt[le] ++; continue; } int dis = 0; for (int j = 1; j <= le; j ++) if (len[j]) { dis = j - 1; break; } for (int j = le; j; j --) { dis ++; if (!len[j]) continue; if (dis > len[j]) { res <<= 1; if (res >= mod) res -= mod; } else if (dis < len[j]) return 0 * puts("0"); dis = 0; } } for (int i = 1; i <= n; i ++) { if (!cnt[i]) continue; LL t = ((i & 1) && i != 1) + 1; LL tmp = Pow(t, cnt[i]); for (int j = (cnt[i] >> 1); j; j --) tmp += C(cnt[i], j << 1) * C(j << 1, j) % mod * Pow((mod + 1) >> 1, j) % mod * fac[j] % mod * Pow(i, j) % mod * Pow(t, (LL) cnt[i] - (j << 1)) % mod; res = res * (tmp % mod) % mod; } printf("%lld\n", res); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=100005,MOD=1000000007; int n,A[MAXN],deg[MAXN],pre[MAXN]; int cnt[MAXN],dp[MAXN]; int ans=1; bool vis[MAXN]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&A[i]); deg[A[i]]++; } for(int i=1;i<=n;i++) { if(deg[i]>2) {puts("0");return 0;} if(deg[i]<2||vis[i]) continue; int u=i; do { if(vis[u]) {puts("0");return 0;} vis[u]=true; pre[A[u]]=u; u=A[u]; }while(u!=i); } for(int i=1;i<=n;i++) if(deg[i]==0) { int u=i; int len1=0,len2=0; while(!vis[u]) { len1++; vis[u]=true; u=A[u]; } do { len2++; u=pre[u]; }while(deg[u]!=2); if(len2<len1) {puts("0");return 0;} else if(len2>len1) ans=ans*2%MOD; } for(int i=1;i<=n;i++) if(!vis[i]) { int len=0; int u=i; do { len++; vis[u]=true; u=A[u]; }while(u!=i); cnt[len]++; } for(int i=1;i<=n;i++) { int mul=1; if(i!=1&&i%2==1) mul=2; dp[0]=1; dp[1]=mul; for(int j=2;j<=cnt[i];j++) dp[j]=(1LL*dp[j-2]*(j-1)%MOD*i%MOD+1LL*dp[j-1]*mul%MOD)%MOD; ans=1LL*ans*dp[cnt[i]]%MOD; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
//In the Name of God //Ya Ali #include<bits/stdc++.h> #define int long long #define pb push_back #define err(A) cout<<#A<<" = "<<(A)<<endl using namespace std; const int M=1e9+7; const int maxn=100100; int n; int a[maxn]; vector<int> g[maxn]; bool vis[maxn]; int cnt[maxn]; int dp[maxn]; int ans=1; void gn() { cout<<ans<<endl; exit(0); } int len(int v) { int ret=1; int c=0; vis[v]=true; for(int u:g[v]) if(!vis[u]) { c++; ret+=len(u); } if(c>1) ans=0; return ret; } void solve(int v) { vector<int> q,p; while(!vis[v]) { vis[v]=true; q.pb(v); v=a[v]; } for(int u:q) vis[u]=false; q.resize(0); while(!vis[v]) { vis[v]=true; q.pb(v); v=a[v]; } p.resize(q.size()); int prv=-1; for(int i=0;i<q.size();i++) if(g[q[i]].size()==2) prv=i; if(prv==-1) { cnt[q.size()]++; return; } for(int i=0;i<q.size();i++) { p[i]=prv; if(g[q[i]].size()==2) { int l1=len(q[i])-1; int l2=(i-prv+q.size())%q.size(); if(l2==0) l2=q.size(); if(l1>l2) ans=0; if(l1<l2) ans=ans*2%M; prv=i; } } } int32_t main() { ios::sync_with_stdio(0);cin.tie(0); cin>>n; for(int i=0;i<n;i++) cin>>a[i],a[i]--; for(int i=0;i<n;i++) g[a[i]].pb(i); for(int i=0;i<n;i++) if(g[i].size()>2) ans=0,gn(); for(int i=0;i<n;i++) if(!vis[i]) solve(i); for(int i=1;i<=n;i++) { dp[0]=1; dp[1]=1+(i>2 and i&1); for(int j=2;j<=cnt[i];j++) dp[j]=(dp[j-1]*dp[1]+dp[j-2]*(j-1)*i)%M; ans=ans*dp[cnt[i]]%M; } gn(); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<iostream> #include<vector> #include<algorithm> #include<cstring> #include<cstdio> #include<cmath> #include<cstdlib> #include<ctime> #include<queue> #include<set> #include<map> #include<stack> using namespace std; typedef long long LL; const int N=1e5+100; int gi() { int w=0;bool q=1;char c=getchar(); while ((c<'0'||c>'9') && c!='-') c=getchar(); if (c=='-') q=0,c=getchar(); while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar(); return q? w:-w; } int p[N],vis[N],st[N],len[N],tot[N]; int head[N],nxt[N]; bool rt[N]; int f[N],g[N]; inline int dfs(int k) { int s=0; for (int i=head[k];i;i=nxt[i]) if (!rt[i]) { if (s) puts("0"),exit(0); s=dfs(i)+1; } return s; } int main() { const int mod=1e9+7; int n=gi(),i,k,t,s,cnt=0,top,ans=1,p2=0; bool is; for (i=1;i<=n;i++) p[i]=gi(),nxt[i]=head[p[i]],head[p[i]]=i; for (i=1;i<=n;i++) if (!vis[i]) { ++cnt; for (k=i;!vis[k];k=p[k]) vis[k]=cnt; if (vis[k]!=cnt) continue; for (t=k,top=0;st[1]!=t;t=p[t]) rt[st[++top]=t]=true; reverse(st+1,st+1+top); is=false; for (k=1;k<=top;k++) is|=len[k]=dfs(st[k]); if (is) { for (k=1;k<=top;k++) if (len[k]) { for (t=k%top+1,s=1;!len[t];t=t%top+1,s++); if (s<len[k]) return puts("0"),0; if (s!=len[k]) p2++; } } else tot[top]++,p2+=top>1&&(top&1); } const int inv4=1LL*(mod+1)*(mod+1)/4%mod; for (i=1;i<=n;i++) { for (k=f[0]=f[1]=g[0]=g[1]=1;k<=tot[i];k++) { f[k+1]=(1LL*f[k-1]*k%mod*inv4%mod*i+f[k])%mod; g[k+1]=(1LL*g[k-1]*k%mod*i+g[k])%mod; } ans=1LL*ans*(i==1||i%2==0?g[tot[i]]:f[tot[i]])%mod; } while (p2--) (ans<<=1)%=mod; printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; #define MAXN 100005 #define P 1000000007 template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } int n, a[MAXN], d[MAXN], cnt[MAXN], p[MAXN], len[MAXN]; bool visited[MAXN], used[MAXN], special[MAXN]; deque <int> Q; long long dp[MAXN]; void add(long long &x, long long y) { x = (x + y) % P; } void times(long long &x, long long y) { x = x * y % P; } int main() { read(n); for (int i = 1; i <= n; i++) read(a[i]), d[a[i]]++; for (int i = 1; i <= n; i++) if (d[i] == 0) { visited[i] = true; Q.push_back(i); } while (!Q.empty()) { int tmp = Q.front(); Q.pop_front(); d[a[tmp]]--; if (d[a[tmp]] == 0) { visited[a[tmp]] = true; Q.push_back(a[tmp]); } } memset(d, 0, sizeof(d)); for (int i = 1; i <= n; i++) d[a[i]]++; for (int i = 1; i <= n; i++) if (visited[i] && d[i] >= 2 || !visited[i] && d[i] >= 3) { printf("0\n"); return 0; } for (int i = 1; i <= n; i++) if (!visited[i]) { p[a[i]] = i; if (!used[i]) { int pos = a[i], mx = d[i], len = 1; used[i] = true; while (pos != i) { mx = max(mx, d[pos]); len++; used[pos] = true; pos = a[pos]; } if (mx <= 1) cnt[len]++; else { pos = a[i]; special[i] = true; while (pos != i) { special[pos] = true; pos = a[pos]; } } } } long long ans = 1; for (int i = 1; i <= n; i++) { if (cnt[i] == 0) continue; dp[0] = 1; for (int j = 1; j <= cnt[i]; j++) { dp[j] = dp[j - 1]; if (i & 1 && i > 1) add(dp[j], dp[j - 1]); if (j > 1) add(dp[j], dp[j - 2] * (j - 1) % P * i); } times(ans, dp[cnt[i]]); } for (int i = 1; i <= n; i++) { if (d[i]) continue; int pos = i, l = 0; while (!special[pos]) { l++, pos = a[pos]; } len[pos] = l; } for (int i = 1; i <= n; i++) { if (len[i] == 0) continue; int pos = p[i], l = 1; while (!len[pos]) { l++, pos = p[pos]; } if (l > len[i]) times(ans, 2); else if (l < len[i]) ans = 0; } cout << ans << endl; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; typedef long long LL; #define N 120000 const LL mod=1000000007; LL n,ans,a[N],b[N],d[N],cnt[N],f[N]; bool vis[N]; int main(){ scanf("%lld",&n); for (LL i=1;i<=n;++i){ scanf("%lld",a+i); ++d[a[i]]; } for (LL i=1;i<=n;++i){ if (d[i]>2){puts("0"); return 0;} if (d[i]<2||vis[i]) continue; vis[i]=1; b[a[i]]=i; for (LL j=a[i];j!=i;j=a[j]){ if (vis[j]){puts("0"); return 0;} vis[j]=1; b[a[j]]=j; } } ans=1; for (LL i=1;i<=n;++i) if (!d[i]){ LL j=i,len1=0,len2=1; for (;!vis[j];j=a[j]){vis[j]=1; ++len1;} for (j=b[j];d[j]<2;j=b[j]) ++len2; if (len1>len2){puts("0"); return 0;} if (len1<len2) ans=ans*2%mod; } for (LL i=1;i<=n;++i) if (!vis[i]){ vis[i]=1; LL len=1; for (LL j=a[i];!vis[j];j=a[j]){vis[j]=1; ++len;} ++cnt[len]; } for (LL i=1;i<=n;++i){ LL t=i!=1&&(i&1)?2:1; f[0]=1; f[1]=t; for (int j=2;j<=cnt[i];++j) f[j]=(f[j-1]*t+f[j-2]*(j-1)%mod*i)%mod; ans=ans*f[cnt[i]]%mod; } printf("%lld\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; const int N=100100,mod=1000000007; typedef long long ll; int a[N],d[N],n,vis[N],ans=1,cnt[N],b[N]; void gofail(){cout<<0<<'\n';exit(0);} int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;++i)cin>>a[i],d[a[i]]++;//,b[a[i]]=i; for(int i=1;i<=n;++i){ if(d[i]>2)gofail(); if(d[i]<2||vis[i])continue; vis[i]=1;b[a[i]]=i; for(int j=a[i];j!=i;j=a[j]){ if(vis[j])gofail(); vis[j]=1;b[a[j]]=j; } } for(int i=1;i<=n;++i) if(!d[i]){ int cha=0,cir=1,j=i; for(;!vis[j];j=a[j])vis[j]=1,++cha; for(j=b[j];d[j]<2;j=b[j])++cir; if(cha<cir)ans=ans*2%mod; else if(cha>cir)gofail(); } for(int i=1;i<=n;++i) if(!vis[i]){ int L=1;vis[i]=1; for(int j=a[i];j!=i;j=a[j])vis[j]=1,++L; cnt[L]++; } for(int i=1;i<=n;++i)if(cnt[i]){ int t=2;if(i==1||(i%2==0))t=1; int u=1,v=t; for(int j=2;j<=cnt[i];++j){ int w=((ll)u*(j-1)%mod*i+(ll)v*t)%mod; u=v;v=w; } ans=(ll)ans*v%mod; } cout<<ans<<'\n'; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
//Heaplax //别让自己后悔 #include<bits/stdc++.h> #define N 100005 #define LL long long #define LOG(x) cerr<<#x<<" = "<<x<<endl #define add_edge(u,v) nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v #define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) char ch;bool fs;void re(int& x) { while(ch=getchar(),ch<33); if(ch=='-')fs=1,x=0;else fs=0,x=ch-48; while(ch=getchar(),ch>33)x=x*10+ch-48; if(fs)x=-x; } using namespace std; const int mod=1000000007; vector<int> cir[N]; int n,ans=1,cnt,a[N],d[N],sy[N],fl[N],sum[N],f[N]; int vis[N]; int main() { re(n); for(int i=1;i<=n;++i) re(a[i]),++d[a[i]]; for(int i=1;i<=n;++i) if(!d[i]) { static int st[N]; int top=0,now=i; while(!vis[now]) { st[++top]=now; vis[now]=i; now=a[now]; } if(sy[now]) { if(fl[now])return puts("0"),0; fl[now]=top; } else if(vis[now]!=i)return puts("0"),0; else { ++cnt; while(st[top]!=now) { cir[cnt].push_back(st[top]); sy[st[top]]=cnt; --top; } --top; cir[cnt].push_back(now); sy[now]=cnt; fl[now]=top; } } for(int i=1;i<=n;++i) if(!vis[i]) { int now=i,len=0; do { ++len; vis[now]=1; now=a[now]; } while(now!=i); ++sum[len]; } // LOG(ans); for(int i=1;i<=n;++i) { f[0]=1; for(int j=1;j<=sum[i];++j) { f[j]=f[j-1]; if((i&1) && i>1)f[j]=(f[j]+f[j-1])%mod; if(j>=2)f[j]=(f[j]+(j-1ll)*i%mod*f[j-2])%mod; } ans=1ll*ans*f[sum[i]]%mod; // LOG(ans); } // LOG(ans); // LOG(cnt); for(int i=1;i<=cnt;++i) { // for(int j:cir[i])cerr<<fl[j]<<" "; // cerr<<endl; reverse(cir[i].begin(),cir[i].end()); int last=0; for(int j=1;j<cir[i].size();++j)if(fl[cir[i][j]]) { if(j-last>fl[cir[i][j]])ans=ans*2%mod; else if(j-last<fl[cir[i][j]])return puts("0"),0; last=j; } // LOG(ans); if(cir[i].size()-last>fl[cir[i][0]])ans=ans*2%mod; else if(cir[i].size()-last<fl[cir[i][0]])return puts("0"),0; } printf("%d\n",ans); } /* 1 2 2 1 3 4 4 3 5 6 6 7 7 5 8 9 9 10 10 8 11 8 12 9 13 11 */
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<iostream> #include<cstring> using namespace std; const int N=1e5+10,mod=1e9+7; int n,a[N],d[N],cir[N],f[N],vis[N],sum[N],foot[N],Ans=1; void GetTree(int x) { int t=0,bs=1,fl=0; for(vis[x]=1,x=a[x];!fl;x=a[x]) { if(vis[x]) fl=1;vis[x]=1;t++; if(foot[x]) bs=bs*(t<foot[x]?0:(t==foot[x]?1:2))%mod,t=0; } Ans=1ll*Ans*bs%mod; } int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i],d[a[i]]++; for(int i=1;i<=n;i++) { if(vis[i]) continue; int x=i;while(!vis[x]) vis[x]=i,x=a[x]; if(vis[x]!=i) continue; while(!cir[x]) cir[x]=1,x=a[x]; } for(int i=1;i<=n;i++) if((cir[i]&&d[i]>2)||(!cir[i]&&d[i]>1)) return puts("0"),0; for(int i=1;i<=n;i++) if(d[i]==0) { int x=i,t=0; while(!cir[x]) t++,x=a[x]; foot[x]=t; } memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) if(cir[i]&&foot[i]&&!vis[i]) GetTree(i); for(int i=1;i<=n;i++) if(!vis[i]&&cir[i]) { int x=i,t=0; while(!vis[x]) vis[x]=1,t++,x=a[x]; sum[t]++; } for(int w=1,u;w<=n;w++) { f[0]=1;u=sum[w]; if(!u) continue; for(int i=1;i<=u;i++) { f[i]=f[i-1]*(((w&1)&&w>1)?2:1)%mod; if(i>1) f[i]=(f[i]+1ll*(i-1)*f[i-2]%mod*w%mod)%mod; } Ans=1ll*Ans*f[u]%mod; } cout<<Ans<<endl; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<stdio.h> #include<algorithm> #include<vector> using namespace std; int w[101000], Deg[101000], n, chk[101000], Q[101000], tail, head, C[101000]; bool v[101000]; long long Res = 1, D[101000], Mod = 1000000007; vector<int>E[101000]; int main(){ int i, x, y, j; scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d",&w[i]); E[w[i]].push_back(i); Deg[w[i]]++; if(Deg[w[i]] == 3) goto fail; } for(i=1;i<=n;i++)if(!Deg[i])Q[++tail] = i; while(head < tail){ x = Q[++head]; Deg[w[x]]--; if(!Deg[w[x]]) Q[++tail] = w[x]; } for(i=1;i<=tail;i++)chk[Q[i]] = 1; for(i=1;i<=n;i++){ if(chk[i] && E[i].size() == 2)goto fail; if(chk[i]){ x=i; while(!v[x]){v[x]=true;x=w[x];} } if(E[i].size() == 2){ x = E[i][0], y = E[i][1]; while(E[x].size()*E[y].size()==1) x = E[x][0], y = E[y][0]; if(E[x].size() + E[y].size() == 2)continue; if(E[x].size() + E[y].size() > 2)goto fail; Res = Res * 2 % Mod; } } for(i=1;i<=n;i++){ if(!v[i]){ x=i;y=0; while(!v[x]){y++;v[x]=true;x=w[x];} C[y]++; } } for(i=1;i<=n;i++){ if(!C[i])continue; D[0] = 1; for(j=1;j<=C[i];j++){ D[j] = D[j-1]; if(i!=1&&i%2==1)D[j]=D[j]*2%Mod; if(j!=1)D[j] = (D[j] + D[j-2] * (j-1) % Mod * i)%Mod; } Res = Res * D[C[i]] % Mod; } printf("%lld\n",Res); return 0; fail: puts("0"); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <cassert> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} // head const int N=101000; VI vp,e[N],s[N]; int vis[N],cyc[N],inc[N],a[N],pre[N],n,cycnt[N]; ll ret,f[N]; void dfs(int u) { vp.pb(u); vis[u]=1; for (auto v:e[u]) if (!vis[v]) dfs(v); } bool check(VI &v) { int c=v[0]; for (auto u:v) vis[u]=0; int m=0; while (1) { vis[c]=1; c=a[c]; if (vis[c]==1) break; } int d=c; while (1) { cyc[m++]=c; inc[c]=1; c=a[c]; if (c==d) break; } for (auto u:v) vis[u]=1; if (m==SZ(v)) { cycnt[m]++; return 1; } for (auto u:v) { if (SZ(s[u])>2) return 0; if (SZ(s[u])==2&&!inc[u]) return 0; } rep(i,0,m) pre[cyc[i]]=cyc[(i+m-1)%m]; rep(i,0,m) { int u=cyc[i]; if (SZ(s[u])==1) continue; int nbr=(s[u][0]==pre[u])?s[u][1]:s[u][0]; VI chain; chain.pb(nbr); while (1) { if (SZ(s[nbr])==0) break; assert(SZ(s[nbr])==1); nbr=s[nbr][0]; chain.pb(nbr); } bool v1=1,v2=1; nbr=pre[u]; rep(j,0,SZ(chain)-1) { if (SZ(s[nbr])==2) v1=0,v2=0; nbr=pre[nbr]; } if (SZ(s[nbr])==2) v2=0; if (v1==0) return 0; if (v2==1) ret=ret*2%mod; } return 1; } ll fac[N],fnv[N]; ll comb(int x,int y) { return fac[x]*fnv[y]%mod*fnv[x-y]%mod; } int main() { ret=1; scanf("%d",&n); rep(i,1,n+1) scanf("%d",a+i); fac[0]=fnv[0]=1; rep(i,1,n+1) fac[i]=fac[i-1]*i%mod,fnv[i]=powmod(fac[i],mod-2); rep(i,1,n+1) { e[i].pb(a[i]); e[a[i]].pb(i); s[a[i]].pb(i); } rep(i,1,n+1) if (!vis[i]) { vp.clear(); dfs(i); if (!check(vp)) { puts("0"); return 0; } } // deal with cycle f[0]=1; rep(i,1,n+1) f[i]=f[i-1]*(2*i-1)%mod; rep(i,1,n+1) if (cycnt[i]!=0) { ll r=0; for (int j=0;2*j<=cycnt[i];j++) { r=(r+comb(cycnt[i],2*j)*f[j]%mod*powmod(i,j)%mod*powmod((i%2==1&&i!=1)?2:1,cycnt[i]-2*j))%mod; } ret=ret*r%mod; } printf("%lld\n",ret); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; const int maxn=100010,mod=1000000007,inv2=500000004; #define lson o<<1,l,mid #define rson o<<1|1,mid+1,r #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define ROF(i,a,b) for(int i=(a);i>=(b);i--) #define MEM(x,v) memset(x,v,sizeof(x)) inline int read(){ int x=0,f=0;char ch=getchar(); while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar(); while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar(); return f?-x:x; } int n,a[maxn],deg[maxn],ans=1,stk[maxn],tp,q[maxn],h,r,len[maxn],seq[maxn],tot,cnt[maxn]; int fac[maxn],inv[maxn],invfac[maxn]; bool vis[maxn],ins[maxn],cyc[maxn]; void dfs(int u){ if(vis[u]){ if(ins[u]){ ROF(i,tp,1){ cyc[stk[i]]=true; if(stk[i]==u) break; } } return; } vis[u]=ins[u]=true; stk[++tp]=u; dfs(a[u]); ins[u]=false; } bool dfs2(int u){ if(vis[u]) return true; vis[u]=true; seq[++tot]=len[u]; return dfs2(a[u]) && !len[u]; } inline int C(int n,int m){ return 1ll*fac[n]*invfac[m]%mod*invfac[n-m]%mod; } inline int qpow(int a,int b){ int ans=1; for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod; return ans; } int main(){ n=read(); FOR(i,1,n) a[i]=read(),deg[a[i]]++; FOR(i,1,n) dfs(i); FOR(i,1,n) if(cyc[i] && deg[i]>=3|| !cyc[i] && deg[i]>=2) return puts("0"),0; h=1;r=0; FOR(i,1,n) if(!deg[i]) q[++r]=i; while(h<=r){ int u=q[h++]; len[a[u]]=len[u]+1; if(!cyc[a[u]]) q[++r]=a[u]; } MEM(vis,0); FOR(i,1,n) if(cyc[i] && !vis[i]){ tot=0; if(dfs2(i)) cnt[tot]++; else{ int pre=0; FOR(j,1,tot) if(seq[j]){ if(pre){ int at=j-seq[j]; if(at<pre) return puts("0"),0; if(at>pre && tot>=2) ans=2*ans%mod; } pre=j; } FOR(j,1,tot) if(seq[j]){ int at=j-seq[j]+tot; if(at<pre) return puts("0"),0; if(at>pre && tot>=2) ans=2*ans%mod; break; } } } fac[0]=fac[1]=inv[1]=invfac[0]=invfac[1]=1; FOR(i,2,n){ fac[i]=1ll*fac[i-1]*i%mod; inv[i]=mod-1ll*(mod/i)*inv[mod%i]%mod; invfac[i]=1ll*invfac[i-1]*inv[i]%mod; } FOR(i,1,n) if(cnt[i]){ int s=0; FOR(j,0,cnt[i]/2){ int x=1ll*C(cnt[i],2*j)*C(2*j,j)%mod*fac[j]%mod; if(j) x=1ll*x*qpow(inv2,j)%mod*qpow(i,j)%mod; if(i%2==1 && i!=1) x=1ll*x*qpow(2,cnt[i]-2*j)%mod; s=(s+x)%mod; } ans=1ll*ans*s%mod; } printf("%d\n",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> #include <vector> #include <algorithm> using namespace std; #define iter(i, n) for (int i = 1; i <= n; ++i) #define NR 101000 const int mod = 1e9 + 7; int n, a[NR], in[NR], in_x[NR], cnt[NR], f[NR], cc[NR], sz; bool vis[NR], ring[NR]; vector<int> R[NR]; int main() { scanf("%d", &n); iter(i, n) { scanf("%d", &a[i]); ++in[a[i]]; in_x[a[i]] = i; } iter(i, n) if (in[i] > 2) { puts("0"); return 0; } int ans = 1; iter(i, n) if (!vis[i]) { int x = i; bool is_ring = true; for (; !vis[x]; x = a[x]) { vis[x] = true; } if (!ring[x]) { ++sz; for (; !ring[x]; x = a[x]) { if (in[x] != 1) is_ring = false; ring[x] = true; R[sz].push_back(x); } } if (x != i) { bool is_chain = true; x = i; while (in[x] == 1) x = in_x[x]; if (in[x] != 0) { puts("0"); return 0; } int len = 1; for (vis[x] = true, x = a[x]; !ring[x]; x = a[x]) { ++len; vis[x] = true; if (in[x] != 1) { puts("0"); return 0; } } cc[x] = len; } else { if (is_ring) ++cnt[R[sz].size()]; } } iter(i, sz) { reverse(R[i].begin(), R[i].end()); int rem = 0; for (int x : R[i]) { if (cc[x] != 0) { if (rem > 1) { puts("0"); return 0; } else rem = cc[x]; } else if (rem != 0) { --rem; if (rem == 0) ans = ans * 2 % mod; } } if (rem != 0) { for (int x : R[i]) { if (cc[x] != 0) { if (rem > 1) { puts("0"); return 0; } else break; } else { --rem; if (rem == 0) { ans = ans * 2 % mod; break; } } } } } iter(k, n) { f[0] = 1; iter(i, cnt[k]) { f[i] = f[i - 1] * ((k % 2 && k != 1) ? 2 : 1) % mod; if (i > 1) f[i] = (f[i] + 1ll * (i - 1) * f[i - 2] % mod * k) % mod; } ans = 1ll * ans * f[cnt[k]] % mod; } printf("%d\n", ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #define ll long long #define P 1000000007 int a[100005],in[100005],col[100005],cnt[100005],n,L[100005];bool cir[100005];long long f[100005],ans=1; int main() { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&a[i]),++in[a[i]]; for(int i=1;i<=n;++i) { if(col[i]) continue;int x=i;for(;!col[x];x=a[x]) col[x]=i; if(col[x]!=i) continue;for(;!cir[x];x=a[x]) cir[x]=1; } for(int i=1;i<=n;++i) if((cir[i]&&in[i]>2)||(!cir[i]&&in[i]>1)) return puts("0"),0; for(int i=1;i<=n;++i){if(in[i]) continue;int x=i,tmp=0;while(!cir[x]) x=a[x],tmp++;L[x]=tmp;} for(int i=1;i<=n;++i) { if(!cir[i]) continue;int x=i,st=0,fir=0,id=0,len=0; while(cir[x]) { ++id;cir[x]=0; if(L[x]) { if(!fir){st=fir=id;len=L[x];x=a[x];continue;} else{(ans*=(L[x]<(id-st))+(L[x]<=(id-st)))%=P;if(!ans)return puts("0"),0;st=id;x=a[x];continue;} } x=a[x]; } if(fir){(ans*=(len<(id+fir-st))+(len<=(id+fir-st)))%=P;if(!ans)return puts("0"),0;} else cnt[id]++; } for(int i=1;i<=n;++i) { f[0]=1;if(!cnt[i]) continue; if(i>1&&(i%2)) for(int j=1;j<=cnt[i];++j) {f[j]=f[j-1]*2%P;if(j>1) (f[j]+=f[j-2]*(j-1)*i%P)%=P;} else for(int j=1;j<=cnt[i];++j){f[j]=f[j-1];if(j>1) (f[j]+=f[j-2]*(j-1)*i%P)%=P;} (ans*=f[cnt[i]])%=P;if(!ans)return puts("0"),0; } printf("%lld",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<cstring> using namespace std; typedef long long ll; const int N=100100,P=1000000007; int i,j,k,n,m,ch,ff,ans; int a[N],b[N],z[N],fg[N],num[N],f[N],Jc[N],Jc_[N],tm2[N],d[N],dp[N]; void R(int &x) { ff=x=0;ch=getchar(); while (ch<'0' || '9'<ch) { if (ch=='-') ff=1;ch=getchar();} while ('0'<=ch && ch<='9') x=x*10+ch-'0',ch=getchar(); if (ff) x=-x; } int ksm(int x,int y) { int z=1; for (;y;y>>=1,x=(ll) x*x%P) if (y&1) z=(ll) z*x%P; return z; } void pre(int n) { int i; Jc[0]=Jc_[0]=f[0]=tm2[0]=1; for (i=1;i<=n;i++) tm2[i]=(tm2[i-1]+tm2[i-1])%P; for (i=1;i<=n;i++) f[i]=(ll) f[i-1]*(2*i-1)%P; for (i=1;i<=n;i++) Jc[i]=(ll) Jc[i-1]*i%P; Jc_[n]=ksm(Jc[n],P-2); for (i=n-1;i;i--) Jc_[i]=(ll) Jc_[i+1]*(i+1)%P; } int C(int n,int m) { return (ll) Jc[n]*Jc_[m]%P*Jc_[n-m]%P; } int Js(int n,int m) { int i,k,ans=0; if ((m&1) && m>1) { for (i=0,k=1;i+i<=n;i++,k=(ll) k*m%P) ans=((ll) f[i]*k%P*C(n,i+i)%P*tm2[n-i-i]%P+ans)%P; } else { for (i=0,k=1;i+i<=n;i++,k=(ll) k*m%P) ans=((ll) f[i]*k%P*C(n,i+i)%P+ans)%P; } return ans; } int main() { R(n); pre(n); for (i=1;i<=n;i++) R(a[i]); for (i=1;i<=n;i++) d[a[i]]++; for (i=1;i<=n;i++) if (!z[i]) { for (j=i;z[j]!=i && !z[j];j=a[j]) z[j]=i; if (z[j]==i) { fg[j]=1; for (k=a[j];k!=j;k=a[k]) fg[k]=1; } } for (i=1;i<=n;i++) if (d[i]>fg[i]+1) return puts("0"),0; for (i=1;i<=n;i++) if (!fg[i]) b[a[i]]=i; for (i=1;i<=n;i++) if (fg[i]) { for (j=b[i];j;j=b[j]) dp[i]++; } memset(z,0,sizeof z); ans=1; for (i=1;i<=n;i++) if (!z[i] && fg[i]) { int nm=0,Fg=0; for (j=i;!z[j];j=a[j]) { z[j]=1; nm++; if (dp[j]) Fg=j; } if (!Fg) num[nm]++; else { k=0; for (j=a[Fg];j!=Fg;j=a[j]) { k++; if (dp[j]) { if (dp[j]>k) ans=0; if (dp[j]<k) ans=(ans+ans)%P; k=0; } } k++; if (dp[Fg]>k) ans=0; if (dp[Fg]<k) ans=(ans+ans)%P; } } for (i=1;i<=n;i++) if (num[i]) ans=(ll) ans*Js(num[i],i)%P; printf("%d\n",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<iostream> #include<cstdio> using namespace std; #define N 200127 #define mod 1000000007 inline int M(int x){return (x>=mod)?(x-mod):x;} inline int M1(int x){return (x<0)?(x+mod):x;} int n,a[N],d[N],c[N],vis[N],ans,f[N],len[N],s[N]; //c:color,vis:oncircle int main(){ scanf("%d",&n);int i,j,x,y,p,st,la,lal;ans=1; for(i=1;i<=n;i++)scanf("%d",&a[i]),++d[a[i]]; for(i=1;i<=n;i++) if(!c[i]) { x=i;while(!c[x]){c[x]=i;x=a[x];} if(c[x]!=i)continue;while(!vis[x]){vis[x]=1;x=a[x];} } for(i=1;i<=n;i++) if((vis[i]&&(d[i]>2))||((!vis[i])&&(d[i]>1))) {puts("0");return 0;} for(i=1;i<=n;i++) if(!d[i]){p=0;x=i;while(!vis[x]){++p;x=a[x];}len[x]=p;} for(i=1;i<=n;i++)//基环树 if(vis[i]) { x=i;p=st=la=lal=0; while(vis[x]) { ++p;vis[x]=0; if(len[x]) { if(!st){st=la=p;lal=len[x];} else{ans=1ll*ans*((len[x]<(p-la))+(len[x]<=(p-la)))%mod;la=p;} }x=a[x]; } if(st)ans=1ll*ans*((lal<(p+st-la))+(lal<=(p+st-la)))%mod;//是基环树 else ++s[p]; } for(i=1;i<=n;i++)//正常环 if(s[i]) { f[0]=1;const int up=s[i]; if((i>1)&&(i&1)) { for(j=1;j<=up;j++) f[j]=M(f[j-1]+f[j-1]),f[j]=M(f[j]+((j>1)?(1ll*f[j-2]*(j-1)%mod*i%mod):0));//就像PKUSC中的sg一样 } else { for(j=1;j<=up;j++) f[j]=f[j-1],f[j]=M(f[j]+((j>1)?(1ll*f[j-2]*(j-1)%mod*i%mod):0));//有i种方式 }ans=1ll*ans*f[up]%mod; }printf("%d",ans);return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 9, P = 1e9 + 7; int a[N], n, Gr[N], s[N], t, la, h[N], sh, f[N], ans = 1; bool v[N], inh[N], is[N]; vector<int>V[N]; unordered_map<int, int>T; void Ext () { puts("0"); exit(0); } void Dfs (int x, int fr) { is[x] = 1; s[++t] = x; v[x] = 1; for (auto v : V[x]) if (v != fr) { if (is[v]) { la = v; break ; } else { Dfs(v, x); if (la != -2) break ; } } else fr = -1; if (la > 0) { h[sh++] = x; inh[x] = sh; if (Gr[x] > 3) Ext(); if (x == la) la = -1; } is[x] = 0; --t; } namespace T1 { void Dfs (int u, int fa) { if (!inh[u]) { v[u] = 1; if (Gr[u] > 2) Ext(); } for (auto v : V[u]) if (!inh[v] && v != fa) { Dfs(v, u); f[u] = f[v] + 1; } } int main () { for (int i = 0; i < sh; ++i) Dfs(h[i], 0); int ans = 1, i = 0, j, k; while (i < sh && !f[h[i]]) ++i; if (i == sh) return ++T[sh], 1; do if (f[h[i]]) { for (j = (i + 1) % sh, k = 1; !f[h[j]]; j = (j + 1) % sh, ++k); int ret = 0; if (f[h[i]] < k) ++ret; if (f[h[i]] <= k) ++ret; ans = 1ll * ans * ret % P; if (j <= i) break ; } while (i = j, 1); return ans; } } int Sol (int m, int n) { f[0] = f[1] = 1; int b = 1 + (m > 1 && (m & 1)); for (int i = 1; i <= n; ++i) f[i] = (1ll * f[i - 2] * m % P * (i - 1) + f[i - 1] * b) % P; return f[n]; } int main () { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); V[i].emplace_back(a[i]), V[a[i]].emplace_back(i); ++Gr[i]; ++Gr[a[i]]; } for (int i = 1; i <= n; ++i) if (!v[i]) { la = -2, sh = 0, Dfs(i, 0); if (sh > 1 && a[h[0]] == h[1]) reverse(h, h + sh); ans = 1ll * ans * T1::main() % P; } for (auto v : T) ans = 1ll * ans * Sol(v.first, v.second) % P; printf("%d\n", ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <memory.h> #include <math.h> #include <assert.h> #include <queue> #include <map> #include <set> #include <string> #include <algorithm> #include <iostream> #include <functional> #include <unordered_map> #include <list> using namespace std; typedef pair<int, int> Pi; typedef long long ll; #define Fi first #define Se second #define pb(x) push_back(x) #define sz(x) (int)x.size() #define rep(i, n) for(int i=0;i<n;i++) #define all(x) x.begin(), x.end() int N, A[100010]; int deg[100010]; int cyc[100010]; map <int, int> OC; int p[100010], z[100010]; int Find(int x){return p[x] == x ? x : p[x] = Find(p[x]); } int chk[100010]; const int MOD = 1e9 + 7; ll D[100010]; int d[100010]; int get(int X){ int res = 1; vector <int> v; v.pb(d[X]); int t = A[X]; while(t != X){ v.pb(d[t]); t = A[t]; } int L = sz(v); for(int S=0;S<L;S++)if(v[S] != 1){ for(int j=0;j<L;){ int t = v[(S-j+L)%L]; if(t == 0){j++; continue; } for(int k=1;k<t;k++){ if(v[(S-j-k+L+L)%L] > 0)return 0; } if(v[(S-j-t+L+L)%L] > 0){ j += t; } else{ j += t + 1; res = res * 2 % MOD; } } break; } return res; } void solve(){ scanf("%d", &N); for(int i=1;i<=N;i++)scanf("%d", A+i); for(int i=1;i<=N;i++)deg[A[i]]++; queue <int> que; for(int i=1;i<=N;i++)if(deg[i] == 0)que.push(i); while(!que.empty()){ int t = que.front(); que.pop(); deg[A[t]]--; d[A[t]] = d[t] + 1; if(deg[A[t]] == 0){ que.push(A[t]); } } for(int i=1;i<=N;i++)cyc[i] = deg[i], deg[i] = 0; for(int i=1;i<=N;i++)deg[A[i]]++; for(int i=1;i<=N;i++){ if(deg[i] > 2 || (deg[i] == 2 && cyc[i] == 0)){ printf("0"); return; } } for(int i=1;i<=N;i++)p[i] = i, z[i] = 1; for(int i=1;i<=N;i++){ int x = Find(i), y = Find(A[i]); if(x != y)p[x] = y, z[y] += z[x]; } for(int i=1;i<=N;i++)if(deg[i] == 2)chk[Find(i)] = 1; for(int i=1;i<=N;i++)if(Find(i) == i && chk[i] == 0)OC[z[i]]++; ll ans = 1; for(auto e : OC){ D[0] = 1; int t = ((e.Fi % 2 == 1 && e.Fi > 1) ? 2 : 1); for(int i=1;i<=e.Se;i++){ D[i] = t * D[i-1] + (i > 1 ? ((ll)(i-1) * e.Fi % MOD) * D[i-2] : 0); D[i] %= MOD; } ans = ans * D[e.Se] % MOD; } for(int i=1;i<=N;i++)if(Find(i) == i && chk[i] == 1){ ans = ans * get(i) % MOD; } printf("%lld\n", ans); } int main(){ int Tc = 1; //scanf("%d", &Tc); for(int tc=1;tc<=Tc;tc++){ solve(); } return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> using namespace std; typedef long long i64; const int MAXN = 100000 + 5; const int MOD = 1e9 + 7; int N; i64 answer, dp[MAXN]; int degree[MAXN], lenCnt[MAXN]; int pre[MAXN], nxt[MAXN]; bool vis[MAXN]; bool noSolution = false; void findCycle() { for (int i = 1; i <= N; i++) { if (degree[i] > 2) return noSolution = true, (void) 0; if (degree[i] != 2 || vis[i]) continue; int cur = i; do { if (vis[cur]) return noSolution = true, (void) 0; vis[cur] = true; pre[nxt[cur]] = cur; cur = nxt[cur]; } while (cur != i); } } void processCircleBasedTree() { for (int i = 1; i <= N; i++) { if (degree[i]) continue; int cur = i, footLen = 0, cycleLen = 0; while (!vis[cur]) vis[cur] = true, cur = nxt[cur], footLen++; do cur = pre[cur], cycleLen++; while (degree[cur] == 1); if (footLen < cycleLen) answer = answer * 2 % MOD; if (footLen > cycleLen) return noSolution = true, (void) 0; } } void countCycle() { for (int i = 1; i <= N; i++) { if (vis[i]) continue; int cur = i, len = 0; do { vis[cur] = true; cur = nxt[cur]; len++; } while (cur != i); lenCnt[len]++; } } int main() { answer = 1; ios::sync_with_stdio(false), cin.tie(nullptr); cin >> N; for (int i = 1; i <= N; i++) cin >> nxt[i], degree[nxt[i]]++; findCycle(); if (noSolution) return cout << 0 << endl, 0; processCircleBasedTree(); if (noSolution) return cout << 0 << endl, 0; countCycle(); if (noSolution) return cout << 0 << endl, 0; for (int i = 1; i <= N; i++) { dp[0] = 1LL; for (int j = 1; j <= lenCnt[i]; j++) { dp[j] = dp[j - 1] * (((i % 2 == 0) || i == 1) ? 1 : 2); if (j >= 2) dp[j] = (dp[j] + ((((dp[j - 2] * (j - 1)) % MOD) * i) % MOD)) % MOD; } answer = answer * dp[lenCnt[i]] % MOD; } cout << answer << endl; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> const int N=100005,mo=1000000007; int deg[N],a[N],vis[N],b[N],cnt[N],f[N]; bool u[N]; int calc(int x,int y){return (x<y)+(x<=y);} int main(){ int n,ans=1; scanf("%d\n",&n); for (int i=1;i<=n;i++){ scanf("%d",&a[i]); deg[a[i]]++; } for (int i=1;i<=n;i++) if (!vis[i]){ int j=i; for (;!vis[j];j=a[j]) vis[j]=i; if (vis[j]==i) for (;!u[j];j=a[j]) u[j]=1; } for (int i=1;i<=n;i++) if (!deg[i]){ int j=i,k=0; for (;!u[j];j=a[j]) k++; b[j]=k; } else if (deg[i]>u[i]+1) ans=0; for (int i=1;i<=n && ans;i++) if (u[i]){ int x=0,y=0,k=0; for (int j=i;u[j];j=a[j]){ k++;u[j]=0; if (b[j]){ if (!x) x=b[j],y=k; else ans=ans*calc(b[j],k)%mo; k=0; } } if (!x) cnt[k]++; else ans=ans*calc(x,y+k)%mo; } for (int i=1;i<=n && ans;i++){ int x=(i&1)+(i>1);f[0]=1; for (int j=1;j<=cnt[i];j++){ f[j]=f[j-1]*x%mo; if (j>1) f[j]=(f[j]+1ll*(j-1)*i%mo*f[j-2])%mo; } ans=1ll*ans*f[cnt[i]]%mo; } printf("%d\n",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define mo 1000000007 #define pi 3.1415926535898 #define eps 1e-9 using namespace std; long long read(){ long long xx=0,flagg=1; char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') ch=getchar(); if(ch=='-'){ flagg=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ xx=xx*10+ch-'0'; ch=getchar(); } return xx*flagg; } void pus(long long xx,long long flagg){ if(xx<0){ putchar('-'); xx=-xx; } if(xx>=10) pus(xx/10,0); putchar(xx%10+'0'); if(flagg==1) putchar(' '); if(flagg==2) putchar('\n'); return; } long long n,i,j,zz[100005],x,sum[100005],book[100005],book2[100005]; long long top2,sum2,zhan[100005],zhan2[100005],book3[100005],ans; long long jc[100005],ny[100005],flag; long long top,nex[100005],to[100005],fir[100005]; void lj(int u,int v){ top++; nex[top]=fir[u]; fir[u]=top; to[top]=v; } int ss(int v){ int sum3=0; sum[v]=1-book2[v]; for(int top1=fir[v];top1;top1=nex[top1]) if(book2[to[top1]]==0){ sum[v]+=ss(to[top1]); sum3++; } if(sum3>=2) flag=1; return sum[v]; } long long ksm(long long u,long long v){ long long o=1; while(v){ if(v&1) o=o*u%mo; u=u*u%mo; v>>=1; } return o; } long long C(long long u,long long v){ return jc[u]*ny[v]%mo*ny[u-v]%mo; } int main(){ n=read(); for(i=1;i<=n;i++){ zz[i]=read(); lj(zz[i],i); } jc[0]=1; for(i=1;i<=n;i++) jc[i]=jc[i-1]*i%mo; ny[n]=ksm(jc[n],mo-2); for(i=n-1;i>=0;i--) ny[i]=ny[i+1]*(i+1)%mo; for(i=1;i<=n;i++) if(book[i]==0){ x=i; while(book[x]==0){ book[x]=i; x=zz[x]; } if(book[x]==i){ while(book2[x]==0){ book2[x]=1; x=zz[x]; } } } ans=1; for(i=1;i<=n;i++) if(book2[i]==1) ss(i); if(flag==1){ pus(0,2); return 0; } for(i=1;i<=n;i++) if(book2[i]==1){ x=i;sum2=0;top2=0; while(book2[x]!=2){ book2[x]=2; sum2++; if(sum[x]!=0){ top2++; zhan[top2]=sum[x]; zhan2[top2]=sum2; } x=zz[x]; } if(top2!=0){ zhan[top2+1]=zhan[1]; zhan2[top2+1]=zhan2[1]+sum2; for(j=1;j<=top2;j++) if(zhan[j+1]<zhan2[j+1]-zhan2[j]) ans=ans*2%mo; else if(zhan[j+1]>zhan2[j+1]-zhan2[j]) ans=0; } else book3[sum2]++; } for(j=1;j<=n;j++){ sum2=0; for(i=0;(i<<1)<=book3[j];i++) if((j&1)&&j!=1) sum2=(sum2+C(book3[j],i<<1)*jc[i<<1]%mo*ny[i]%mo*ksm(ksm(2,i),mo-2)%mo*ksm(j,i)%mo*ksm(2,book3[j]-(i<<1)))%mo; else sum2=(sum2+C(book3[j],i<<1)*jc[i<<1]%mo*ny[i]%mo*ksm(ksm(2,i),mo-2)%mo*ksm(j,i))%mo; ans=ans*sum2%mo; } pus(ans,2); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<cstring> #include<algorithm> #define MAXN 100000 #define MO 1000000007 using namespace std; int N,a[MAXN+5],d[MAXN+5],pre[MAXN+5]; int cnt[MAXN+5],f[MAXN+5]; bool vis[MAXN+5]; int main() { scanf("%d",&N); for(int i=1;i<=N;i++) scanf("%d",&a[i]),d[a[i]]++; for(int i=1;i<=N;i++) if(d[i]>2) { printf("0\n"); return 0; } for(int i=1;i<=N;i++) if(d[i]==2&&vis[i]==false) { int p=i; do { if(vis[p]==true) { printf("0\n"); return 0; } vis[p]=true; pre[a[p]]=p; p=a[p]; }while(p!=i); } int ans=1; for(int i=1;i<=N;i++)//处理基环内向树 if(d[i]==0) { int p=i,len1=0,len2=0; do { vis[p]=true; p=a[p]; len1++; }while(vis[p]==false); do { p=pre[p]; len2++; }while(d[p]==1); if(len1>len2) { printf("0\n"); return 0; } if(len2>len1) ans=2LL*ans%MO; } for(int i=1;i<=N;i++)//处理单个的循环 if(vis[i]==false) { int len=0,p=i; do { p=a[p]; vis[p]=true; len++; }while(p!=i); cnt[len]++; } for(int i=1;i<=N;i++) if(cnt[i]) { int mul=1; if(i%2==1&&i!=1) mul=2; f[0]=1,f[1]=mul; for(int j=2;j<=cnt[i];j++) f[j]=(1LL*f[j-2]*(j-1)%MO*i%MO+1LL*f[j-1]*mul%MO)%MO; ans=1LL*ans*f[cnt[i]]%MO; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <vector> #define MOD 1000000007 using namespace std; typedef long long ll; ll dp(int n,int m) { ll s1=1,s2=0; for(int i=1;i<=m;i++) { ll f=(s1*(((n&1)&&n>1)?2LL:1LL)+s2*(i-1)%MOD*n)%MOD; s2=s1; s1=f; } return s1; } int a[100005],sum[100005]; bool vis[100005],cir[100005]; vector <int> son[100005]; int getlen(int x) { vis[x]=1; if (!cir[x]&&son[x].size()>=2) { puts("0"); exit(0); } for(int i=0;i<son[x].size();i++) if (!cir[son[x][i]]) return getlen(son[x][i])+1; return 1; } int now[100005],l[100005],pos[100005]; ll calc(int x) { do { vis[x]=1; x=a[x]; } while (!vis[x]); int d=0,tot=0; do { cir[x]=1; x=a[x]; } while (!cir[x]); int t=x; do { pos[x]=++d; l[x]=getlen(x)-1; if (l[x]) now[++tot]=x; x=a[x]; } while (x!=t); if (!tot) { sum[d]++; return 1; } ll ans=1; for(int i=1;i<=tot;i++) { int p=now[i],q=now[i%tot+1]; int dis=(pos[q]-pos[p]+d)%d; if (!dis) dis=d; if (dis<l[q]) { puts("0"); exit(0); } if (dis>l[q]) ans=ans*2LL%MOD; } return ans; } int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); son[a[i]].push_back(i); if (son[a[i]].size()>2) { puts("0"); exit(0); } } ll ans=1; for(int i=1;i<=n;i++) if (!vis[i]) ans=(ans*calc(i))%MOD; for(int i=1;i<=n;i++) if (sum[i]) ans=ans*dp(i,sum[i])%MOD; printf("%lld\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> #include<vector> #define ll long long #define inf 0x3f3f3f3f #define mod 1000000007 #define maxn 200010 inline ll read() { ll x=0; char c=getchar(),f=1; for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1; for(;'0'<=c&&c<='9';c=getchar())x=x*10+c-'0'; return x*f; } inline void write(ll x) { static char buf[20]; int len=0; if(x<0)putchar('-'),x=-x; for(;x;x/=10)buf[len++]=x%10+'0'; if(!len)putchar('0'); else while(len)putchar(buf[--len]); } inline void writesp(ll x){write(x); putchar(' ');} inline void writeln(ll x){write(x); putchar('\n');} int p[maxn],vis[maxn],col[maxn],mark[maxn],dep[maxn],rt[maxn],deg[maxn]; int len[maxn],dis[maxn]; int cnt[maxn],f[maxn]; std::vector<int>a; int n,tot; int main() { // freopen("agc008E.in","r",stdin); // freopen("agc008E.out","w",stdout); n=read(); for(int i=1;i<=n;i++) p[i]=read(); for(int i=1;i<=n;i++) if(!vis[i]){ int now=i; while(!vis[now]){ a.push_back(now); vis[now]=1; now=p[now]; } if(col[now]){ int sz=a.size(); for(int j=sz-1;j>=0;j--) col[a[j]]=col[now],rt[a[j]]=rt[now],dep[a[j]]=dep[now]+(sz-j); } else{ int sz=a.size(),flag=0; ++tot; for(int j=sz-1;j>=0;j--){ col[a[j]]=tot; if(!flag)mark[a[j]]=1,rt[a[j]]=a[j],dep[a[j]]=0; else rt[a[j]]=now,dep[a[j]]=dep[a[j+1]]+1; flag|=(a[j]==now); } } a.clear(); } for(int i=1;i<=n;i++) if(!mark[i])++deg[p[i]]; for(int i=1;i<=n;i++) if(deg[i]>=2){ puts("0"); return 0; } for(int i=1;i<=n;i++) len[rt[i]]=std::max(len[rt[i]],dep[i]); memset(vis,0,sizeof(vis)); ll ans=1; for(int i=1;i<=n;i++) if(mark[i]&&!vis[i]){ a.push_back(i); vis[i]=1; int now=p[i]; while(now!=i){ a.push_back(now); vis[now]=1; now=p[now]; } int flag=0,sz=a.size(); for(int j=0;j<sz;j++) if(len[a[j]]){ flag=1; break; } if(flag){ int last=-1,first=-1; for(int j=0;j<sz;j++) if(len[a[j]]){ if(~first)dis[a[j]]=j-last; else first=j; last=j; } dis[a[first]]=first+sz-last; for(int j=0;j<sz;j++) if(len[a[j]]){ if(len[a[j]]<dis[a[j]])ans=ans*2%mod; else if(len[a[j]]>dis[a[j]]){ puts("0"); return 0; } } } else ++cnt[sz]; a.clear(); } for(int i=1;i<=n;i++){ int w=((i&1)&&(i>1))?2:1; f[0]=1; f[1]=w; for(int j=2;j<=cnt[i];j++) f[j]=(f[j-1]*w+(ll)(j-1)*f[j-2]%mod*i)%mod; ans=ans*f[cnt[i]]%mod; } writeln(ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> #define For(i, j, k) for(int i = j; i <= k; i++) using namespace std; const int N = 100010; const int Mod = 1e9 + 7; int n; int to[N], deg[N], pre[N]; int cnt[N], f[N]; bool vis[N]; int main(){ scanf("%d", &n); For(i, 1, n) scanf("%d", &to[i]), deg[to[i]]++; For(i, 1, n) if(deg[i] > 2){ puts("0"); return 0; } For(i, 1, n) if(deg[i] == 2 && !vis[i]){ int o = i; do{ pre[to[o]] = o; if(vis[o]){ puts("0"); return 0; } vis[o] = true; o = to[o]; }while(o != i); } int ans = 1; For(i, 1, n) if(!deg[i]){ int o = i, len = 0; while(!vis[o]) vis[o] = true, o = to[o], ++len; int len2 = 0; do{ o = pre[o]; ++len2; }while(deg[o] == 1); if(len < len2) ans = ans * 2 % Mod; else if(len > len2){ puts("0"); return 0; } } For(i, 1, n) if(!vis[i]){ int o = i, len = 0; do{ vis[o] = true; o = to[o]; ++len; }while(o != i); cnt[len]++; } For(i, 1, n) if(cnt[i]){ int x = i > 1 && i % 2 == 1 ? 2 : 1; f[0] = 1, f[1] = x; For(j, 2, cnt[i]) f[j] = (1ll * (j - 1) * i * f[j - 2] + f[j - 1] * x) % Mod; ans = 1ll * ans * f[cnt[i]] % Mod; } printf("%d\n", ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define rep(i,x,y) for (int i=(x);i<=(y);i++) #define ll long long using namespace std; const int N=1e5+10,mod=1e9+7; int n,cnt,head[N],vis[N],a[N]; int tot,cir_tot,q[N],fa[N],dep[N],mk[N],len[N]; int sum[N],ans,f[N]; struct edge{int to,nxt;}e[N<<1]; void adde(int x,int y){ e[++cnt].to=y; e[cnt].nxt=head[x]; head[x]=cnt; } void dfs(int u,int par){ vis[u]=1; tot++; for (int i=head[u],v;i;i=e[i].nxt) if ((i^1)!=par){ v=e[i].to; if (!vis[v]) fa[v]=u,dep[v]=dep[u]+1,dfs(v,i); else if (dep[v]<=dep[u]){ for (int i=u;i!=v;i=fa[i]) q[++cir_tot]=i,mk[i]=1; q[++cir_tot]=v,mk[v]=1; } } } int tmp; void dfs1(int u,int par){ if (par) tmp++; int son=0; for (int i=head[u],v;i;i=e[i].nxt) if (v=e[i].to,v!=par&&!mk[v]) dfs1(v,u),son++; if (son>1){puts("0"); exit(0);} } int main(){ scanf("%d",&n); cnt=1; rep (i,1,n) scanf("%d",&a[i]),adde(a[i],i),adde(i,a[i]); ans=1; rep (i,1,n) if (!vis[i]){ tot=cir_tot=0,dfs(i,-1); if (cir_tot==2&&q[1]==q[2]) cir_tot--; if (tot==cir_tot) sum[tot]++; else{ if (a[q[1]]!=q[2]) reverse(q+1,q+1+cir_tot); int st=1; rep (j,1,cir_tot){ tmp=0,dfs1(q[j],0),len[j]=tmp; if (tmp) st=j; } int len_2=0; for (int j=st%cir_tot+1;;j=j%cir_tot+1){ len_2++; if (len[j]){ ans=(ll)ans*((len[j]<=len_2)+(len[j]<len_2))%mod; len_2=0; } if (j==st) break; } } } rep (i,1,n){ if (!sum[i]) continue; f[0]=1; rep (j,1,sum[i]){ f[j]=f[j-1]; if (i>1&&(i&1)) f[j]=(f[j]+f[j-1])%mod; if (j>1) f[j]=(f[j]+(ll)f[j-2]*(j-1)%mod*i%mod)%mod; } ans=(ll)ans*f[sum[i]]%mod; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <set> #include <map> #include <cmath> #include <ctime> #include <cstdio> #include <bitset> #include <vector> #include <string> #include <cassert> #include <cstring> #include <cstdlib> #include <iomanip> #include <iostream> #include <algorithm> #define rep(i, x, y) for(int i = (int)x; i <= (int)y; i ++) #define fi first #define se second #define pb push_back #define mk make_pair using namespace std; typedef long long LL; const int N = 100005; const int P = (int)1e9 + 7; int st[N<<1], deg[N], d[N], n, vis[N], a[N], tot, ct[N], sz[N], ans=1, fac[N], inv[N]; int power(int x, int y){ int an = 1; for(; y; y >>= 1, x = (LL)x * x % P) if(y & 1) an = (LL)an * x % P; return an; } int C(int x, int y){ if(x < 0 || y < 0 || x < y) return 0; return (LL)fac[x] * inv[y] % P * inv[x - y] % P; } void renew(int &x, const int y){ x += y; if(x < 0) x += P; if(x >= P) x -= P; } void init(){ fac[0] = inv[0] = fac[1] = inv[1] = 1; rep(i, 2, n){ fac[i] = (LL)fac[i - 1] * i % P; inv[i] = power(fac[i], P - 2); } } int getdp(){ int fr = 0; rep(i, 1, tot) st[i + tot] = st[i]; rep(i, 1, tot) if(sz[st[i]]){ fr = i; break; } reverse(st + fr + 1, st + fr + tot); int a = fr + sz[st[fr]] - 1, b = a + 1, c = 1; rep(i, fr + 1, fr + tot - 1)if(sz[st[i]]){ int na = i + sz[st[i]] - 1, nb = na + 1, cc = 0; if(a < i) renew(cc, c); if(b < i) renew(cc, c); a = na; b = nb; c = cc; } int ans = 0; if(a < fr + tot) renew(ans, c); if(b < fr + tot) renew(ans, c); return ans; } int que[N], t; int main(){ scanf("%d", &n); rep(i, 1, n) scanf("%d", a + i); rep(i, 1, n){ deg[a[i]] ++; if(deg[a[i]] > 2){ puts("0"); exit(0); } } rep(i, 1, n) d[i] = deg[i]; rep(i, 1, n) if(!d[i]) que[++t] = i; rep(h, 1, t){ int u = que[h]; --d[a[u]]; vis[u] = 1; if(!d[a[u]]) que[++t] = a[u]; } rep(i, 1, n) if(vis[i] && deg[i] >= 2){ puts("0"); exit(0); } rep(i, 1, n) if(vis[i] && !deg[i]){ int x = i, cnt = 0; for(x = i; vis[x]; x = a[x])cnt++; sz[x] = cnt; } init(); ans=1; rep(i, 1, n)if(!vis[i]) { int u = i, cu = 1; tot = 0; int res = 0; do{ st[++tot] = u; cu &= !sz[u]; vis[u] = 1; u = a[u]; }while(u != i); if(cu){ ct[tot] ++; res = 1; }else{ res = getdp(); } ans = (LL)ans * res % P; } rep(i, 1, n) if(ct[i]) { int tot = ct[i]; int res = 0; for(int x = 0; x <= tot; x += 2){ int cb = 1; cb = (LL)cb * power(i, (x >> 1)) % P; cb = (LL)cb * C(tot, x) % P; cb = (LL)cb * fac[x] % P; cb = (LL)cb * inv[x >> 1] % P; cb = (LL)cb * power((P + 1) >> 1, x >> 1) % P; if((i > 1) && (i & 1)){ cb = (LL)cb * power(2, tot - x) % P; } renew(res, cb); } ans = (LL)ans * res % P; } printf("%d\n", ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> #include <iomanip> using namespace std; #define pb push_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; typedef double ld; typedef vector<int> vi; #define fi first #define se second #define fe first #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #define es(x,e) (int e=fst[x];e;e=nxt[e]) #define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e]) #define SZ 666666 int n,a[SZ],ff[SZ],sz[SZ]; Edg int gf(int x) {return ff[x]?ff[x]=gf(ff[x]):x;} void uni(int a,int b) { a=gf(a),b=gf(b); (a^b)?(ff[a]=b):0; } map<int,int> cp; bool ic[SZ],vv[SZ]; int dfs(int x) { int ans=0,chh=0; vv[x]=1; for esb(x,e,b) if(ic[b]||vv[b]);else { int t=dfs(b);++chh; if(t<0) return -1; ans=max(ans,t+1); } if(chh>=2) return -1; return ans; } int py(int a,int b) { if(b>a) return 0; if(a==b) return 1; return 2; } const ll MOD=1e9+7; ll f[SZ]; int main() { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",a+i),adde(i,a[i]),uni(i,a[i]); for(int i=1;i<=n;++i) ++sz[gf(i)]; ll ans=1; for(int i=1;i<=n;++i) if(!ff[i]) { int g=i; for(int j=1;j<=sz[i];++j) g=a[g]; vector<int> cyc; cyc.pb(g); for(int h=a[g];h!=g;h=a[h]) cyc.pb(h); if(cyc.size()==sz[i]) {++cp[sz[i]]; continue;} for(auto r:cyc) ic[r]=1; vector<int> tmp; for(auto r:cyc) { int t=dfs(r); if(t<0) {puts("0"); return 0;} tmp.pb(t); } int cl=0,pv=-1,bl=0; for(int i=0;i<tmp.size();++i) { ++cl; if(!tmp[i]) continue; if(pv==-1) bl=cl,pv=tmp[i]; else ans*=py(cl,tmp[i]),ans%=MOD; cl=0; } ans*=py(bl+cl,pv); ans%=MOD; } for(auto g:cp) { int x=g.fi,y=g.se; f[0]=1; for(int i=1;i<=y;++i) f[i]=(((i>=2)?(f[i-2]*(i-1)%MOD*x):0)+(1+(x%2==1&&x>1))*f[i-1])%MOD; ans*=f[y]; ans%=MOD; } cout<<ans<<"\n"; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<algorithm> using namespace std; #define MAXN 100010 #define MO 1000000007 #define LL long long int a[MAXN],n,vis[MAXN],du[MAXN],pre[MAXN],cnt[MAXN]; LL fac[MAXN],inv[MAXN],Pow2[MAXN],P[MAXN],ans=1; LL PowMod(LL a,int b) { LL ret=1; while(b) { if(b&1) ret=ret*a%MO; a=a*a%MO; b>>=1; } return ret; } LL C(int n,int m) { return fac[n]*inv[m]%MO*inv[n-m]%MO; } void Pre() { fac[0]=1; for(int i=1;i<MAXN;i++) fac[i]=fac[i-1]*i%MO; inv[MAXN-1]=PowMod(fac[MAXN-1],MO-2); for(int i=MAXN-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%MO; Pow2[0]=1; for(int i=1;i<MAXN;i++) Pow2[i]=2*Pow2[i-1]%MO; P[1]=1; for(int i=2;i<MAXN;i++) P[i]=P[i-1]*(2*i-1)%MO; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); du[a[i]]++; } for(int i=1;i<=n;i++) { if(du[i]>2) { printf("0\n"); return 0; } if(du[i]<2||vis[i]) continue; int p=i; do { if(vis[p]) { printf("0\n"); return 0; } vis[p]=1,pre[a[p]]=p;p=a[p]; }while(p!=i); } for(int i=1;i<=n;i++) if(!du[i]) { int l1=0,l2=0,p=i; while(!vis[p]) { vis[p]=1,p=a[p],l1++; } do { p=pre[p];l2++; }while(du[p]!=2); if(l1<l2) ans=ans*2%MO; else if(l1>l2) { printf("0\n"); return 0; } } for(int i=1;i<=n;i++) if(!vis[i]) { int p=i,c=0; while(!vis[p]) { c++;vis[p]=1; p=a[p]; } cnt[c]++; } Pre(); for(int i=1;i<=n;i++) if(cnt[i]) { if((i&1)&&i!=1) { LL tmp=Pow2[cnt[i]]; LL x=i; for(int j=1;2*j<=cnt[i];(x*=i)%=MO,j++) (tmp+=C(cnt[i],2*j)*P[j]%MO*x%MO*Pow2[cnt[i]-2*j]%MO)%=MO; ans*=tmp; ans%=MO; } else { LL tmp=1; LL x=i; for(int j=1;2*j<=cnt[i];(x*=i)%=MO,j++) (tmp+=C(cnt[i],2*j)*P[j]%MO*x%MO)%=MO; ans*=tmp; ans%=MO; } } printf("%lld\n",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #define ll long long #define MOD 1000000007 using namespace std; inline int read(){ int re=0,flag=1;char ch=getchar(); while(!isdigit(ch)){ if(ch=='-') flag=-1; ch=getchar(); } while(isdigit(ch)) re=(re<<1)+(re<<3)+ch-'0',ch=getchar(); return re*flag; } ll f[200010],finv[200010],meth[200010]; ll qpow(ll a,ll b){ ll re=1; while(b){ if(b&1) re=(re*a)%MOD; a=a*a%MOD;b>>=1; } return re; } void init(){ ll i,len=200000; f[0]=f[1]=finv[0]=finv[1]=1; for(i=2;i<=len;i++) f[i]=f[i-1]*i%MOD; finv[len]=qpow(f[len],MOD-2); for(i=len;i>2;i--) finv[i-1]=finv[i]*i%MOD; } int n,a[200010],vis[200010],cir[200010],cntcir=0,in[200010],bst[200010],siz[200010]; ll ans=1; vector<int>s; vector<int>nd[200010]; bool cmp(int l,int r){ return siz[l]<siz[r]; } ll C(ll x,ll y){ return f[x]*finv[y]%MOD*finv[x-y]%MOD; } int main(){ n=read();int i,j;ll tmp,c,cc; init(); meth[0]=1; for(i=1;i<=n;i++) meth[i]=C(i*2,i)*f[i]%MOD*qpow(qpow(2,MOD-2),i)%MOD; for(i=1;i<=n;i++) a[i]=read(),in[a[i]]++; for(i=1;i<=n;i++){//取出环 j=i; while(!vis[j]) vis[j]=i,j=a[j]; if(vis[j]^i) continue; cntcir++; while(!cir[j]){ cir[j]=cntcir,nd[cntcir].push_back(j),siz[cntcir]++,j=a[j]; } } memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++){//判断环套树,以及外挂树是不是都是链 if(in[i]) continue; j=i; while(!cir[j]&&!vis[j]) j=a[j]; if(vis[j]){ puts("0");return 0; } bst[cir[j]]=1;tmp=cir[j]; j=i;c=0; while(!cir[j]) cir[j]=tmp,c++,vis[j]=1,j=a[j]; vis[j]=c; } for(i=1;i<=cntcir;i++){//环套树处理 if(!bst[i]){s.push_back(i);continue;} for(j=0;j<nd[i].size();j++) if(vis[nd[i][j]]) break; tmp=j; do{ c=j;cc=1; j--; (j+=(int)nd[i].size()); j%=(int)nd[i].size(); for(;!vis[nd[i][j]];j--,j=((j<0)?j+nd[i].size():j)) cc++; if(vis[nd[i][c]]>cc){ puts("0");return 0; } if(vis[nd[i][c]]<cc) (ans*=2)%=MOD; }while(tmp!=j); } sort(s.begin(),s.end(),cmp); for(i=0;i<s.size();i+=c){ j=i;tmp=0; while(siz[s[j]]==siz[s[i]]&&j<s.size()) j++; c=j-i; for(j=0;j<=c/2;j++){ (tmp+=C(c,2*j)*meth[j]%MOD*qpow(siz[s[i]],j)%MOD*qpow(2,(c-2*j)*(siz[s[i]]!=1)*(siz[s[i]]&1))%MOD)%=MOD; } ans=ans*tmp%MOD; } printf("%lld\n",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<cstring> #include<algorithm> #define MAXN 100000 #define MO 1000000007 using namespace std; int N,a[MAXN+5],d[MAXN+5],pre[MAXN+5]; int cnt[MAXN+5],f[MAXN+5]; bool vis[MAXN+5]; int main() { // freopen("test.in","r",stdin); // freopen("test.out","w",stdout); scanf("%d",&N); for(int i=1;i<=N;i++) scanf("%d",&a[i]),d[a[i]]++; for(int i=1;i<=N;i++) if(d[i]>2) { printf("0\n"); return 0; } for(int i=1;i<=N;i++) if(d[i]==2&&vis[i]==false) { int p=i; do { if(vis[p]==true) { printf("0\n"); return 0; } vis[p]=true; pre[a[p]]=p; p=a[p]; }while(p!=i); } int ans=1; for(int i=1;i<=N;i++)//处理基环内向树 if(d[i]==0) { int p=i,len1=0,len2=0; do { vis[p]=true; p=a[p]; len1++; }while(vis[p]==false); do { p=pre[p]; len2++; }while(d[p]==1); if(len1>len2) { printf("0\n"); return 0; } if(len2>len1) ans=2LL*ans%MO; } for(int i=1;i<=N;i++)//处理单个的循环 if(vis[i]==false) { int len=0,p=i; do { p=a[p]; vis[p]=true; len++; }while(p!=i); cnt[len]++; } for(int i=1;i<=N;i++) if(cnt[i]) { int mul=1; if(i%2==1&&i!=1) mul=2; f[0]=1,f[1]=mul; for(int j=2;j<=cnt[i];j++) f[j]=(1LL*f[j-2]*(j-1)%MO*i%MO+1LL*f[j-1]*mul%MO)%MO; ans=1LL*ans*f[cnt[i]]%MO; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> #include <cstdlib> #define ref(i,x,y)for(int i=x;i<=y;++i) const int N=100005,mod=1e9+7; void print(int x){printf("%d\n",x);exit(0);} int n,m,l,res,a[N],cnt,head[N],h[N],w[N*2],p[N],f[N];bool b[N],vis[N]; struct edge{int to,nxt;}e[N]; void add(int x,int y){e[++cnt]=(edge){y,head[x]};head[x]=cnt;} int dfs(int f,int x){ vis[x]=1; int Y=0,t=0; for(int i=head[x];i;i=e[i].nxt){ int y=e[i].to;if(y!=f)Y=y,t++; } if(t>1)return 0; if(t==0)return 1; int s=dfs(x,Y);if(s)++s; return s; } int main(){ scanf("%d",&n); ref(i,1,n)scanf("%d",&a[i]),add(a[i],i); res=1; ref(i,1,n)if(!vis[i]){ m=l=0; for(int x=i;;b[x]=1,x=a[x]){ h[++m]=x;if(b[x])break; } bool fg=0; for(int x=m;x==m||h[x]!=h[m];x--){ w[++l]=dfs(h[x-1],h[x]); if(!w[l])print(0);w[l]--; } ref(i,1,l)w[i+l]=w[i]; ref(i,1,l)if(w[i]){ fg=1;int tt=0; ref(j,i+1,2*l)if(!w[j])tt++;else break; if(tt<w[i]-1)print(0); if(tt>w[i]-1)(res*=2)%=mod; } if(!fg)p[l]++; } ref(i,1,n)if(p[i]){ f[0]=1;if((i&1)&&i>1)f[1]=2;else f[1]=1; if((i&1)&&i>1) ref(j,2,p[i])f[j]=(1LL*f[j-2]*(j-1)%mod*i+2*f[j-1])%mod; else ref(j,2,p[i])f[j]=(1LL*f[j-2]*(j-1)%mod*i+f[j-1])%mod; res=1LL*res*f[p[i]]%mod; } print(res); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define re(i,a,b) for(int i=(a);i<(b);i++) #define repd(i,a,b) for(int i=(a);i>=(b);i--) #define clr(a) memset(a,0,sizeof(a)); #define il inline #define sz(a) ((int)o.size()) #define run(x) for(int k=head[x];k;k=e[k].ne) #define v e[k].t #define all(a) o.begin(),o.end() #define mp make_pair #define pb push_back #define w1 first #define w2 second #define adm(a,b,c) {a=a+b;if(a>=c)a-=c;else if(a<0)a+=c;} typedef double db; typedef long long ll;typedef long double ld;typedef unsigned long long ull; typedef pair<int,int> pa; const int N=1e6+5,M=1e7+5,INF=1e9,mod=1e9+7; const ll linf=1e18;const double eps=1e-8,pi=acos(-1); il int gmin(int &a,int b){if(a>b)a=b;}il ll gmin(ll &a,ll b){if(a>b)a=b;}il int gmax(int &a,int b){if(a<b)a=b;}il ll gmax(ll &a,ll b){if(a<b)a=b;} il void read(ll&x){ll f=1,t=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){t=t*10+ch-'0';ch=getchar();}x=t*f;}il ll read(ll&x,ll&y){read(x);read(y);} il void read(int&x){int f=1,t=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){t=t*10+ch-'0';ch=getchar();}x=t*f;}il int read(int&x,int&y){read(x);read(y);} il void read(int&a,int&b,int&c){read(a);read(b);read(c);}il void read(ll&a,ll&b,ll&c){read(a);read(b);read(c);} il int read(){int x;read(x);return x;} il ll qpow(ll a,ll b,ll p){ll ret=1;for(;b;b>>=1,a=a*a%p)if(b&1)ret=ret*a%p;return ret;}il ll qpow(ll a,ll b){ll ret=1;for(;b;b>>=1,a=a*a%mod)if(b&1)ret=ret*a%mod;return ret;} il ll qmul(ll a,ll b,ll p){ll ret=0;for(;b;b>>=1,a=(a<<1)%p)if(b&1)adm(ret,a,p);return ret;}il ll qmul(ll a,ll b){ll ret=0;for(;b;b>>=1,a=(a<<1)%mod)if(b&1)adm(ret,a,mod);return ret;} il void judge(){ freopen("dato.in","r",stdin); freopen("dato.out","w",stdout); } int n,res=1,a[N],vis[N],d[N],len[N],cnt[N]; ll f[N]; void gao(int n,int m){ ll f1=1;if(n>1&&n%2==1)f1++; f[0]=1;f[1]=f1; rep(i,2,m)(f[i]=f1*f[i-1]%mod+f[i-2]*(i-1)%mod*n%mod)%=mod; res=res*f[m]%mod; } int main(){ read(n);rep(i,1,n)read(a[i]),d[a[i]]++; rep(i,1,n)if(d[i]>2)return puts("0"),0; rep(i,1,n)if(!d[i]){ int x=i,sz=0; for(;d[x]<2;x=a[x])vis[x]=1,sz++; len[x]=sz; } rep(i,1,n)if(!vis[i]){ int x;vector<int>o; for(x=i;!vis[x];x=a[x])vis[x]=1,o.pb(x); if(x!=i)return puts("0"),0; int isc=1;re(j,0,o.size())if(len[o[j]])isc=0; if(isc)cnt[o.size()]++;else{ int tmp=1; repd(j,o.size()-1,0)if(!len[o[j]])tmp++;else break; re(j,0,o.size())if(len[o[j]]){ if(len[o[j]]<tmp)res=res*2%mod; if(len[o[j]]>tmp)return puts("0"),0; tmp=1; }else tmp++; } }rep(i,1,n)if(cnt[i])gao(i,cnt[i]); cout<<res<<endl; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; int N; vector<int> G[100010],ele; int A[100010]; bool vis[100010]; bool loop[100010]; int len[100010]; int par[100010]; int num[100010]; int nxt[100010]; int cnt; ll ans = 1; ll dp[100010]; ll mod = 1000000007; void DFS(int v) { if(vis[v])return; vis[v] = true; ele.push_back(v); for(int i = 0; i < G[v].size(); i++)DFS(G[v][i]); DFS(par[v]); return; } int main() { scanf("%d",&N); for(int i = 0; i < N; i++) { int a; scanf("%d",&a); G[a].push_back(i + 1); par[i + 1] = a; } for(int i = 1; i <= N; i++)if(!vis[i]) { cnt = 0; ele.clear(); DFS(i); int now = i; for(int j = 0; j < ele.size(); j++)now = par[now]; for(int j = 0; j < ele.size(); j++) { if(!loop[now])cnt++; loop[now] = true; now = par[now]; } if(cnt == ele.size()) { num[cnt]++; continue; } int sum = 1; for(int j = 0; j < ele.size() * 2; j++) { nxt[now] = sum; sum++; if(G[now].size() == 2)sum = 1; now = par[now]; } for(int j = 0; j < ele.size(); j++) { if(G[ele[j]].size() >= 3)ans = 0; if(!loop[ele[j]] && G[ele[j]].size() >= 2)ans = 0; if(G[ele[j]].size() == 0) { int l = 0; int tmp = ele[j]; while(!loop[tmp]) { l++; tmp = par[tmp]; } len[tmp] = l; } } for(int j = 0; j < ele.size(); j++) { int tmp = ele[j]; if(!loop[tmp] || G[tmp].size() == 1)continue; if(nxt[tmp] < len[tmp])ans = 0; if(nxt[tmp] > len[tmp])ans *= 2; ans %= mod; } } for(int i = 1; i <= N; i++) { for(int j = 0; j <= num[i]; j++)dp[j] = 0; dp[num[i]] = 1; for(int j = num[i]; j > 0; j--) { dp[j - 1] += dp[j]; dp[j - 1] %= mod; if(j - 2 >= 0)dp[j - 2] += dp[j] * (ll)(j - 1) % mod * (ll)i % mod; dp[j - 2] %= mod; if(i % 2 && i > 1)dp[j - 1] += dp[j]; dp[j - 1] %= mod; } ans *= dp[0]; ans %= mod; } printf("%lld\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<iostream> #include<cstdio> #include<cstring> template<typename T>inline void read(T &x) { char c=x=0; for(c=getchar();!isdigit(c);c=getchar()); for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48); } namespace gg { typedef long long ll; const int N=101000,MOD=1000000007; ll qpow(ll a,ll b){ll c=1;for(;b;b>>=1,a=a*a%MOD)if(b&1)c=c*a%MOD;return c;} struct disjoint_union_set { int q[N]; inline void init(int n){for(int i=1;i<=n;i++)q[i]=i;} inline int ask(int p){return p==q[p]?p:q[p]=ask(q[p]);} inline void link(int u,int v){q[ask(u)]=ask(v);} inline bool uni(int u,int v){return ask(u)==ask(v);} }d; ll fact[N],ifact[N]; bool is_cir[N],not_leave[N],vis[N]; int dep[N],cnt[N]; int s[N]; int n; ll initialize() { read(n),d.init(n); fact[0]=1; for(int i=1;i<=n;i++) { read(s[i]); not_leave[s[i]]=1; fact[i]=fact[i-1]*i%MOD; } ifact[n]=qpow(fact[n],MOD-2); for(int i=n;i;i--)ifact[i-1]=ifact[i]*i%MOD; for(int i=1;i<=n;i++) { if(d.uni(i,s[i])) { for(int p=i;!is_cir[p];p=s[p]) vis[p]=is_cir[p]=1; } d.link(i,s[i]); } for(int i=1;i<=n;i++) if(!not_leave[i]) { int p=i,w=0; for(;!vis[p];p=s[p]) vis[p]=1,w++; if(!is_cir[p] || dep[p])return 0; dep[p]=w; } ll ret=1; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) if(dep[i] && !vis[i]) { int p=s[i],w=1; for(;!vis[p];vis[p]=1,p=s[p]) { if(dep[p]>w)return 0; else if(dep[p]) { if(dep[p]<w)ret=ret*2%MOD; w=0; } w++; } } for(int i=1;i<=n;i++) if(!vis[i] && is_cir[i]) { int p=i,w=0; for(;!vis[p];vis[p]=1,p=s[p])w++; cnt[w]++; } return ret; } void solve() { ll ans,ret,res,tmp; if(!(ans=initialize())){printf("0\n");return;} for(int x=1;x<=n;x++) { int K=cnt[x];if(!K)continue; // printf("%d : %d\n",x,K); res=0; for(int i=0;i*2<=K;i++) { ret=fact[K]*ifact[K-i*2]%MOD*ifact[i]%MOD*qpow(x,i)%MOD; tmp=qpow(2,i),tmp=qpow(tmp,MOD-2); ret=ret*tmp%MOD; if(x>1 && x%2)ret=ret*qpow(2,K-2*i)%MOD; res=(res+ret)%MOD; } ans=ans*res%MOD; } printf("%lld\n",ans); } } int main() { // freopen("E.in","r",stdin); // freopen("E.out","w",stdout); gg::solve(); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> using namespace std; const int N = 200000; const int MOD = 1e9 + 7; int n; int ai[N], vis[N], cir[N], cnt[N], V[N]; vector <int> bi[N]; int ans = 1; int f[N], fac[N], inv[N]; int powi(int a, int b) { int c = 1; for (; b; b >>= 1, a = 1ll * a * a % MOD) if (b & 1) c = 1ll * c * a % MOD; return c; } int C(int a, int b) { return 1ll * fac[a] * inv[b] % MOD * inv[a - b] % MOD; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++ i) scanf("%d", &ai[i]), bi[ai[i]].push_back(i); for (int i = 1; i <= n; ++ i) if (!vis[i]) { int p = i; for (; !vis[p]; p = ai[p]) vis[p] = i; if (vis[p] == i) for (; !cir[p]; p = ai[p]) cir[p] = 1; } for (int i = 1; i <= n; ++ i) if (bi[i].size() - cir[i] > 1) return puts("0"), 0; for (int i = 1; i <= n; ++ i) if (bi[i].size() > 1 && !cir[bi[i][1]]) swap(bi[i][0], bi[i][1]); for (int i = 1; i <= n; ++ i) vis[i] = 0; for (int i = 1; i <= n; ++ i) if (cir[i] && !vis[i]) { int l = 0, G = 0; for (int j = i; !vis[j]; j = ai[j]) { l ++; vis[j] = 1; cnt[l] = 0; if (!cir[bi[j][0]]) for (int k = j; bi[k].size(); k = bi[k][0]) cnt[l] ++, G = 1; } if (!G) { V[l] ++; continue; } for (int j = 1; j <= l; ++ j) cnt[j + l] = cnt[j]; int x = 1; while (!cnt[x]) x ++; for (int j = x + 1, k = x; j <= x + l; ++ j) if (cnt[j]) { if (cnt[j] > j - k) return puts("0"), 0; else if (cnt[j] < j - k) ans = 2ll * ans % MOD; k = j; } } f[0] = 1; for (int i = 1; i <= n; ++ i) f[i] = 1ll * f[i - 1] * (2 * i - 1) % MOD; fac[0] = 1; for (int i = 1; i <= n; ++ i) fac[i] = 1ll * fac[i - 1] * i % MOD; inv[n] = powi(fac[n], MOD - 2); for (int i = n - 1; ~i; -- i) inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD; for (int i = 1; i <= n; ++ i) { int res = 0; for (int j = 0, k = 1; j <= V[i] / 2; ++ j, k = 1ll * k * i % MOD) res = (res + 1ll * C(V[i], j * 2) * f[j] % MOD * k % MOD * (i > 1 && (i & 1)? powi(2, V[i] - j * 2): 1)) % MOD; ans = 1ll * ans * res % MOD; } printf("%d\n", ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100, MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { a += b; if (a < 0) a += MOD; else if (a >= MOD) a -= MOD; return a; } int mul(int a, int b) { return 1LL * a * b % MOD; } void _sum(int &a, int b) { a = sum(a, b); } void _mul(int &a, int b) { a = mul(a, b); } int n, ans = 1, out[N], cnt[N], fac[N], inv[N], val[N], have[N], power[3][N]; bool mark[N]; vector<int> in[N]; void ZERO() { cout << "0\n"; exit(0); } void pre_pro() { fac[0] = 1; for (int i = 1; i < N; i++) fac[i] = mul(fac[i - 1], i); inv[0] = inv[1] = 1; for (int i = 2; i < N; i++) inv[i] = mul(MOD - MOD / i, inv[MOD % i]); for (int i = 2; i < N; i++) _mul(inv[i], inv[i - 1]); val[0] = 1; for (int i = 1; i < N; i++) val[i] = mul(2 * i - 1, val[i - 1]); for (int i = 1; i <= 2; i++) { power[i][0] = 1; for (int j = 1; j < N; j++) power[i][j] = mul(power[i][j - 1], i); } } int c(int m, int n) { return (m > n? 0: mul(fac[n], mul(inv[m], inv[n - m]))); } void input() { cin >> n; for (int i = 0; i < n; i++) { cin >> out[i]; in[--out[i]].push_back(i); } } void check() { for (int i = 0; i < n; i++) if (in[i].size() > 2) ZERO(); } void go(int v, int now = 0) { if (cnt[v]) ZERO(); cnt[v] = now; if (in[v].size() > 1) return; mark[v] = true; go(out[v], now + 1); } void set_len() { for (int i = 0; i < n; i++) if (in[i].empty()) go(i); } void dfs(int v, vector<int> *vec) { mark[v] = true; vec->push_back(v); if (mark[out[v]] == false) dfs(out[v], vec); } bool cycle(vector<int> vec) { for (int v: vec) if (cnt[v]) return false; return true; } void f(vector<int> vec) { vector<int> X; for (int i = 0; i < vec.size(); i++) if (cnt[vec[i]]) X.push_back(i); X.push_back(X[0]); for (int i = 1; i < X.size(); i++) { int a = (X[i] - X[i - 1] + vec.size()), b = cnt[vec[X[i]]]; if (a > vec.size()) a -= vec.size(); if (b > a) ZERO(); else if (b < a) _mul(ans, 2); } } void find_cycles() { for (int v = 0; v < n; v++) if (mark[v] == false) { vector<int> vec; dfs(v, &vec); if (cycle(vec)) have[vec.size()]++; else f(vec); } } int handle(int l) { int res = 0; int p = 1 + ((l & 1) && (l > 1)); int now = 1; for (int i = 0; 2 * i <= have[l]; i++) { _sum(res, mul(mul(c(2 * i, have[l]), val[i]), mul(power[p][have[l] - 2 * i], now))); _mul(now, l); } return res; } void check_cycles() { for (int i = 1; i < N; i++) _mul(ans, handle(i)); } int main() { ios::sync_with_stdio(false), cin.tie(0); pre_pro(); input(); check(); set_len(); find_cycles(); check_cycles(); cout << ans; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> #pragma GCC optimize ("O2,unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef pair<ll, ll> pll; #define debug(x) cerr<<#x<<'='<<(x)<<endl; #define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl; #define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl; #define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;} #define all(x) x.begin(), x.end() #define pb push_back #define kill(x) return cout<<x<<'\n', 0; const int inf=1000000010; const ll INF=10000000000000010LL; const int mod=1000000007; const int MAXN=100010, LOG=20; int n, m, k, u, v, x, y, t, a, b, ans=1; int A[MAXN], B[MAXN], ted[MAXN], deg[MAXN]; int mark[MAXN], last[MAXN], dor[MAXN]; ll dp[MAXN]; int main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); cin>>n; for (int i=1; i<=n; i++) cin>>A[i], deg[A[i]]++; for (int i=1; i<=n; i++) if (!mark[i]){ int v=i; while (mark[v]<=2){ if (last[v]!=i) last[v]=i, mark[v]=1; else mark[v]++; if (mark[v]==2) dor[v]=1; v=A[v]; } } for (int i=1; i<=n; i++) if (deg[i]>2 || (deg[i]==2 && !dor[i])) kill(0); for (int i=1; i<=n; i++) B[A[i]]^=i; memset(mark, 0, sizeof(mark)); for (int i=1; i<=n; i++) if (dor[i] && !mark[i]){ int v=i, bad=0; while (!mark[v]){ mark[v]++; B[A[v]]^=v; v=A[v]; } vector<int> vec; while (mark[v]<2){ mark[v]++; int len=0, u=v; while (!dor[B[u]] && B[u]){ u=B[u]; len++; } if (len) bad=1; vec.pb(len); v=A[v]; } // debugv(vec) if (!bad){ ted[vec.size()]++; continue ; } int c0=0; while (vec.back()==0) c0++, vec.pop_back(); for (int x:vec){ if (x==0) c0++; else{ if (x<=c0) ans=ans*2%mod; if (x-1>c0) kill(0) c0=0; } } } // debug(ans) for (int i=1; i<=n; i++) if (ted[i]){ ll z=1+(i>1 && i%2); dp[0]=1; dp[1]=z; for (int j=2; j<=ted[i]; j++) dp[j]=(dp[j-1]*z + (j-1ll)*i%mod*dp[j-2])%mod; ans=ans*dp[ted[i]]%mod; } cout<<ans<<"\n"; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <queue> #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define N 100100 #define fi first #define se second #define pb push_back #define mp make_pair #define mod 1000000007 #define rep(x, a, b) for(int x=a; x<=b; x++) #define drp(x, a, b) for(int x=a; x>=b; x--) int deg[N], d[N], n, vis[N], a[N], tot, num[N], l[N], ans=1, fac[N], inv[N]; vector<pair<int, int> > fu; queue<int> q; int power(int a, int k){ int ret=1; while(k) { if(k&1) ret=1ll*ret*a%mod; a=1ll*a*a%mod; k>>=1; } return ret; } int C(int n, int m){ return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod; } int pr(int n){ return 1ll*fac[2*n]*inv[n]%mod*power((mod+1)>>1, n)%mod; } void init(){ fac[0]=1; rep(i, 1, n) fac[i]=1ll*fac[i-1]*i%mod; inv[n]=power(fac[n], mod-2); drp(i, n, 1) inv[i-1]=1ll*inv[i]*i%mod; } int st[N<<1]; void renew(int &x, const int y){ x += y; if(x < 0) x += mod; if(x >= mod) x -= mod; } int getdp(){ int fr = 0; rep(i, 1, tot) st[i + tot] = st[i]; rep(i, 1, tot) if(l[st[i]]){ fr = i; break; } reverse(st + fr + 1, st + fr + tot); int a = fr + l[st[fr]] - 1, b = a + 1, c = 1; rep(i, fr + 1, fr + tot - 1)if(l[st[i]]){ int na = i + l[st[i]] - 1, nb = na + 1, cc = 0; if(a < i) renew(cc, c); if(b < i) renew(cc, c); a = na; b = nb; c = cc; } int ans = 0; if(a < fr + tot) renew(ans, c); if(b < fr + tot) renew(ans, c); return ans; } int main(){ scanf("%d", &n); init(); rep(i, 1, n) { scanf("%d", a+i); deg[a[i]]++; } rep(i, 1, n) if(deg[i]>2) return puts("0"), 0; rep(i, 1, n) d[i]=deg[i]; rep(i, 1, n) if(!d[i]) q.push(i); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=1; if(--d[a[u]]==0) q.push(a[u]); } rep(i, 1, n) if(vis[i] && deg[i]>1) return puts("0"), 0; rep(i, 1, n) if(vis[i] && !deg[i]) { int x=i, cnt=0; for(; vis[x]; x=a[x]) cnt++; l[x]=cnt; } rep(i, 1, n) if(!vis[i]) { int x=i, fl=0, ass=1; tot=0; for(; !vis[x]; x=a[x]) st[++tot]=x, fl|=bool(l[x]), vis[x]=1; if(!fl) num[tot]++; else {/* int p=0; fu.clear(); if(l[i]) fu.pb(mp(l[i], 0)); for(p++, x=a[i]; x!=i; x=a[x]) if(l[x]) fu.pb(mp(l[x], p)); fu.pb(mp(fu[0].fi, fu[0].se+tot)); int sz=fu.size(); rep(i, 1, sz-1) { if(fu[i].fi>fu[i].se-fu[i-1].se) ass=0; if(fu[i].fi<fu[i].se-fu[i-1].se) ass=2ll*ass%mod; }*/ ass=getdp(); } ans=1ll*ans*ass%mod; } rep(i, 1, n) if(num[i]) { int m=num[i], ass=0; rep(j, 0, m/2) { int tmp=1ll*C(m, 2*j)*pr(j)%mod*power(i, j)%mod; if(i>1 && (i&1)) tmp=1ll*tmp*power(2, m-2*j)%mod; ass=(ass+tmp)%mod; } ans=1ll*ans*ass%mod; } printf("%d\n", ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> #include <algorithm> using namespace std; const int md=1000000007,MX=100100; int n,i,j,k,cc,d,r=1,cur,all[MX],fi[MX],la[MX],a[MX],b[MX],c[MX],cnt[MX],cyc[MX],comp[MX],v[MX],gv[MX]; long long f[MX],g[MX]; bool u[MX],w[MX],ic[MX],was; int dfs(int i, int d) { b[d]=i; u[i]=true; w[i]=true; if (w[a[i]]) { c[i]=++cc; cyc[cc]=1; fi[cc]=cur; ic[a[i]]=true; all[cur++]=a[i]; for (int j=d; b[j]!=a[i]; j--, cyc[cc]++) { ic[b[j]]=true; all[cur++]=b[j]; } la[cc]=cur; } else if (u[a[i]]) c[i]=c[a[i]]; else c[i]=dfs(a[i],d+1); w[i]=false; comp[c[i]]++; return c[i]; } int main() { scanf("%d",&n); for (f[0]=g[0]=i=1; i<=n; i++) { scanf("%d",&a[i]); v[a[i]]++; f[i]=(((i>1)?(i-1)*f[i-2]:0LL)+f[i-1]*2)%md; g[i]=(((i>1)?(i-1)*g[i-2]:0LL)+g[i-1])%md; } for (i=1; i<=n; i++) if (v[i]==0) dfs(i,1); for (i=1; i<=n; i++) if (u[i]) { if (v[i]>2) { puts("0"); return 0; } if (!ic[i]) { if (v[i]>1) { puts("0"); return 0; } gv[a[i]]=i; } } for (i=1; i<=cc; i++) { if (comp[i]>2*cyc[i]) { puts("0"); return 0; } was=false; for (cur=0, j=fi[i]; j<la[i]; j++, cur--) { k=all[j]; int cz=0; for (int e=gv[k]; e!=0; e=gv[e]) cz++; if (cz>0) { if (was) { if (cur>0) { puts("0"); return 0; } if (cur<0) r=(r*2)%md; } else was=true; cur=cz; } } for (j=fi[i]; j<la[i]; j++, cur--) { k=all[j]; int cz=0; for (int e=gv[k]; e!=0; e=gv[e]) cz++; if (cz>0) { if (cur>0) { puts("0"); return 0; } if (cur<0) r=(r*2)%md; cur=cz; break; } } } for (i=1; i<=n; i++) if (!u[i]) { for (j=i, k=0; !u[j]; j=a[j], k++) u[j]=true; cnt[k]++; } r=(r*g[cnt[1]])%md; for (j=2; j<=n; j+=2) { for (i=1; i<=cnt[j]; i++) g[i]=(((i>1)?((i-1)*g[i-2])%md*j:0LL)+g[i-1])%md; r=(r*g[cnt[j]])%md; } for (j=3; j<=n; j+=2) { for (i=1; i<=cnt[j]; i++) f[i]=(((i>1)?((i-1)*f[i-2])%md*j:0LL)+f[i-1]*2)%md; r=(r*f[cnt[j]])%md; } printf("%d\n",r); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cmath> #include<stdio.h> #include<algorithm> #define ll long long using namespace std; const int N=1000100,P=1e9+7; inline int read(){ int x=0,f=0,c=getchar(); for(;c>'9'||c<'0';f=c=='-',c=getchar()); for(;c>='0'&&c<='9';c=getchar()) x=(x<<1)+(x<<3)+c-'0';return f?-x:x; } inline int ksm(int x,int y){ int z=1;for(;y;y>>=1,x=1ll*x*x%P) if(y&1)z=1ll*z*x%P;return z; } int q[N],r,n,a[N],du[N],f[N],ans,s[N],fac[N],inv[N],pw[N],ipw[N]; void dfs(int x){du[x]--;q[++r]=x;if(du[a[x]])dfs(a[x]);} inline int C(int n,int m){return 1ll*fac[n]*inv[m]%P*inv[n-m]%P;} int main(){ int i,l,x,la,fr,re; n=read();ans=1; for(i=1;i<=n;i++)a[i]=read(),du[a[i]]++; for(i=1;i<=n;i++){ if(!du[i])q[++r]=i; if(du[i]>2)goto fuck; } for(l=1;l<=r;l++){ x=q[l];if(f[a[x]])goto fuck; f[a[x]]=f[x]+1;if(!(--du[a[x]]))q[++r]=a[x]; } for(l=1;l<=n;l++)if(du[l]){ r=la=0;dfs(l);re=1; for(i=1;i<=r;i++) if(f[x=q[i]]){ if(!la){fr=la=i;continue;} if(i-la<f[x])goto fuck; if(i-la>f[x])re=(re+re)%P;la=i; } if(!la){s[r]++;continue;} x=q[fr];fr=fr+r; if(fr-la<f[x])goto fuck; if(fr-la>f[x])re=(re+re)%P; ans=1ll*ans*re%P; } fac[0]=inv[0]=pw[0]=ipw[0]=1; pw[1]=2;ipw[1]=(P+1)/2; for(i=1;i<=n;i++){ fac[i]=1ll*fac[i-1]*i%P; inv[i]=ksm(fac[i],P-2); pw[i]=1ll*pw[i-1]*pw[1]%P; ipw[i]=1ll*ipw[i-1]*ipw[1]%P; } for(l=1;l<=n;l++)if(s[l]){ re=0;for(i=0;i+i<=s[l];i++){ x=1ll*C(s[l],i+i)*fac[i+i]%P*inv[i]%P*ipw[i]%P*ksm(l,i)%P; if(l>1&&(l&1)){x=1ll*x*pw[s[l]-i-i]%P;}re=(re+x)%P; }ans=1ll*ans*re%P; }printf("%d\n",ans); return 0;fuck:puts("0");; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<stdio.h> #include<queue> #include<assert.h> #include<tuple> #include<string> #include<algorithm> #include<iostream> #include<map> #include<string.h> #include<vector> #include<math.h> #include<stdlib.h> #include<set> #include<ctype.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef tuple<int,int,int> t3; const int MX = 1 << 20; const int MM = 1000000007; int D[MX], vst[MX], cnt[MX], cycle[MX]; ll T[MX], U[MX], V[MX]; int N; void dfs(int x, int c = 1){ if( vst[x] ){ if( vst[x] == -1 ) cnt[x] = c - 1; else{ int t = x; do{ cycle[D[t]] = t; t = D[t]; }while(t != x); cnt[x] = vst[x] - 1; } return; } vst[x] = c; dfs(D[x], c+1); vst[x] = -1; } int indeg[MX]; int main() { scanf("%d", &N); for(int i = 1; i <= N; i++) scanf("%d", D+i), indeg[D[i]]++; for(int i = 1; i <= N; i++){ if( indeg[i] >= 3 ) return !printf("0\n"); } for(int i = 1; i <= N; i++){ if( vst[i] || indeg[i] ) continue; dfs(i); } for(int i = 1; i <= N; i++){ if(vst[i]) continue; dfs(i); } for(int i = 1; i <= N; i++) if( !cycle[i] && cnt[i] ) return !printf("0\n"); ll ans = 1; for(int i = 1; i <= N; i++){ if( !cnt[i] ) continue; int x = i; for(int j = 1; j < cnt[i]; j++){ x = cycle[x]; if( cnt[x] ) return !printf("0\n"); } if( !cnt[cycle[x]] ) ans = ans*2 % MM; } for(int i = 1; i <= N; i++) vst[i] = 0; map<int, int> L; int c1 = 0; for(int i = 1; i <= N; i++){ if( vst[i] || !cycle[i] ) continue; int tt = 0, ch = 1; int x = i; do{ x = D[x]; vst[x] = 1; if( cnt[x] ) ch = 0; tt++; }while(x != i); if( ch ) L[tt] += 1; } for(auto c : L){ T[0] = 1; T[1] = c.first >= 2 && c.first%2 == 1 ? 2 : 1; for(int i = 2; i <= c.second; i++){ T[i] = (T[i-1] * T[1] + T[i-2] * c.first % MM * (i-1)) % MM; } ans = ans * T[c.second] % MM; } printf("%lld\n", ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<iostream> #include<cstdio> #include<algorithm> #include<set> #include<map> #include<queue> #include<cassert> #define PB push_back #define MP make_pair #define sz(v) (in((v).size())) #define forn(i,n) for(in i=0;i<(n);++i) #define forv(i,v) forn(i,sz(v)) #define fors(i,s) for(auto i=(s).begin();i!=(s).end();++i) #define all(v) (v).begin(),(v).end() using namespace std; typedef long long in; typedef vector<in> VI; typedef vector<VI> VVI; const in mdl=1000000007LL; in p2(in a){ return (1LL<<a); } in pw(in a, in b, in lm=62){ a%=mdl; if(a<0) a+=mdl; in r=1; for(in i=lm;i>=0;--i){ r=r*r%mdl; if(b&p2(i)) r=r*a%mdl; } return r; } in inv(in a){ a%=mdl; if(a<0) a+=mdl; assert(a!=0); return pw(a,mdl-2,30); } VI fc,invfc; in ncr(in a, in b){ if(b==0 || b==a) return 1;//even if a<0 if(b<0 || b>a) return 0; return fc[a]*invfc[b]%mdl*invfc[a-b]%mdl; } void inifc(){ const in mxfc=1001000; fc.resize(mxfc); invfc.resize(mxfc); fc[0]=fc[1]=invfc[0]=invfc[1]=1; for(in i=2;i<mxfc;++i){ fc[i]=fc[i-1]*i%mdl; invfc[i]=invfc[mdl%i]*(mdl-mdl/i)%mdl; } for(in i=2;i<mxfc;++i){ invfc[i]*=invfc[i-1]; invfc[i]%=mdl; } } void fl(){ cout<<0<<endl; exit(0); } in tw(in cnt, in wmix, in wsing){ in sm=0; in cmix=0; in wsl=1; while(cmix<=cnt){ if(wsing) sm+=pw(wmix,cmix/2)*pw(2,cnt-cmix)%mdl*wsl%mdl*invfc[cmix/2]%mdl; else sm+=pw(wmix,cmix/2)*wsl%mdl*invfc[cmix/2]%mdl; wsl*=cnt-cmix; wsl%=mdl; ++cmix; wsl*=cnt-cmix; wsl%=mdl; ++cmix; wsl*=inv(2); wsl%=mdl; } sm%=mdl; return sm; } VI pcyc; VI a; VI dg; VI itsz; VI vis; int main(){ ios::sync_with_stdio(0); cin.tie(0); inifc(); in n; cin>>n; a.resize(n); pcyc.resize(n+1); dg.resize(n); forn(i,n){ cin>>a[i]; --a[i]; ++dg[a[i]]; if(dg[a[i]]>2) fl(); } itsz.resize(n); vis.resize(n); queue<in> q; forn(i,n){ if(dg[i]==0){ vis[i]=1; q.push(i); } } while(!q.empty()){ in u=q.front(); q.pop(); if(itsz[a[u]]) fl(); itsz[a[u]]=itsz[u]+1; if(dg[a[u]]==1){ vis[a[u]]=1; q.push(a[u]); } } VI tit; in wys=1; forn(i,n){ tit.clear(); if(vis[i]) continue; in u=i; in ft=-1; do{ vis[u]=1; tit.PB(itsz[u]); if(itsz[u]) ft=sz(tit)-1; u=a[u]; }while(u!=i); if(ft==-1){ ++pcyc[sz(tit)]; continue; } in cf=ft; do{ in cnt=0; do{ ++cnt; cf=(cf+1)%sz(tit); }while(tit[cf]==0); if(cnt<tit[cf]) fl(); if(cnt>tit[cf]) wys=wys*2%mdl; }while(cf!=ft); } forn(i,n+1){ if(pcyc[i]){ wys=wys*tw(pcyc[i],i,((i%2==1 && i!=1)?1:0))%mdl; } } cout<<wys<<endl; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define ha 1000000007 #define fuck {puts("0");return 0;} using namespace std; const int N=100010; int nxt[N],n; int pre[N][2]; int ring[N],f[N]; bool vis[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",nxt+i); if(!pre[nxt[i]][0]) pre[nxt[i]][0]=i; else if(!pre[nxt[i]][1]) pre[nxt[i]][1]=i; else fuck } int ans=1; for(int i=1;i<=n;i++) { if(vis[i]) continue; int j=i,sz=0; while(!vis[j]) vis[j]=1,j=nxt[j]; int p=j,q; do{ sz++;q=nxt[p]; if(pre[q][0]!=p) swap(pre[q][0],pre[q][1]); }while((p=q)!=j); bool flag=1;int cur=1; do{ if(!pre[p][1]) continue; int a=pre[p][0],b=pre[p][1]; flag=0;vis[b]=1; while(pre[b][0]) { if(pre[a][1]||pre[b][1]) fuck a=pre[a][0];b=pre[b][0];vis[b]=1; } if(!pre[a][1]) (cur<<=1)%=ha; }while((p=nxt[p])!=j); if(flag) ring[sz]++; else ans=1ll*ans*cur%ha; } for(int i=1;i<=n;i++) { bool flag=(i&1)&&(i!=1); f[0]=1;f[1]=flag+1; for(int j=2;j<=ring[i];j++) f[j]=(1ll*f[j-2]*(j-1)%ha*i+(f[j-1]<<flag))%ha; ans=1ll*ans*f[ring[i]]%ha; } cout<<ans<<endl; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cctype> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; typedef long long int64; inline int read(int f = 1, int x = 0, char ch = ' ') { while(!isdigit(ch = getchar())) if(ch == '-') f = -1; while(isdigit(ch)) x = x*10+ch-'0', ch = getchar(); return f*x; } const int N = 1e5+5, P = 1e9+7; int n, a[N], d[N], rec[N], cir[N], size[N], c[N]; int64 ans = 1, f[N]; int main() { n = read(); for(int i = 1; i <= n; ++i) a[i] = read(), ++d[a[i]]; for(int i = 1, p; i <= n; ++i) { if(rec[i]) continue; p = i; while(!rec[p]) rec[p] = i, p = a[p]; if(rec[p] == i) while(!cir[p]) cir[p] = 1, p = a[p]; } for(int i = 1, k, p; i <= n; ++i) { if((cir[i]&&d[i] > 2)||(!cir[i]&&d[i] > 1)) return puts("0"), 0; if(d[i]) continue; p = i, k = 0; while(!cir[p]) ++k, p = a[p]; size[p] = k; } for(int i = 1, p, fir, j, k, firs; i <= n; ++i) { if(!cir[i]) continue; firs = fir = j = k = 0, p = i; while(cir[p]) { ++k, cir[p] = 0; if(size[p]&&!fir) j = fir = k, firs = size[p]; else if(size[p]) ans = ans*((size[p] < k-j)+(size[p] <= k-j))%P, j = k; p = a[p]; } if(!fir) ++c[k]; else ans = ans*((firs < k-j+fir)+(firs <= k-j+fir))%P; } for(int i = 1; i <= n; ++i) { if(!c[i]) continue; f[0] = 1; for(int j = 1; j <= c[i]; ++j) { if(i > 1&&(i&1)) f[j] = (f[j-1]<<1)%P; else f[j] = f[j-1]; if(j > 1) f[j] = (f[j]+f[j-2]*(j-1)%P*i%P)%P; } ans = ans*f[c[i]]%P; } printf("%lld\n", ans); return 0; } /* 1 1 1 2 1 2 2 3 1 2 3 4 4 1 2 3 4 10 5 1 2 3 4 5 26 6 1 2 3 4 5 6 76 7 1 2 3 4 5 6 7 232 9 2 1 4 5 6 3 8 7 9 */
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> using namespace std; #define ll long long const int N=110000,mod=1000000007; int n,ans; int jc[N],njc[N],bir[N],nbir[N]; int a[N],fa[N],du[N],num[N],vis[N],tail[N],rem[N]; int f[N][2][2]; vector<int>vec[N],v1; int qpow(int x,int y) { int ret=1; while(y) { if(y&1)ret=(ll)ret*x%mod; x=(ll)x*x%mod;y>>=1; } return ret; } void init() { for(int i=1;i<=n;i++)fa[i]=i; jc[0]=njc[0]=bir[0]=nbir[0]=1; for(int i=1;i<=n;i++) { jc[i]=(ll)jc[i-1]*i%mod; bir[i]=bir[i-1]*2%mod; } njc[n]=qpow(jc[n],mod-2); nbir[n]=qpow(bir[n],mod-2); for(int i=n-1;i>=1;i--) { njc[i]=(ll)njc[i+1]*(i+1)%mod; nbir[i]=nbir[i+1]*2%mod; } } int find(int x){return fa[x]==x ? x : fa[x]=find(fa[x]);} void quit(){puts("0");exit(0);} void ins(int x) { if(!tail[x])return; if(rem[x]<tail[x])quit(); else if(rem[x]>tail[x])ans=ans*2%mod; } int main() { //freopen("tt.in","r",stdin); scanf("%d",&n); init(); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(find(i)!=find(a[i])) fa[find(i)]=find(a[i]); du[a[i]]++; } for(int i=1;i<=n;i++) { if(du[i]>2)quit(); vec[find(i)].push_back(i); } ans=1; for(int i=1,sz;i<=n;i++)if(sz=vec[i].size()) { int p=-1; for(int j=0;j<sz;j++) if(du[vec[i][j]]==2)p=vec[i][j]; if(p==-1)num[sz]++; else { int t=0,p1=a[p]; vis[p]=++t; while(p1!=p) { if(vis[p1])quit(); vis[p1]=++t; p1=a[p1]; } for(int j=0,cnt;j<sz;j++) if(du[p1=vec[i][j]]==0) { cnt=0; while(!vis[p1]) { cnt++; if(du[p1]==2)quit(); p1=a[p1]; } tail[p1]=cnt; } int now=0;p1=p; for(int j=1;j<=t*2;j++,p1=a[p1]) { if(tail[p1]) rem[p1]=max(rem[p1],now+1),now=0; else now++; } v1.clear(); ins(p);p1=a[p]; while(p1!=p) { ins(p1); p1=a[p1]; } } } for(int i=1;i<=n;i++)if(num[i]) { int t=0,now=1; for(int j=0;j<=num[i];j+=2) { int t1=(ll)jc[num[i]]*njc[num[i]-j]%mod*njc[j>>1]%mod*nbir[j>>1]%mod*now%mod; if((i&1)&&i!=1)t1=(ll)t1*bir[num[i]-j]%mod; t=(t+t1)%mod; now=(ll)now*i%mod; } ans=(ll)ans*t%mod; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <stdio.h> #include <algorithm> #include <queue> using namespace std; const long long mod = 1000000007; long long inv[100100]={0,1},fact[100100]={1,1},ifact[100100]={1,1}; int N,P[100100],D[100100],L[100100],G[100100],X[100100],S[200200]; queue<int> Q; int main() { scanf ("%d",&N); for (int i=1;i<=N;i++) scanf ("%d",&P[i]), D[P[i]]++; for (int i=1;i<=N;i++){ if (D[i] >= 3){ puts("0"); return 0; } if (D[i] == 0) Q.push(i); } while (!Q.empty()){ int x = Q.front(); Q.pop(); if (L[P[x]]){ puts("0"); return 0; } L[P[x]] = L[x] + 1; if (--D[P[x]] == 0) Q.push(P[x]); } long long ans = 1; for (int i=1;i<=N;i++) if (D[i] == 1){ int x = i, c = 0; while (D[x] == 1){ X[c] = x; S[c] = (L[x] > 0); c++; D[x] = 0; x = P[x]; } reverse(X,X+c); reverse(S,S+c); for (int i=0;i<c;i++) S[i+c] = S[i]; for (int i=1;i<c*2;i++) S[i] += S[i-1]; if (S[c-1] == 0) G[c]++; else{ for (int i=0;i<c;i++){ int x = X[i], v = 0; if (L[x]){ if (L[x] <= c && S[i+L[x]-1] - S[i] == 0) v++; if (L[x] <= c-1 && S[i+L[x]] - S[i] == 0) v++; ans = ans * v % mod; } } } } for (int i=2;i<=N;i++){ inv[i] = (mod - mod / i) * inv[mod % i] % mod; fact[i] = fact[i-1] * i % mod; ifact[i] = ifact[i-1] * inv[i] % mod; } for (int i=1;i<=N;i++) if (G[i]){ long long coeff = (i % 2 == 1 && i > 1 ? 2 : 1); long long sum = 0, now = 1; for (int j=0;j<G[i];j++) now = now * coeff % mod; for (int j=0;j<=G[i]/2;j++){ sum = (sum + now * fact[G[i]] % mod * ifact[j*2] % mod * ifact[G[i]-j*2]) % mod; now = now * (j * 2 + 1) % mod; now = now * i % mod; now = now * inv[coeff] % mod; now = now * inv[coeff] % mod; } ans = ans * sum % mod; } printf ("%lld\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define mp make_pair #define PI pair<int,int> #define lowbit(i) i&-i #define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++) #define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--) #define pb push_back #define fi first #define se second inline char gc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; } #define gc getchar inline ll read(){ ll x = 0; char ch = gc(); bool positive = 1; for (; !isdigit(ch); ch = gc()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = gc()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(ll a){ if(a<0){ a=-a; putchar('-'); } if(a>=10)write(a/10); putchar('0'+a%10); } inline void writeln(ll a){write(a); puts("");} inline void wri(ll a){write(a); putchar(' ');} inline ull rnd(){ ull ans=0; For(i,0,5)ans=ans<<15^rand(); return ans; } const int N=100005,mod=1000000007; int a[N],f[N],fa[N],TO[N]; vector<int> pre[N],v[N]; ll fac[N],ni[N],ans=1; bool vis[N]; inline int gf(int x){ return fa[x]==x?x:fa[x]=gf(fa[x]); } void GG(int frog){ if(!frog){ puts("0"); exit(0); } } int getpre(int x,int id){ For(i,0,1)if(vis[pre[x][i]]==id)return pre[x][i]; assert(0); } ll ksm(ll a,int b){ int ans=1; for(;b;b>>=1){ if(b&1)ans=ans*a%mod; a=a*a%mod; } return ans; } void mer(int x,int y){ fa[gf(x)]=gf(y); } int main(){ int n=read(); For(i,fac[0]=1,n)fac[i]=fac[i-1]*i%mod; ni[n]=ksm(fac[n],mod-2); Rep(i,n,1)ni[i-1]=ni[i]*i%mod; For(i,1,n)pre[a[fa[i]=i]=read()].pb(i); For(i,1,n)mer(i,a[i]); For(i,1,n)v[gf(i)].pb(i); For(i,1,n)if(fa[i]==i){ int f=1; for(auto j:v[i])if(pre[j].size()!=1)f=0; if(f)TO[v[i].size()]++; else{ for(int j=i;;j=a[j]){ if(!vis[j])vis[j]=1; else{ for(auto k:v[i])vis[k]=0; vis[j]=1; for(int k=a[j];k!=j;k=a[k])vis[k]=1; bool t=0; int sz=0; for(int k=j;t==0||k!=j;k=a[k]){ t=1; int f=0,g=0; if(pre[k].size()>=2){//cout<<pre[k].size()<<endl; GG(pre[k].size()==2); for(int l=getpre(k,1);pre[l].size()==1;l=pre[l][0])f++; for(int l=getpre(k,0);pre[l].size();l=pre[l][0]){GG(pre[l].size()==1);g++;} g++; //cout<<g<<" "<<f<<endl; GG(g<=f+1); if(g<=f)ans=ans*2%mod; //cout<<g<<" "<<f<<" "<<v[k].size()<<endl; } } break; } } } } For(i,1,n)if(TO[i]){ int d=((i&1)&&i>1)?2:1; int sum=ksm(d,TO[i]); For(j,1,TO[i]/2)sum=(sum+fac[TO[i]]*ni[j]%mod*ni[TO[i]-j-j]%mod*ksm(d,TO[i]-j-j)%mod*ksm((ll)i*(mod+1)/2%mod,j))%mod; //cout<<sum<<" "<<TO[i]<<endl; ans=ans*sum%mod; } cout<<ans<<endl; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #define ll long long #define mod 1000000007 #define N 110000 int a[N],num[N],col[N],mrk[N],cnt[N],n,lng[N]; bool cir[N]; ll dp[N]; int main() { scanf("%d", &n); ll ans=1; for (int i=1;i<=n;++i) { scanf("%d", &a[i]); ++num[a[i]]; } for (int i = 1; i <= n; i++) { if (col[i]) continue; int x = i; for (; !col[x]; x = a[x]) col[x] = i; if (col[x] != i) continue; for (;!cir[x];x=a[x]) cir[x]=1; } for (int i = 1; i <= n; i++) if ((cir[i]&&num[i]>2)||(!cir[i]&&num[i]>1)) {printf("0");return 0;} for (int i=1;i<=n;++i){ if (num[i]) continue;int x=i,tmp=0; while(!cir[x]) x=a[x],tmp++;lng[x]=tmp; } for (int i=1;i<=n;++i){ if (!cir[i]) continue;int x=i,st=0,first=0,id=0,len=0; while (cir[x]){ ++id;cir[x]=0; if (lng[x]){ if (!first){ st=first=id;len=lng[x];x=a[x];continue; }else{ int numm=(lng[x]<(id-st))+(lng[x]<=(id-st)); (ans*=numm)%=mod;if (!ans) {printf("0");return 0;}st=id;x=a[x];continue; } }x=a[x]; }if (first){ int numm=(len<(id+first-st))+(len<=(id+first-st)); (ans*=numm)%=mod;if (!ans) {printf("0");return 0;} }else cnt[id]++; } for (int i=1;i<=n;++i){ dp[0]=1;if (!cnt[i]) continue; if (i>1&&(i%2)){ for (int j=1;j<=cnt[i];++j) { dp[j]=dp[j-1]*2%mod;if (j>1) (dp[j]+=dp[j-2]*(j-1)*i%mod)%=mod; } }else{ for (int j=1;j<=cnt[i];++j){ dp[j]=dp[j-1];if (j>1) (dp[j]+=dp[j-2]*(j-1)*i%mod)%=mod; } } (ans*=dp[cnt[i]])%=mod;if (!ans){printf("0");return 0;} }printf("%lld",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #define maxn 100010 #define mod 1000000007 using namespace std; int read() { int x=0,f=1; char ch=getchar(); while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();} while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();} return x*f; } int n; int a[maxn],deg[maxn],vis[maxn],cir[maxn],len[maxn],sum[maxn]; int ans=1; void work(int x) { int now=0,fr=0,ed=0,l=0; while(cir[x]) { ++now;cir[x]=0; if(len[x]) { if(!fr) fr=ed=now,l=len[x]; else{ ans=1ll*ans*((len[x]<now-ed)+(len[x]<=now-ed))%mod; ed=now; } } x=a[x]; } if(!fr) sum[now]++; else{ ans=1ll*ans*((l<now-ed+fr)+(l<=now-ed+fr))%mod; } } int f[maxn]; void solve() { for(int i=1;i<=n;i++) { if(deg[i]) continue; int x=i,l=0; while(!cir[x]) { x=a[x]; l++; } len[x]=l; } for(int i=1;i<=n;i++) if(cir[i]) work(i); for(int i=1;i<=n;i++) { if(!sum[i]) continue; f[0]=1; for(int j=1;j<=sum[i];j++) { if(i>1&&(i&1)) f[j]=(f[j-1]+f[j-1])%mod; else f[j]=f[j-1]; if(j>1) f[j]=(f[j]+1ll*f[j-2]*(j-1)%mod*i%mod)%mod; } ans=1ll*ans*f[sum[i]]%mod; } } int main() { n=read(); for(int i=1;i<=n;i++) a[i]=read(),deg[a[i]]++; for(int i=1;i<=n;i++) { if(vis[i]) continue; int x=i; while(!vis[x]) vis[x]=i,x=a[x]; if(vis[x]!=i) continue; while(!cir[x]) cir[x]=1,x=a[x]; } for(int i=1;i<=n;i++) { if((cir[i]&&deg[i]>2)||(!cir[i]&&deg[i]>1)) { puts("0"); return 0; } } solve(); printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int MX=100105,md=1000000007; int i,j,k; int f[MX],fac[MX],facinv[MX],M[MX]; int pw(int x,int y){ int z=1; for(;y;y>>=1,x=(LL)x*x%md)if(y&1)z=(LL)z*x%md; return z; } int C(int n,int m){return (LL)fac[n]*facinv[m]%md*facinv[n-m]%md;} void ini(int n){ int i; fac[0]=facinv[0]=f[0]=M[0]=1; for(i=1;i<=n;i++)M[i]=(M[i-1]+M[i-1])%md; for(i=1;i<=n;i++)f[i]=(LL)f[i-1]*(2*i-1)%md; for(i=1;i<=n;i++)fac[i]=(LL)fac[i-1]*i%md; facinv[n]=pw(fac[n],md-2); for(i=n-1;i;i--)facinv[i]=(LL)facinv[i+1]*(i+1)%md; } int n,m,ch,ff,ans; int a[MX],b[MX],z[MX],fg[MX],num[MX],d[MX],dp[MX]; int cal(int n,int m){ int i,k,ans=0; if((m&1)&& m>1){ for(i=0,k=1;i+i<=n;i++,k=(LL)k*m%md)ans=((LL)f[i]*k%md*C(n,i+i)%md*M[n-i-i]%md+ans)%md; }else{ for(i=0,k=1;i+i<=n;i++,k=(LL)k*m%md)ans=((LL)f[i]*k%md*C(n,i+i)%md+ans)%md; } return ans; } int main(){ cin>>n; ini(n); for(i=1;i<=n;i++)cin>>a[i]; for(i=1;i<=n;i++)d[a[i]]++; for(i=1;i<=n;i++)if(!z[i]){ for(j=i;z[j]!=i && !z[j];j=a[j])z[j]=i; if(z[j]==i){ fg[j]=1; for(k=a[j];k!=j;k=a[k])fg[k]=1; } } for(i=1;i<=n;i++)if(d[i]>fg[i]+1)return cout<<0<<endl,0; for(i=1;i<=n;i++)if(!fg[i])b[a[i]]=i; for(i=1;i<=n;i++)if(fg[i]){ for(j=b[i];j;j=b[j])dp[i]++; } memset(z,0,sizeof z); ans=1; for(i=1;i<=n;i++)if(!z[i] && fg[i]){ int nm=0,Fg=0; for(j=i;!z[j];j=a[j]){ z[j]=1; nm++; if(dp[j])Fg=j; } if(!Fg)num[nm]++; else { k=0; for(j=a[Fg];j!=Fg;j=a[j]){ k++; if(dp[j]){ if(dp[j]>k)ans=0; if(dp[j]<k)ans=(ans+ans)%md; k=0; } } k++; if(dp[Fg]>k)ans=0; if(dp[Fg]<k)ans=(ans+ans)%md; } } for(i=1;i<=n;i++)if(num[i])ans=(LL)ans*cal(num[i],i)%md; cout<<ans<<endl; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int N=200005,md=1000000007; vector<int> vct[N]; int n,i,j,k,a[N],p[N],d[N],fa[N],ans=1,cnt,size[N],huan[N],f[N],c[N],num[N],head[N],adj[N],nxt[N],pre[N],u[N]; bool oh[N],v[N]; int find(int x) { return !fa[x]?x:fa[x]=find(fa[x]); } void dfs(int x) { v[x]=true; p[++cnt]=x; if(!v[a[x]]) dfs(a[x]); else { int i; for(i=cnt;i>=1;--i) if(p[i]==a[x]) break; if(i>=1) { int fst=0,lst=0,st=i; while(i<=cnt) { if(d[p[i]]>1) { if(!fst) fst=i; pre[p[i]]=i-lst; lst=i; } oh[p[i]]=true; huan[find(x)]++; ++i; } pre[p[fst]]=fst-st+1+cnt-lst; } } } int work(int x) { int rtn=1; for(int y=head[x];y;y=nxt[y]) if(!oh[adj[y]]) rtn+=work(adj[y]); return rtn; } int main() { scanf("%d",&n); for(i=1;i<=n;++i) { scanf("%d",a+i); adj[i]=i; nxt[i]=head[a[i]]; head[a[i]]=i; ++d[a[i]]; if(find(i)!=find(a[i])) fa[find(i)]=find(a[i]); } for(i=1;i<=n;++i) vct[find(i)].push_back(i); for(i=1;i<=n;++i) if(d[i]>2) { printf("0"); return 0; } for(i=1;i<=n;++i) { size[find(i)]++; if(!v[i]) { cnt=0; dfs(i); } } for(i=1;i<=n;++i) if(!oh[i]&&d[i]>1) { printf("0"); return 0; } for(i=1;i<=n;++i) if(d[i]>1) c[find(i)]++; for(i=1;i<=n;++i) if(d[i]>1) u[i]=work(i)-1; for(i=1;i<=n;++i) if(d[i]>1) { if(u[i]<pre[i]) ans=ans*2%md; else if(u[i]>pre[i]) { printf("0"); return 0; } } for(i=1;i<=n;++i) if(find(i)==i) { if(c[i]==0) ++num[size[i]]; } for(i=1;i<=n;++i) if(num[i]) { f[0]=1,f[1]=(i==1?1:i&1?2:1); for(j=2;j<=num[i];++j) f[j]=(1ll*f[j-1]*(i==1?1:i&1?2:1)%md+1ll*(j-1)*f[j-2]%md*i%md)%md; ans=1ll*ans*f[num[i]]%md; } printf("%d",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> #define N 100005 #define ll long long #define For(i,x,y) for(int i=(x);i<=(y);++i) #define Rof(i,x,y) for(int i=(x);i>=(y);--i) #define p_b push_back #define pii pair<int,int> #define mp make_pair #define fr first #define sd second #define mod 1000000007 using namespace std; ll _2[N],_2_[N],fac[N],invf[N]; int nxt[N],id[N],len[N],deg[N],a[N],tot[N],cir[N],d[N],pos; bool vis[N],inq[N]; vector<pii > v[N]; void dfs(int x,int dep){ if(inq[x] || cir[x]){ if(!cir[x]){ len[x]=dep-d[x],pos=x,cir[x]=x,id[x]=1; if(d[x]-1) v[x].push_back(mp(1,d[x]-1)); } else if(dep-1) v[cir[x]].push_back(mp(id[x],dep-1)); return; } d[x]=dep,inq[x]=1,vis[x]=1; dfs(nxt[x],dep+1),inq[x]=0; if(pos && dep>d[pos]) cir[x]=pos,id[x]=id[nxt[x]]+1; } int ___(int x,int y){ if(x>y) return 2; if(x<y) return 0;return 1; } ll qpow(ll x,int y){ ll tmp=1; for(;y;y>>=1,(x*=x)%=mod) if(y&1) (tmp*=x)%=mod; return tmp; } int main(){ int n;ll ans=1,ans2=1; scanf("%d",&n); _2[0]=_2_[0]=1;ll fuck=qpow(2ll,mod-2); fac[0]=fac[1]=invf[0]=invf[1]=1,_2[1]=2,_2_[1]=fuck; For(i,1,n) scanf("%d",&a[i]),nxt[i]=a[i],deg[nxt[i]]++; For(i,2,n){ _2[i]=(_2[i-1]+_2[i-1])%mod; _2_[i]=_2_[i-1]*fuck%mod; fac[i]=1ll*fac[i-1]*i%mod; invf[i]=1ll*(mod-mod/i)*invf[mod%i]%mod; } For(i,1,n) (invf[i]*=invf[i-1])%=mod; For(i,1,n) if(!deg[i]) pos=0,dfs(i,1); For(i,1,n) if(!vis[i]) pos=0,dfs(i,1); For(i,1,n){ if(deg[i]>2) return puts("0"),0; else if(!cir[i] && deg[i]>=2) return puts("0"),0; } For(i,1,n) sort(v[i].begin(),v[i].end()); For(i,1,n){ if(!v[i].size()){ if(len[i]) tot[len[i]]++;continue; } if(v[i].size()==1){ if(len[i]<v[i][0].sd) return puts("0"),0; else if(len[i]>v[i][0].sd)(ans*=2ll)%=mod; } else{ For(j,0,(int)v[i].size()-2){ ll p=1ll*___(v[i][j+1].fr-v[i][j].fr,v[i][j].sd); if(!p) return puts("0"),0; else (ans*=p)%=mod; } int p=v[i].size()-1; if(len[i]-v[i][p].fr+v[i][0].fr<v[i][p].sd) return puts("0"),0; else if(len[i]-v[i][p].fr+v[i][0].fr>v[i][p].sd) (ans*=2ll)%=mod; } } For(i,1,n){ if(!tot[i]) continue; ll qwq=0; For(j,0,tot[i]/2){ int x=j*2; ll tmp=qpow(1ll*i,j)*fac[tot[i]]%mod*invf[tot[i]-x]%mod*_2_[j]%mod*invf[j]%mod; (tmp*=_2[((i%2) && i!=1)*(tot[i]-x)])%=mod; (qwq+=tmp)%=mod; } (ans2*=qwq)%=mod; } printf("%lld\n",ans*ans2%mod); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define maxn 200005 #define mod 1000000007 using namespace std; int deg[maxn],a[maxn],incy[maxn],vis[maxn],svis[maxn],n; int chlen[maxn],cnt[maxn]; int ans=1; int add(int a,int b){a+=b; return a>=mod?a-mod:a;} void SolveCyc(int x) { int t=x; x=a[x],svis[t]=svis[x]=1; int fi=0,fpos=0,la=0,len=1; while(1) { while(!chlen[x]&&x!=t) x=a[x],svis[x]=1,len++; if(chlen[x]) { if(!fi) fi=x,la=len,fpos=len; else { int coef=(len-la>=chlen[x])+(len-la>chlen[x]); ans=1ll*ans*coef%mod,la=len; } } if(x==t) break; x=a[x],svis[x]=1,len++; } if(chlen[t]) { if(!fi) fi=t; else la=len; } if(!fi) cnt[len]++; else { int coef=(len-la+fpos>=chlen[fi])+(len-la+fpos>chlen[fi]); ans=1ll*ans*coef%mod; } } int dp[maxn]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),deg[a[i]]++; for(int i=1;i<=n;i++) { if(vis[i]) continue; int nw=i; while(!vis[nw]) vis[nw]=i,nw=a[nw]; if(vis[nw]!=i) continue; int t=nw; incy[nw]=1,nw=a[nw]; while(nw!=t) incy[nw]=1,nw=a[nw]; } for(int i=1;i<=n;i++) if(((!incy[i])&&deg[i]>=2)||deg[i]>=3) return puts("0"),0; for(int i=1;i<=n;i++) { if(deg[i]) continue; int nw=i; while(!incy[nw]) chlen[a[nw]]=chlen[nw]+1,nw=a[nw]; } for(int i=1;i<=n;i++) if(incy[i]&&!svis[i]) SolveCyc(i); for(int i=1;i<=n;i++) { if(!cnt[i]) continue; dp[0]=1; for(int j=1;j<=cnt[i];j++) { if(i>1&&(i&1)) dp[j]=add(dp[j-1],dp[j-1]); else dp[j]=dp[j-1]; if(j>1) dp[j]=add(dp[j],1ll*dp[j-2]*(j-1)%mod*i%mod); } ans=1ll*ans*dp[cnt[i]]%mod; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <iostream> #include <cstdio> #include <cstring> #include <cassert> #include <cctype> using namespace std; typedef long long lint; #define cout cerr #define ni (next_num<int>()) template<class T>inline T next_num(){ T i=0;char c; while(!isdigit(c=getchar())&&c!='-'); bool flag=c=='-'; flag?c=getchar():0; while(i=i*10-'0'+c,isdigit(c=getchar())); return flag?-i:i; } template<class T1,class T2>inline void apmax(T1 &a,const T2 &b){if(a<b)a=b;} template<class T1,class T2>inline void apmin(T1 &a,const T2 &b){if(b<a)a=b;} const int N=100010,O=1000000007; int pre[N][2],nxt[N]; bool vis[N]; int ring[N]; int f[N]; inline int Main(){ int n=ni; memset(pre,0,sizeof(pre)); for(int i=1;i<=n;i++){ nxt[i]=ni; if(pre[nxt[i]][0]==0){ pre[nxt[i]][0]=i; }else if(pre[nxt[i]][1]==0){ pre[nxt[i]][1]=i; }else return 0; } memset(ring,0,sizeof(ring)); lint ans=1; for(int i=1;i<=n;i++){ if(!vis[i]){ int j=i,sz=0; for(;!vis[j];vis[j]=true,j=nxt[j]); for(int p=j,q;sz++,q=nxt[p],pre[q][0]!=p?swap(pre[q][0],pre[q][1]):void(),p=q,p!=j;); int p=j; bool flag=true; int cur=1; do if(pre[p][1]){ flag=false; int a=pre[p][0],b=pre[p][1]; for(;vis[b]=true,pre[b][0];a=pre[a][0],b=pre[b][0]){ if(pre[a][1]||pre[b][1])return 0; } if(pre[a][1]==0){ cur=(cur<<1)%O; } }while(p=nxt[p],p!=j); if(flag){ assert(cur==1); ring[sz]++; }else{ (ans*=cur)%=O; } } } for(int i=1;i<=n;i++){ bool flag=(i&1)&&i!=1; f[0]=1,f[1]=flag+1; for(int j=2;j<=ring[i];j++){ f[j]=((lint)f[j-2]*(j-1)%O*i%O+(f[j-1]<<flag))%O; } (ans*=f[ring[i]])%=O; } return ans; } int main(){ printf("%d\n",Main()); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define LL long long #define db double using namespace std; const int N=2e5+10,mod=1e9+7,inf=1<<30; int rd() { int x=0,w=1;char ch=0; while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();} return x*w; } void ad(int &x,int y){x+=y,x-=x>=mod?mod:0;} int fpow(int a,int b){int an=1;while(b){if(b&1) an=1ll*an*a%mod;a=1ll*a*a%mod,b>>=1;}return an;} int ginv(int a){return fpow(a,mod-2);} vector<int> e[N]; int n,a[N],v1[N],ti,cr[N],st[N],tp,f[N],g[N],b[N],h[N],fac[N],iac[N]; int C(int a,int b){return b<0||a<b?0:1ll*fac[a]*iac[b]%mod*iac[a-b]%mod;} bool mk[N]; void dfs(int x) { int nn=e[x].size(); for(int i=0;i<nn;++i) { int y=e[x][i]; dfs(y),f[x]=f[x]?-inf:f[y]+1; } } int main() { //iee fac[0]=1; for(int i=1;i<=N-5;++i) fac[i]=1ll*fac[i-1]*i%mod; iac[N-5]=ginv(fac[N-5]); for(int i=N-5;i;--i) iac[i-1]=1ll*iac[i]*i%mod; n=rd(); for(int i=1;i<=n;++i) a[i]=rd(); for(int i=1;i<=n;++i) if(!v1[i]) { ++ti; int x=i; tp=0; while(!v1[x]) v1[x]=ti,st[++tp]=x,x=a[x]; if(cr[x]) {mk[x]=1;continue;} if(v1[x]<ti) continue; int cn=0; for(int j=tp;;--j) { ++cn; if(st[j]==x) break; } for(int j=tp;;--j) { cr[st[j]]=cn; if(st[j]==x) break; } mk[x]=tp>cn; } ++ti; for(int i=1;i<=n;++i) if(cr[i]&&v1[i]<ti) { int x=i; for(int j=1;j<=cr[i]*2;++j) v1[x]=ti,mk[a[x]]|=mk[x],x=a[x]; } for(int i=1;i<=n;++i) if(!cr[i]) e[a[i]].push_back(i); int ans=1; for(int i=1;i<=n;++i) if(cr[i]) { if(mk[i]) { int x=i; tp=0; for(int j=1;j<=cr[i];++j) st[++tp]=x,x=a[x]; for(int j=1;j<=cr[i];++j) dfs(st[j]); for(int j=1;j<=cr[i];++j) if(f[st[j]]<0||f[st[j]]>cr[i]){puts("0");return 0;} for(int j=1;j<=cr[i];++j) g[j]=g[j-1]+(bool)f[st[j]]; for(int j=1;j<=cr[i];++j) g[j+cr[i]]=g[j-1+cr[i]]+(bool)f[st[j]]; for(int j=cr[i]+1;j<=cr[i]+cr[i];++j) { int y=st[j-cr[i]]; if(!f[y]) continue; if(cr[i]>1&&g[j-1]-g[j-f[y]]){puts("0");return 0;} ans=1ll*ans*(2-(bool)(g[j-1]-g[j-f[y]-1]))%mod; } } else ++b[cr[i]]; int x=i,lm=cr[i]; for(int j=1;j<=lm;++j) cr[x]=0,x=a[x]; } h[0]=h[1]=1; for(int i=2;i<=n;++i) h[i]=1ll*h[i-1]*(2*i-1)%mod; for(int i=1;i<=n;++i) { if(!b[i]) continue; int sm=0,dx=1+(i>1&&(i&1)); for(int j=0;j+j<=b[i];++j) ad(sm,1ll*C(b[i],j+j)*h[j]%mod*fpow(i,j)%mod*fpow(dx,b[i]-j-j)%mod); ans=1ll*ans*sm%mod; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define pb push_back #define mp make_pair #define FL "a" using namespace std; typedef vector<int> VI; typedef long long ll; typedef double dd; const int N=1e5+10; const int mod=1e9+7; inline ll read(){ ll data=0,w=1;char ch=getchar(); while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar(); if(ch=='-')w=-1,ch=getchar(); while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar(); return data*w; } inline void file(){ freopen(FL".in","r",stdin); freopen(FL".out","w",stdout); } inline void upd(int &a,int b){a+=b;if(a>=mod)a-=mod;} inline void dec(int &a,int b){a-=b;if(a<0)a+=mod;} inline int poww(int a,int b){ int res=1; for(;b;b>>=1,a=1ll*a*a%mod) if(b&1)res=1ll*res*a%mod; return res; } inline void er(){puts("0");exit(0);} int n,a[N],ans; int d[N],head[N],nxt[N<<1],to[N<<1],cnt; inline void add(int u,int v){to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;} int t[N],f[N]; int vis[N],line[N],dis[N],cal[N],top,cir[N],len; void dfs(int u){ vis[u]=1;cal[++top]=u; for(int i=head[u],v;i;i=nxt[i]) if(~i&1){ if(!vis[v=to[i]])dfs(v); else for(int j=top;cal[j]!=v;j--) cir[++len]=cal[j]; } top--; } inline void solve(int x){ top=len=0; for(int p=0;!vis[x];vis[x]=1,cal[++top]=x,p=x,x=a[x]); for(int i=top,k=1;i;i--){ k?cir[++len]=cal[i],dis[cal[i]]=len:vis[cal[i]]=0; if(cal[i]==x)k=0; } reverse(cir+1,cir+len+1); for(int i=1;i<=len;i++)dis[cir[i]]=len-dis[cir[i]]+1; //for(int i=1;i<=len;i++)printf("%d ",cir[i]);puts(""); for(int k=1,u;k<=len;k++){ u=cir[k];if(d[u]>2)er();line[u]=0; for(int i=head[u],v;i;i=nxt[i]) if(!vis[v=to[i]]){ line[u]++;vis[v]=1; for(v;d[v];v=to[head[v]],vis[v]=1,line[u]++) if(d[v]>1)er(); } } for(int k=1;!line[cir[k]];k++) if(k==len){t[len]++;return;} if(len==1&&line[cir[1]]<=1)return; top=0; for(int k=1;k<=len;k++) if(line[cir[k]])cal[++top]=cir[k]; /*for(int i=1;i<=top;i++) printf("(%d,%d,%d)",cal[i],dis[cal[i]],line[cal[i]]);puts("");*/ if(line[cal[1]]>dis[cal[1]]+(len-dis[cal[top]]))er(); else ans=1ll*min(2,dis[cal[1]]+(len-dis[cal[top]])-line[cal[1]]+1)*ans%mod; for(int i=2;i<=top;i++) if(line[cal[i]]>dis[cal[i]]-dis[cal[i-1]])er(); else ans=1ll*min(2,dis[cal[i]]-dis[cal[i-1]]-line[cal[i]]+1)*ans%mod; } int main() { n=read();cnt=1;ans=1; for(int i=1;i<=n;i++){ a[i]=read();d[a[i]]++;add(a[i],i); //printf("%d %d\n",i,a[i]); if(a[i]>n)er(); } for(int i=1;i<=n;i++) if(!vis[i])solve(i); for(int i=1;i<=n;i++) if(t[i]){ f[0]=1; for(int j=1;j<=t[i];j++){ f[j]=1ll*((i&1)&&i!=1?2:1)*f[j-1]%mod; upd(f[j],1ll*(j-1)*i%mod*f[j-2]%mod); } ans=1ll*ans*f[t[i]]%mod; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pii; #define pb push_back #define mp make_pair #define fi first #define se second #define sz(a) int(a.size()) const int N=1e5+10; const int mod=1e9+7; int gi() { int x=0,o=1;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') ch=getchar(); if(ch=='-') o=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*o; } int n,a[N],in[N],ans=1,cnt[N],fa[N],f[N],pre[N]; bool vis[N]; vi vec[N]; int getf(int x) { return x==fa[x]?x:fa[x]=getf(fa[x]); } void No() { cout<<0;exit(0); } int main() { cin>>n; for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=n;i++) a[i]=gi(),++in[a[i]],fa[getf(i)]=getf(a[i]); for(int i=1;i<=n;i++) if(in[i]>2) No(); for(int i=1;i<=n;i++) vec[getf(i)].pb(i); for(int u=1;u<=n;u++) if(getf(u)==u) { bool fl=1;int sz=0; for(auto x:vec[u]) { ++sz; if(in[x]!=1) fl=0; } //cerr<<u<<' '<<fl<<' '<<sz<<'\n'; if(fl) ++cnt[sz]; else { int now=u; while(!vis[now]) vis[now]=1,now=a[now]; for(auto x:vec[u]) vis[x]=0; while(!vis[now]) vis[now]=1,pre[a[now]]=now,now=a[now]; for(auto x:vec[u]) if(in[x]>1&&!vis[x]) No(); for(auto x:vec[u]) if(!in[x]) { int now=x,l1=0,l2=0; while(!vis[now]) ++l1,now=a[now]; ++l2;now=pre[now]; while(in[now]==1) ++l2,now=pre[now]; //cerr<<l1<<' '<<l2<<'\n'; if(l1>l2) No(); if(l1<l2) ans=2ll*ans%mod; } } } for(int k=1;k<=n;k++) { for(int i=0;i<=cnt[k];i++) f[i]=0; f[0]=1; for(int i=1;i<=cnt[k];i++) { f[i]=f[i-1]*((k&1)&&k>1?2:1)%mod; if(i>1) f[i]=(f[i]+1ll*f[i-2]*(i-1)%mod*k)%mod; } ans=1ll*ans*f[cnt[k]]%mod; //cerr<<"k,cnt[k]="<<k<<' '<<cnt[k]<<'\n'; } cout<<ans; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; typedef long long lint; typedef long double louble; template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;} template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;} const char lf = '\n'; namespace ae86 { const int bufl = 1 << 15; char buf[bufl],*s=buf,*t=buf; inline int fetch() { if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;} return *s++; } inline int ty() { int a=0;int b=1,c=fetch(); while(!isdigit(c))b^=c=='-',c=fetch(); while(isdigit(c))a=a*10+c-48,c=fetch(); return b?a:-a; } } using ae86::ty; const int _ = 100007 , mo = 1000000007; template<typename T1,typename T2> inline T1 ad(T1 &a,T2 b){return a=a+b>=mo?a+b-mo:a+b;} template<typename T1,typename T2> inline T1 dl(T1 &a,T2 b){return a=a>=b?a-b:a-b+mo;} template<typename T1,typename T2> inline T1 add(T1 a,T2 b){return a+b>=mo?a+b-mo:a+b;} template<typename T1,typename T2> inline T1 del(T1 a,T2 b){return a>=b?a-b:a-b+mo;} lint powa(lint a,lint t) { lint b=1;a=(a+mo)%mo; while(t){if(t&1)b=b*a%mo;a=a*a%mo,t>>=1;} return b; } inline lint inva(lint a) { return powa(a,mo-2); } int n,nex[_]={0},din[_]={0},ed[_]={0}; int isc[_]={0},dlen[_]={0},got[_]={0}; int ccnt[_]={0}; lint ans=1; void dfs(int x) { int len=0,lasloc=0,firloc=0,firlen=0; while(!got[x]) { len++,got[x]=1; if(dlen[x]) { if(!firloc)firloc=len,firlen=dlen[x]; else ans=ans*add<int,int>(dlen[x]<len-lasloc,dlen[x]<=len-lasloc)%mo; lasloc=len; } x=nex[x]; } if(!firloc)ccnt[len]++; else ans=ans*add<int,int>(firlen<len-lasloc+firloc,firlen<=len-lasloc+firloc)%mo; } lint f[_]={0}; int main() { ios::sync_with_stdio(0),cout.tie(nullptr); n=ty(); for(int i=1;i<=n;i++)nex[i]=ty(),din[nex[i]]++; for(int i=1;i<=n;i++) { if(ed[i])continue; int now=i; while(!ed[now])ed[now]=i,now=nex[now]; if(ed[now]!=i)continue; while(!isc[now])isc[now]=1,now=nex[now]; } for(int i=1;i<=n;i++)if(din[i]>1+isc[i]){cout<<0<<lf;return 0;} for(int i=1;i<=n;i++) { if(din[i])continue; int x=i,len=0; while(!isc[x])len++,x=nex[x]; dlen[x]=len; } for(int i=1;i<=n;i++)if(isc[i] && !got[i])dfs(i); for(int i=1;i<=n;i++) { if(!ccnt[i])continue; f[0]=1; for(int j=1;j<=ccnt[i];j++) { if(i>1 && (i&1))f[j]=add(f[j-1],f[j-1]); else f[j]=f[j-1]; if(j>1)ad(f[j],f[j-2]*(j-1)%mo*i%mo); } ans=ans*f[ccnt[i]]%mo; } cout<<ans<<lf; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
# include <bits/stdc++.h> using namespace std; const int mod = 1e9+7; int n; const int maxn = 100010; int p[maxn]; int ind[maxn]; bool vis[maxn]; int pos[maxn]; int feet[maxn]; map<int, int> cir; int dp[maxn]; int DP(int m,int n) { long long s = 1 + ((m > 1) & (m & 1)); long long p = m; dp[0] = 1; dp[1] = s; for(int i = 2; i <= n; ++i) { dp[i] = (s * dp[i-1] + p * (i-1) % mod * dp[i-2]) % mod; } return dp[n]; } int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d", &p[i]); ind[p[i]] += 1; } for(int i = 1; i <= n; ++i) if(ind[i] > 2) { puts("0"); return 0; } for(int i = 1; i <= n; ++i) if(ind[i] == 0) { int j, l = 0; for(j = i; ind[j] < 2; j = p[j]) vis[j] = true, l += 1; feet[j] = l; } int ans = 1; for(int i = 1; i <= n; ++i) if(!vis[i]) { int l = 0, j; for(j = i; !vis[j]; j = p[j]) vis[j] = true, pos[l++] = j; if(i!=j) ans = 0; int last = 0; for(int j = 0; j < l; ++j) if(feet[pos[j]] > 0) last = j - l; if(last == 0) { cir[l]++; continue; } for(int j = 0; j < l; ++j) if(feet[pos[j]] > 0) { if(j - last > feet[pos[j]]) ans = (ans << 1) % mod; else if(j - last < feet[pos[j]]) ans = 0; last = j; } } for(auto e: cir) ans = 1ll * ans * DP(e.first, e.second) % mod; cout << ans << endl; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; const int mod=1e9+7; int gi() { int x=0,o=1;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') ch=getchar(); if(ch=='-') o=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*o; } int n,ans=1,a[N],du[N],pre[N],cnt[N],f[N]; bool vis[N]; int main() { cin>>n; for(int i=1;i<=n;i++) a[i]=gi(),du[a[i]]++; for(int i=1;i<=n;i++) { if(du[i]>2) return puts("0"),0; if(du[i]<2||vis[i]) continue; int p=i; do { if(vis[p]) return puts("0"),0; vis[p]=1;pre[a[p]]=p;p=a[p]; } while(p!=i); } for(int i=1;i<=n;i++) if(!du[i]) { int p=i,l1=0,l2=0; while(!vis[p]) vis[p]=1,p=a[p],l1++; do l2++,p=pre[p]; while(du[p]!=2); if(l1<l2) ans=2ll*ans%mod; else if(l1>l2) return puts("0"),0; } for(int i=1;i<=n;i++) if(!vis[i]) { int p=i,l=0; do p=a[p],l++,vis[p]=1; while(p!=i); cnt[l]++; } for(int i=1;i<=n;i++) { int mul=1;if(i!=1&&(i&1)) mul++; f[0]=1;f[1]=mul; for(int j=2;j<=cnt[i];j++) f[j]=(1ll*f[j-2]*(j-1)%mod*i%mod+1ll*f[j-1]*mul%mod)%mod; ans=1ll*ans*f[cnt[i]]%mod; } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define L long long using namespace std; const int q=1000000007; int n,a[100010],b[100010],x[100010],f[100010],p=1,w[100010],u[100010]; vector<int> y[100010]; inline int C(int n,int m) { return (L)a[n]*b[m/2]%q*b[n-m]%q; } inline void calc(int i) { int j,k=0,l,v,e; for(j=i;w[j]<2;j=x[j]) { w[j]=2; u[++k]=j; } if(j!=i || k==0) return; for(i=2;i<k+2-i;i++) swap(u[i],u[k+2-i]); u[k+1]=u[1]; for(j=1,l=0,v=1;j<=k;j++,l--) { if(y[u[j]].size()>2) v=0; if(y[u[j]].size()==2) { if(l>0) v=0; if(l<0) v=(v<<1)%q; for(l=1,e=y[u[j]][0]^y[u[j]][1]^u[j+1];y[e].size()==1;e=y[e][0],l++); if(y[e].size()>1) v=0; } } if(l>0) v=0; if(l<0) v=(v<<1)%q; p=(L)p*v%q; } int main() { //freopen(".in","r",stdin); //freopen(".out","w",stdout); int i,j,k,l; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&x[i]); y[x[i]].push_back(i); } b[1]=1; for(i=2;i<=n;i++) b[i]=q-(L)q/i*b[q%i]%q; a[0]=b[0]=1; for(i=1;i<=n;i++) { a[i]=(L)a[i-1]*i%q; b[i]=(L)b[i-1]*b[i]%q; } for(i=1;i<=n;i++) if(!w[i]) { for(j=i,k=0,l=0;!w[j];j=x[j],k++) { w[j]=1; if(y[j].size()>1) l=1; } if(j==i) { if(!l) f[k]++; } else calc(j); } for(i=1;i<=n;i++) { if(i&1 && i>1) for(j=1,l=1;j<=f[i];j++) l=(l<<1)%q; else l=1; for(j=0,k=0;j*2<=f[i];j++,l=(L)l*i%q) { k=(k+(L)C(f[i],2*j)*l)%q; l=(L)l*(q+1>>1)%q; if(i&1 && i>1) l=(L)l*(q+1>>1)%q*(q+1>>1)%q; } p=(L)p*k%q; } printf("%d\n",p); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; const int maxn=100010,mod=1000000007,inv2=500000004; #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define ROF(i,a,b) for(int i=(a);i>=(b);i--) #define MEM(x,v) memset(x,v,sizeof(x)) inline int read(){ int x=0,f=0;char ch=getchar(); while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar(); while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar(); return f?-x:x; } int n,a[maxn],deg[maxn],ans=1,stk[maxn],tp,q[maxn],h,r,len[maxn],seq[maxn],tot,cnt[maxn]; int fac[maxn],inv[maxn],invfac[maxn]; bool vis[maxn],ins[maxn],cyc[maxn]; void dfs(int u){ if(vis[u]){ if(ins[u]){ ROF(i,tp,1){ cyc[stk[i]]=true; if(stk[i]==u) break; } } return; } vis[u]=ins[u]=true; stk[++tp]=u; dfs(a[u]); ins[u]=false; } bool dfs2(int u){ if(vis[u]) return true; vis[u]=true; seq[++tot]=len[u]; return dfs2(a[u]) && !len[u]; } inline int C(int n,int m){ return 1ll*fac[n]*invfac[m]%mod*invfac[n-m]%mod; } inline int qpow(int a,int b){ int ans=1; for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod; return ans; } int main(){ n=read(); FOR(i,1,n) a[i]=read(),deg[a[i]]++; FOR(i,1,n) dfs(i); FOR(i,1,n) if(cyc[i] && deg[i]>=3|| !cyc[i] && deg[i]>=2) return puts("0"),0; h=1;r=0; FOR(i,1,n) if(!deg[i]) q[++r]=i; while(h<=r){ int u=q[h++]; len[a[u]]=len[u]+1; if(!cyc[a[u]]) q[++r]=a[u]; } MEM(vis,0); FOR(i,1,n) if(cyc[i] && !vis[i]){ tot=0; if(dfs2(i)) cnt[tot]++; else{ int pre=0; FOR(j,1,tot) if(seq[j]){ if(pre){ int at=j-seq[j]; if(at<pre) return puts("0"),0; if(at>pre && tot>=2) ans=2*ans%mod; } pre=j; } FOR(j,1,tot) if(seq[j]){ int at=j-seq[j]+tot; if(at<pre) return puts("0"),0; if(at>pre && tot>=2) ans=2*ans%mod; break; } } } fac[0]=fac[1]=inv[1]=invfac[0]=invfac[1]=1; FOR(i,2,n){ fac[i]=1ll*fac[i-1]*i%mod; inv[i]=mod-1ll*(mod/i)*inv[mod%i]%mod; invfac[i]=1ll*invfac[i-1]*inv[i]%mod; } FOR(i,1,n) if(cnt[i]){ int s=0; FOR(j,0,cnt[i]/2){ int x=1ll*C(cnt[i],2*j)*C(2*j,j)%mod*fac[j]%mod; if(j) x=1ll*x*qpow(inv2,j)%mod*qpow(i,j)%mod; if(i%2==1 && i!=1) x=1ll*x*qpow(2,cnt[i]-2*j)%mod; s=(s+x)%mod; } ans=1ll*ans*s%mod; } printf("%d\n",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <queue> #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define N 100100 #define fi first #define se second #define pb push_back #define mp make_pair #define mod 1000000007 #define rep(x, a, b) for(int x=a; x<=b; x++) #define drp(x, a, b) for(int x=a; x>=b; x--) int deg[N], d[N], n, vis[N], a[N], num[N], l[N], ans=1, fac[N], inv[N]; vector<pair<int, int> > fu; queue<int> q; int power(int a, int k){ int ret=1; while(k) { if(k&1) ret=1ll*ret*a%mod; a=1ll*a*a%mod; k>>=1; } return ret; } int C(int n, int m){ return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod; } int pr(int n){ return 1ll*fac[2*n]*inv[n]%mod*power((mod+1)>>1, n)%mod; } void init(){ fac[0]=1; rep(i, 1, n) fac[i]=1ll*fac[i-1]*i%mod; inv[n]=power(fac[n], mod-2); drp(i, n, 1) inv[i-1]=1ll*inv[i]*i%mod; } int st[N<<1]; int main(){ scanf("%d", &n); init(); rep(i, 1, n) { scanf("%d", a+i); deg[a[i]]++; } rep(i, 1, n) if(deg[i]>2) return puts("0"), 0; rep(i, 1, n) d[i]=deg[i]; rep(i, 1, n) if(!d[i]) q.push(i); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=1; if(--d[a[u]]==0) q.push(a[u]); } rep(i, 1, n) if(vis[i] && deg[i]>1) return puts("0"), 0; rep(i, 1, n) if(vis[i] && !deg[i]) { int x=i, cnt=0; for(; vis[x]; x=a[x]) cnt++; l[x]=cnt; } rep(i, 1, n) if(!vis[i]) { int x=i, fl=0, ass=1, tot=0; for(; !vis[x]; x=a[x]) st[++tot]=x, fl|=bool(l[x]), vis[x]=1; if(!fl){ num[tot]++; continue; } fu.clear(); // if(l[i]) fu.pb(mp(l[i], 1)); // for(int p=2, x=a[i]; x!=i; x=a[x]) if(l[x]) fu.pb(mp(l[x], p)); int p=1; x=i; do{ if(l[x]) fu.pb(mp(l[x], p)); x=a[x]; p++; }while(x!=i); //rep(p, 1, tot) if(l[st[p]]) fu.pb(mp(l[st[p]], p)); fu.pb(mp(fu[0].fi, fu[0].se+tot)); int sz=fu.size(); rep(j, 1, sz-1) { if(fu[j].fi>fu[j].se-fu[j-1].se) ass=0; if(fu[j].fi<fu[j].se-fu[j-1].se) ass=2ll*ass%mod; } ans=1ll*ans*ass%mod; } rep(i, 1, n) if(num[i]) { int m=num[i], ass=0; rep(j, 0, m/2) { int tmp=1ll*C(m, 2*j)*pr(j)%mod*power(i, j)%mod; if(i>1 && (i&1)) tmp=1ll*tmp*power(2, m-2*j)%mod; ass=(ass+tmp)%mod; } ans=1ll*ans*ass%mod; } printf("%d\n", ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=(a),_ed=(b);i<=_ed;++i) #define DREP(i,a,b) for(int i=(a),_ed=(b);i>=_ed;--i) #define mp(x,y) make_pair((x),(y)) #define sz(x) (int)(x).size() #define pb push_back typedef long long ll; typedef pair<int,int> pii; inline int read(){ register int x=0,f=1;register char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();} while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();} return f?x:-x; } const int N=1e5+5,mod=1e9+7; int ans=1,n,a[N],d[N],vis[N],is[N],cnt[N],dis[N],f[N]; void work(int u){ int len=0,s=-1,sd,las; while(is[u]){ is[u]=0,++len; if(dis[u]){ if(!~s)s=len,sd=dis[u]; else ans=1ll*ans*((dis[u]<=len-las)+(dis[u]<len-las))%mod; las=len; } u=a[u]; } if(!~s)++cnt[len]; else ans=1ll*ans*((sd<=s+len-las)+(sd<s+len-las))%mod; } int main(){ //freopen("in.in","r",stdin); REP(i,1,n=read())a[i]=read(),++d[a[i]]; REP(u,1,n){ if(vis[u])continue; int p=u;while(!vis[p])vis[p]=u,p=a[p]; if(vis[p]!=u)continue; while(!is[p])is[p]=1,p=a[p]; } REP(i,1,n)if((is[i]&&d[i]>2)||(!is[i]&&d[i]>1))return puts("0"),0; REP(i,1,n)if(!d[i]){ int p=i,l=0; while(!is[p])++l,p=a[p]; dis[p]=l; } REP(i,1,n)if(is[i])work(i); REP(l,1,n){ if(!cnt[l])continue; f[0]=1; REP(i,1,cnt[l]){ if(l>1&&l&1)f[i]=2ll*f[i-1]%mod;//i->p_i + i->p_{p_i}(同构) else f[i]=f[i-1];//i->p_i if(i>1)f[i]=(f[i]+1ll*f[i-2]*(i-1)%mod*l%mod)%mod;//i->p_{p_i}(变成2个环) } ans=1ll*ans*f[cnt[l]]%mod; } printf("%d\n",ans); return 0; } /* 对于一个排列p_i,i向p_i连边,形成若干简单环(每个点入度出度都为1) a_i是p_i或p_{p_i},反映到现图上就是点i的下一个点或下下个点 把p_i的边擦除,连i->a_i的边 会有4中种情况,分别讨论一下 现在就是给定a_i的图还原成p_i的图 */
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; const int mod=1e9+7; int gi() { int x=0,o=1;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') ch=getchar(); if(ch=='-') o=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*o; } int n,ans=1,a[N],du[N],pre[N],cnt[N],f[N]; bool vis[N]; int main() { cin>>n; for(int i=1;i<=n;i++) a[i]=gi(),du[a[i]]++; for(int i=1;i<=n;i++) { if(du[i]>2) return puts("0"),0; if(du[i]<2||vis[i]) continue; int p=i; do { if(vis[p]) return puts("0"),0; vis[p]=1;pre[a[p]]=p;p=a[p]; } while(p!=i); } for(int i=1;i<=n;i++) if(!du[i]) { int p=i,l1=0,l2=0; while(!vis[p]) vis[p]=1,p=a[p],l1++; do l2++,p=pre[p]; while(du[p]!=2); if(l1<l2) ans=2ll*ans%mod; else if(l1>l2) return puts("0"),0; } for(int i=1;i<=n;i++) if(!vis[i]) { int p=i,l=0; do p=a[p],l++,vis[p]=1; while(p!=i); cnt[l]++; } for(int i=1;i<=n;i++) { int mul=1;if(i!=1&&(i&1)) mul++; f[0]=1;f[1]=mul; for(int j=2;j<=cnt[i];j++) f[j]=(1ll*f[j-2]*(j-1)%mod*i%mod+1ll*f[j-1]*mul%mod)%mod; ans=1ll*ans*f[cnt[i]]%mod; } printf("%d\n",ans); return 0; } //orzgzy //鸡贼明年进队超稳
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; const int mod = 1e9+7; int in[100010]; int a[100010]; int vis[100010],line[100010],cycc[100010]; vector<vector<int> > cyc; bool oncyc[100010]; long long int dp[100010]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i],in[a[i]]++; for(int i=1;i<=n;i++) if(!in[i]){ int u=i;vis[i]=i;while(!vis[a[u]]) u=a[u],vis[u]=i; if(vis[a[u]]==i){ cyc.push_back({}); cyc.back().push_back(u); for(int j=u;a[j]!=u;j=a[j]) cyc.back().push_back(a[j]); for(auto it:cyc.back()) oncyc[it]=true; } else if(!oncyc[a[u]]||line[a[u]]){ cout<<0<<endl; return 0; } int cnt=0; for(int j=i;j!=a[u];j=a[j]) cnt++; line[a[u]]=cnt; } for(int i=1;i<=n;i++) if(!vis[i]){ int cnt=0; for(int j=i;!vis[j];j=a[j]) cnt++,vis[j]=i; cycc[cnt]++; } long long int ans=1; for(int i=1;i<=n;i++){ int cnt=cycc[i]; dp[0]=1; dp[1]=1; if(i>1&&(i&1)) dp[1]++; for(int j=2;j<=cnt;j++) dp[j]=(dp[j-1]*dp[1]+dp[j-2]*(j-1)*i)%mod; ans=ans*dp[cnt]%mod; } for(auto it:cyc){ reverse(it.begin(),it.end()); int sz=it.size(); int last=sz; for(int i=0;i<sz;i++,last++) if(line[it[i]]) break; for(int i=sz-1;i>=0;i--) if(line[it[i]]){ int l1=line[it[i]],l2=last-i; if(l1>l2) ans=0; else if(l1<l2) ans=ans*2%mod; last=i; } } cout<<ans<<endl; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; const int maxn=2e5,mod=1e9+7; int n,ctt,tmp,a[maxn],d[maxn],cnt[maxn],cir_cnt[maxn]; long long ans=1,f[maxn]; bool vis[maxn]; vector <int> to[maxn]; queue <int> q; void dfs(int now){ if(cnt[now]!=1) tmp=now; vis[now]=true;ctt++; int siz=to[now].size(); for(int i=0;i<siz;i++){ int u=to[now][i]; if(vis[u]==true) continue; dfs(u); } } int work(int now){ int siz=to[now].size(),len; for(int i=0;i<siz;i++){ int u=to[now][i]; if(u==tmp){len=cnt[tmp]-2;continue;} len=work(u); } if(cnt[now]==1){return len-1;} else{ if(len>=1){ans=0;} if(len<=-1){ans=ans*2%mod;} return cnt[now]-2; } } void Go(int now){ ctt=0;tmp=0; dfs(now); if(!tmp){cir_cnt[ctt]++;} else{work(tmp);} } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){cnt[i]=1;scanf("%d",&a[i]);to[i].push_back(a[i]);d[a[i]]++;} for(int i=1;i<=n;i++){if(d[i]==0) q.push(i);} while(!q.empty()){ int now=q.front();q.pop(); int siz=to[now].size(); vis[now]=true; for(int i=0;i<siz;i++){ int u=to[now][i]; d[u]--; if(cnt[u]!=1) ans=0; else cnt[u]+=cnt[now]; if(d[u]==0) q.push(u); } } for(int i=1;i<=n;i++){if(!vis[i]){Go(i);}} for(int i=1;i<=n;i++){ f[0]=1; int opt=((i&1)&&(i!=1))?2:1,k=cir_cnt[i]; for(int j=1;j<=k;j++){f[j]=0;} for(int j=0;j<k;j++){ f[j+1]=(f[j+1]+f[j]*opt)%mod; f[j+2]=(f[j+2]+f[j]*(k-j-1)%mod*i%mod)%mod; } ans=ans*f[k]%mod; } printf("%lld\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> using namespace std; #define ll long long const int mxN=1e5, M=1e9+7; int n, c[mxN+1]; vector<int> a[mxN]; bool b[mxN]; ll ans=1, d[mxN+1]; void fk() { cout << 0; exit(0); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i=0, b; i<n; ++i) { cin >> b, --b; a[b].push_back(i); } for(int i=0; i<n; ++i) if(a[i].size()>2) fk(); for(int i=0; i<n; ++i) { if(b[i]||a[i].size()<2) continue; int u1=a[i][0], u2=a[i][1]; while(1) { if(u2==i) swap(u1, u2); if(u1==i) break; b[u1]=1; vector<int> v=a[u1]; if(~u2) { v.insert(v.end(), a[u2].begin(), a[u2].end()); b[u2]=1; } if(v.size()>2||!v.size()) fk(); u1=v[0]; if(v.size()>1) u2=v[1]; else { ans=ans*(1+(u2>=0))%M; u2=-1; } } b[u1]=1; if(~u2) { if(a[u2].size()) fk(); b[u2]=1; } } for(int i=0; i<n; ++i) { if(b[i]) continue; int s=0; for(int u=i; !b[u]; u=a[u][0]) { b[u]=1; ++s; } ++c[s]; } d[0]=1; for(int i=1; i<=n; ++i) { for(int j=1; j<=c[i]; ++j) d[j]=(d[j-1]*(i&1&&i>1?2:1)+(j>1?d[j-2]*(j-1)%M*i:0))%M; ans=ans*d[c[i]]%M; } cout << ans; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, MOD = 1e9 + 7; int a[N], vis[N], chain[N], in[N], len[N]; bool in_circle[N]; int main() { int n; scanf("%d", &n); bool flag = 0; for (int i = 1; i <= n; ++ i) { scanf("%d", &a[i]); in[a[i]] ++; if (in[a[i]] >= 3) flag = 1; } if (flag) return 0 * puts("0"); vector<vector<int>> circles; int xxx = 0; for (int i = 1; i <= n; ++ i) { if (vis[i]) continue; ++ xxx; vis[i] = xxx; vector<int> st; st.push_back(i); int j = a[i]; while (!vis[j]) { st.push_back(j); vis[j] = xxx; j = a[j]; } if (vis[j] < xxx) continue; int end = j; vector<int> t; while (t.empty() || t.back() != end) { t.push_back(st.back()); st.pop_back(); } for (int x : t) in_circle[x] = 1; circles.push_back(t); } for (int i = 1; i <= n; ++ i) { if (in_circle[i]) continue; if (in[i] == 2) flag = 1; if (in[i]) continue; int cnt = 0, j = i; while (!in_circle[j]) { j = a[j]; cnt ++; } chain[j] = cnt; } if (flag) return 0 * puts("0"); unordered_map<int, int> nofeet; vector<vector<int>> feet; for (auto &t : circles) { bool has = 0; for (int x : t) { if (chain[x]) has = 1; } if (has) feet.push_back(t); else nofeet[(int) t.size()] ++; } int ans = 1; for (auto xy : nofeet) { int n = xy.first, cnt = xy.second; //printf("no feet : n=%d cnt=%d\n", n, cnt); static int dp[N]; dp[0] = 1; for (int i = 1; i <= cnt; ++ i) { dp[i] = 1LL * dp[i - 1] * ((n & 1) && n > 1 ? 2 : 1) % MOD; if (i > 1) (dp[i] += 1LL * (i - 1) * n % MOD * dp[i - 2] % MOD) %= MOD; } ans = 1LL * ans * cnt[dp] % MOD; } for (auto &t : feet) { int n = (int) t.size(), st; for (int i = 0; i < n; ++ i) if (chain[t[i]]) { st = i; break; } int res = 0; for (int i = 0; i < n; ++ i) { if (chain[t[(st + n - i) % n]]) { len[t[(st + n - i) % n]] = res; res = 0; } else ++ res; } len[t[st]] = res; for (int i : t) { if (!chain[i]) continue; if (chain[i] > len[i] + 1) ans = 0; if (chain[i] < len[i] + 1) (ans *= 2) %= MOD; } } printf("%d\n", ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+10,p=1e9+7; int inc(int x,int y){x+=y;return x>=p?x-p:x;} int mul(int x,int y){return (ll)x*y%p;} int n,a[N],cir[N],link[N],len[N],top[N]; bool ok[N]; int length(int x){ if (top[x]) return link[x]; link[x]=length(a[x])+1; top[x]=top[a[x]]; return link[x]; } int cnt[N]; void init(){ static int vis[N],deg[N]; scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]),deg[a[i]]++; for (int i=1;i<=n;i++) if (!vis[i]){ int j=i; while (!vis[j]) vis[j]=i,j=a[j]; if (vis[j]!=i) continue; int p=j; for (cir[p]=p,j=a[j];j!=p;cir[j]=p,j=a[j]); } for (int i=1;i<=n;i++) if (cir[i]) len[cir[i]]++,top[i]=i; for (int i=1;i<=n;i++) length(i); for (int i=1;i<=n;i++){ if (deg[i]>2) puts("0"),exit(0); if (deg[i]>1&&!cir[i]) puts("0"),exit(0); } for (int i=1;i<=n;i++) link[top[i]]=max(link[top[i]],link[i]); for (int i=1;i<=n;i++) if (link[i]) ok[cir[top[i]]]=1; for (int i=1;i<=n;i++) if (!ok[i]&&len[i]) cnt[len[i]]++; } typedef pair<int,int> pii; int calc(int L,int n){ static map<pii,int> M; if (!n) return 1; pii now(L,n); if (M.count(now)) return M[now]; int ans=calc(L,n-1); if ((L&1)&&L>1) ans=inc(ans,ans); if (n>1) ans=inc(ans,mul(mul(L,(n-1)),calc(L,n-2))); return M[now]=ans; } int q[N],size; int Calc(){ static int cnt[N]; for (int i=0;i<=size*2;i++) cnt[i]=0; for (int i=1;i<=size;i++) if (q[i]>size) return 0; for (int i=1;i<=size;i++) cnt[size+i-q[i]]++,cnt[size+i]--; for (int i=1;i<=size*2;i++) cnt[i]+=cnt[i-1]; for (int i=1;i<=size;i++) cnt[i]=cnt[i+size]=cnt[i]+cnt[i+size]; cnt[0]=cnt[size]; for (int i=1;i<=size;i++) if (cnt[i]>1) return 0; int ans=1; for (int i=1;i<=size;i++) if (q[i]&&!cnt[size+i-q[i]-1]) ans=inc(ans,ans); return ans; } int main() { init(); int ans=1; for (int i=1;i<=n;i++) if (cnt[i]) ans=mul(ans,calc(i,cnt[i])); for (int i=1;i<=n;i++) if (ok[i]&&len[i]){ q[size=1]=link[i]; for (int j=a[i];j!=i;j=a[j]) q[++size]=link[j]; ans=mul(ans,Calc()); } printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
// Next or Nextnext // * frank_c1 // * 2017 / 09 / 29 #include <bits/stdc++.h> using namespace std; typedef long long LL; LL pow_mod(LL b, LL p, LL k) { LL ret = 1; for (; p; p >>= 1) { if (p & 1) ret = ret * b % k; b = b * b % k; } return ret; } const int maxn = (int)(1e5) + 5; const int mo = (int)(1e9) + 7; int a[maxn], in[maxn], w[maxn], cnt[maxn], vi[maxn], s[maxn]; int q[maxn], l = 1, r, len; LL fac[maxn], ivf[maxn], pw[maxn]; inline LL C(int n, int m) { if (m < 0 || n - m < 0) return 0; return (fac[n] * ivf[m] % mo) * ivf[n - m] % mo; } inline LL F(int n, int m) { return (C(n, m << 1) * C(m << 1, m) % mo) * (pow_mod(pw[m], mo - 2, mo) * fac[m] % mo) % mo; } int calc() { LL now = 1; for (int i = 1; i <= len; ++i) if (cnt[w[i]]) { int ps = i, v = 0; do { ++v; --ps; if (ps < 1) ps = len; } while (!cnt[w[ps]]); if (v < cnt[w[i]]) return 0; if (v > cnt[w[i]]) now = now * 2 % mo; } return now; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), ++in[a[i]]; fac[0] = ivf[0] = pw[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % mo, pw[i] = pw[i - 1] * 2 % mo; ivf[n] = pow_mod(fac[n], mo - 2, mo); for (int i = n - 1; i >= 1; --i) ivf[i] = ivf[i + 1] * (i + 1) % mo; memcpy(w, in, sizeof(in)); for (int i = 1; i <= n; ++i) if (!in[i]) q[++r] = i; for (; l <= r; ) { int x = q[l++]; if (!(--in[a[x]])) q[++r] = a[x]; cnt[a[x]] = cnt[x] + 1; } for (int i = 1; i <= n; ++i) if (w[i] - in[i] > 1) return printf("0\n"), 0; LL ret = 1; for (int i = 1; i <= n; ++i) if (in[i] && !vi[i]) { int x = i, c = 0; len = 0; while (!vi[x]) { w[++len] = x; vi[x] = 1; if (cnt[x]) c = 1; x = a[x]; } if (!c) ++s[len]; else ret = ret * calc() % mo; } for (int i = 1; i <= n; ++i) if (s[i]) { LL tmp = 0, ww = 1; for (int j = 0; j <= (s[i] >> 1); ++j) { LL buf = 1; (buf *= (F(s[i], j) * ww % mo)) %= mo; if ((i & 1) && i > 1) (buf *= pw[s[i] - (j << 1)]) %= mo; ww = ww * i % mo; (tmp += buf) %= mo; } (ret *= tmp) %= mo; } return printf("%lld\n", ret), 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #define maxn 100005 #define mod 1000000007 int n,a[maxn],pre[maxn],deg[maxn],cnt[maxn],f[maxn],ans=1; bool vis[maxn]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); deg[a[i]]++; } for(int i=1;i<=n;i++) { if(deg[i]>2) { printf("0\n"); return 0; } if(deg[i]<2||vis[i]) continue; int p=i; do { if(vis[p]) { printf("0\n"); return 0; } vis[p]=true,pre[a[p]]=p,p=a[p]; }while(p!=i); } for(int i=1;i<=n;i++) if(!deg[i]) { int p=i,l1=0,l2=0; while(!vis[p]) vis[p]=true,p=a[p],l1++; do l2++,p=pre[p]; while(deg[p]!=2); if(l1<l2) ans=ans*2%mod; else if(l1>l2) { printf("0\n"); return 0; } } for(int i=1;i<=n;i++) if(!vis[i]) { int p=i,l=0; do l++,p=a[p],vis[p]=true; while(p!=i); cnt[l]++; } for(int i=1;i<=n;i++) { int mul=1; if(i!=1&&(i&1)) mul++; f[0]=1,f[1]=mul; for(int j=2;j<=cnt[i];j++) f[j]=(1ll*f[j-2]*(j-1)%mod*i%mod+1ll*f[j-1]*mul%mod)%mod; ans=1ll*ans*f[cnt[i]]%mod; } printf("%d\n",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> using namespace std; const long long mod=1000000007ll; int main() { int n; scanf("%d",&n); static int a[100000]; for(int i=0;i<n;i++){ scanf("%d",a+i); a[i]--; } vector<vector<int> > e(n); for(int i=0;i<n;i++){ e[a[i]].push_back(i); } static int b[100000]; static int T[100000]; for(int i=0;i<n;i++){ b[i]=-1; } int C[100001]={0}; long long ans=1ll; for(int t=0;t<n;t++){ if(b[t]!=-1){ continue; } int i=t; while(b[i]==-1){ b[i]=t; i=a[i]; } if(b[i]!=t){ continue; } int p=i; i=a[i]; int ini=i; int N=0; bool F=0; do{ if(e[i].size()>2){ ans=0ll; goto ZERO; } if(e[i].size()==1){ T[N]=0; } else{ F=1; int q=e[i][0]+e[i][1]-p; T[N]=1; while(e[q].size()>0){ if(e[q].size()>1){ ans=0ll; goto ZERO; } q=e[q][0]; T[N]++; } } N++; p=i; i=a[i]; }while(i!=ini); if(!F){ C[N]++; } else{ for(int i=0;i<N;i++){ if(T[i]==0){ continue; } int j=1; while(T[(i+N-j)%N]==0){ j++; } if(j>=T[i]+1){ ans*=2; ans%=mod; } else if(j<T[i]){ ans=0; goto ZERO; } } } } for(int i=1;i<=n;i++){ if(C[i]==0){ continue; } vector<long long> dp(C[i]+1); dp[0]=1ll; dp[1]=(i>1&&i%2==1?2:1); for(int j=2;j<=C[i];j++){ dp[j]=(dp[j-1]*dp[1]%mod+dp[j-2]*(j-1)%mod*i%mod)%mod; } ans*=dp[C[i]]; ans%=mod; } ZERO: printf("%lld\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <queue> #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define N 100100 #define fi first #define se second #define pb push_back #define mp make_pair #define mod 1000000007 #define rep(x, a, b) for(int x=a; x<=b; x++) #define drp(x, a, b) for(int x=a; x>=b; x--) int deg[N], d[N], n, vis[N], a[N], num[N], l[N], ans=1, fac[N], inv[N]; vector<pair<int, int> > fu; queue<int> q; int power(int a, int k){ int ret=1; while(k) { if(k&1) ret=1ll*ret*a%mod; a=1ll*a*a%mod; k>>=1; } return ret; } int C(int n, int m){ return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod; } int pr(int n){ return 1ll*fac[2*n]*inv[n]%mod*power((mod+1)>>1, n)%mod; } void init(){ fac[0]=1; rep(i, 1, n) fac[i]=1ll*fac[i-1]*i%mod; inv[n]=power(fac[n], mod-2); drp(i, n, 1) inv[i-1]=1ll*inv[i]*i%mod; } int main(){ scanf("%d", &n); init(); rep(i, 1, n) { scanf("%d", a+i); deg[a[i]]++; } rep(i, 1, n) if(deg[i]>2) return puts("0"), 0; rep(i, 1, n) d[i]=deg[i]; rep(i, 1, n) if(!d[i]) q.push(i); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=1; if(--d[a[u]]==0) q.push(a[u]); } rep(i, 1, n) if(vis[i] && deg[i]>1) return puts("0"), 0; rep(i, 1, n) if(vis[i] && !deg[i]) { int x=i, cnt=0; for(; vis[x]; x=a[x]) cnt++; l[x]=cnt; } rep(i, 1, n) if(!vis[i]) { int x=i, fl=0, ass=1, tot=0; for(; !vis[x]; x=a[x]) ++tot, fl|=bool(l[x]), vis[x]=1; if(!fl){ num[tot]++; continue; } fu.clear(); int p=1; x=i; do{ if(l[x]) fu.pb(mp(l[x], p)); x=a[x]; p++; }while(x!=i); fu.pb(mp(fu[0].fi, fu[0].se+tot)); int sz=fu.size(); rep(j, 1, sz-1) { if(fu[j].fi>fu[j].se-fu[j-1].se) ass=0; if(fu[j].fi<fu[j].se-fu[j-1].se) ass=2ll*ass%mod; } ans=1ll*ans*ass%mod; } rep(i, 1, n) if(num[i]) { int m=num[i], ass=0; rep(j, 0, m/2) { int tmp=1ll*C(m, 2*j)*pr(j)%mod*power(i, j)%mod; if(i>1 && (i&1)) tmp=1ll*tmp*power(2, m-2*j)%mod; ass=(ass+tmp)%mod; } ans=1ll*ans*ass%mod; } printf("%d\n", ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> #include <cassert> #include <cstdlib> #include <vector> #include <map> const int64_t MOD=1e9+7; const int64_t INV2=5e8+4; int vis[100005]; int next[100005]; std::vector<int> prev[100005]; int buddy[100005]; int root[100005]; bool cycle[100005]; int64_t inv[100005]; void fail(){ printf("0\n"); exit(0); } void dfs_root(int node){ root[node]=node; if(!root[next[node]]) dfs_root(next[node]); root[node]=root[next[node]]; } int main(){ int N; scanf("%d",&N); inv[1]=1; for(int i=2;i<=N;i++){ inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD; } for(int i=1;i<=N;i++){ scanf("%d",&next[i]); prev[next[i]].push_back(i); } for(int i=1;i<=N;i++){ if(prev[i].size()>2) fail(); } for(int i=1;i<=N;i++){ int layer[4]; int layer_cnt=0; for(int x:prev[i]) layer[layer_cnt++]=x; while(layer_cnt==2){ int a=layer[0],b=layer[1]; //printf("LAYER %d:%d\n",a,b); if(buddy[a]||buddy[b]){ assert(buddy[a]==b); assert(buddy[b]==a); break; } buddy[a]=b,buddy[b]=a; layer_cnt=0; for(int x:prev[a]) layer[layer_cnt++]=x; for(int x:prev[b]) layer[layer_cnt++]=x; } if(layer_cnt>2) fail(); } std::map<int,int> pures; for(int i=1;i<=N;i++){ if(!root[i]) dfs_root(i); } for(int i=1;i<=N;i++){ if(root[i]==i){ bool ispure=true; int length=0; int j=i; do{ if(prev[j].size()>1) ispure=false; cycle[j]=true; length++; j=next[j]; }while(j!=i); if(ispure){ pures[length]++; } } } int64_t ans=1; for(int i=1;i<=N;i++){ if(cycle[i]&&buddy[next[i]]&&!buddy[i]){ ans=ans*2%MOD; } } for(auto pair:pures){ int64_t len=pair.first,cnt=pair.second; //printf("PURE %ld x%ld\n",len,cnt); int64_t ac=1; if(len%2==1&&len>1){ for(int i=0;i<cnt;i++){ ac=ac*2%MOD; } } int64_t sum=ac; //printf(" %ld",ac); int pairs=0; while(cnt>1){ ac=(ac*len)%MOD*((cnt*(cnt-1)/2)%MOD)%MOD*inv[++pairs]%MOD; cnt-=2; if(len%2==1&&len>1){ ac=ac*INV2%MOD*INV2%MOD; } sum=(sum+ac)%MOD; //printf("+%ld",ac); } //printf(" => %ld\n",sum); ans=ans*sum%MOD; } printf("%ld\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define RI register int #define g getchar() using namespace std; typedef long long ll; const int mod=1e9+7,N=1e5+10; void qr(int &x) { char c=g;x=0; while(!isdigit(c))c=g; while(isdigit(c))x=x*10+c-'0',c=g; } int n; ll ans; int a[N],deg[N],vis[N],footL[N],sum[N],f[N]; bool cir[N]; int qm(int x) {return x>=mod?x-mod:x;} void workcir(int x) { int now=0,fr=0,frL=0,ed=0; //now是当前环的长度,fr是第一个有脚点的位置,frL是位置对应的脚长,ed为上一个有脚点的位置。 while(cir[x]) { ++now; cir[x]=0; if(footL[x]) { if(!fr) ed=fr=now,frL=footL[x]; else { ans=ans*((footL[x]<now-ed)+(footL[x]<=now-ed))%mod;//脚的放法 ed=now; } } x=a[x]; } if(!ed) ++sum[now]; else ans=ans*((frL<now-ed+fr)+(frL<=now-ed+fr))%mod; } void work() { for(RI i=1;i<=n;i++) {//脚对环上交接点贡献 if(deg[i]) continue; int x=i,len=0; while(!cir[x])x=a[x],++len; footL[x]=len;//脚长 } ans=f[0]=1; for(RI i=1;i<=n;i++) if(cir[i]) workcir(i);//以环为基准计算基环内向树的情况 for(RI i=1;i<=n;i++) {//计算简单环 for(RI j=1;j<=sum[i];j++) { if(i>1&&(i&1)) f[j]=qm(f[j-1]<<1); else f[j]=f[j-1]; if(j>1) f[j]=qm(f[j]+(ll)f[j-2]*(j-1)%mod*i%mod); } ans=ans*f[sum[i]]%mod; } } int main() { qr(n); for(RI i=1;i<=n;i++) qr(a[i]),++deg[a[i]]; for(RI i=1;i<=n;i++) if(!vis[i]) { int x=i;while(!vis[x]) vis[x]=i,x=a[x]; if(vis[x]!=i) continue;//脚 while(!cir[x]) cir[x]=1,x=a[x];//标记环上点 } for(RI i=1;i<=n;i++) if((cir[i]&&deg[i]>2)||(!cir[i]&&deg[i]>1)) return puts("0"),0;//开叉就不行 work(); printf("%lld\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<cstdio> #include<vector> using namespace std; const int MAXN = 100000; const int MOD = int(1E9) + 7; vector<int>rev[MAXN + 5]; int a[MAXN + 5], f[MAXN + 5], dis[MAXN + 5], dep[MAXN + 5]; bool vis[MAXN + 5], vis2[MAXN + 5], tag[MAXN + 5]; int siz, del, ans = 1, tmp; void dfs1(int x) { siz++; if( rev[x].size() >= 2 ) tmp = x; vis[x] = true; for(int i=0;i<rev[x].size();i++) if( !vis[rev[x][i]] ) dfs1(rev[x][i]); if( !vis[a[x]] ) dfs1(a[x]); } int dfs2(int x) { if( rev[x].size() == 0 ) return 1; else if( rev[x].size() == 1 ) { int d = dfs2(rev[x][0]); if( d == -1 ) return -1; else return d + 1; } else return -1; } int fact[MAXN + 5], inv[MAXN + 5], fun[MAXN + 5]; int pow_mod(int b, int p) { int ret = 1; while( p ) { if( p & 1 ) ret = 1LL*ret*b%MOD; b = 1LL*b*b%MOD; p >>= 1; } return ret; } void init() { fact[0] = 1; for(int i=1;i<=MAXN;i++) fact[i] = 1LL*fact[i-1]*i%MOD; inv[MAXN] = pow_mod(fact[MAXN], MOD-2); for(int i=MAXN-1;i>=0;i--) inv[i] = 1LL*inv[i+1]*(i+1)%MOD; fun[0] = 1; for(int i=1;i<=MAXN;i++) fun[i] = 1LL*fun[i-1]*(2*i-1)%MOD; } int comb(int n, int m) { return 1LL*fact[n]*inv[m]%MOD*inv[n-m]%MOD; } int main() { init(); int N; scanf("%d", &N); for(int i=1;i<=N;i++) { scanf("%d", &a[i]); rev[a[i]].push_back(i); } for(int i=1;i<=N;i++) if( !vis[i] ) { tmp = -1; siz = 0; dfs1(i); if( tmp != -1 ) { int nw = tmp; do { vis2[nw] = true; nw = a[nw]; }while( !vis2[nw] ); if( nw != tmp ) { ans = 0; continue; } int p = nw; del = 1; do { tag[p] = true; if( rev[p].size() > 2 ) del = 0; p = a[p]; }while( p != nw ); if( del == 0 ) { ans = 0; continue; } int tmp; do { if( rev[p].size() == 2 ) { dep[p] = tag[rev[p][0]] ? dfs2(rev[p][1]) : dfs2(rev[p][0]); if( dep[p] == -1 ) del = 0; tmp = p; } p = a[p]; }while( p != nw ); p = nw = tmp; int lst = 0; do { p = a[p]; lst++; dis[p] = lst; if( rev[p].size() == 2 ) { lst = 0; if( dis[p] > dep[p] ) del = 2LL*del%MOD; else if( dis[p] < dep[p] ) del = 0; } }while( p != nw ); ans = 1LL*ans*del%MOD; } else f[siz]++; } for(int i=1;i<=N;i++) { if( (i & 1) && i != 1 ) { del = 0; for(int j=0;j<=f[i];j+=2) del = (del + 1LL*comb(f[i], j)*fun[j/2]%MOD*pow_mod(i, j/2)%MOD*pow_mod(2, f[i]-j)%MOD)%MOD; ans = 1LL*del*ans%MOD; } else { del = 0; for(int j=0;j<=f[i];j+=2) del = (del + 1LL*comb(f[i], j)*fun[j/2]%MOD*pow_mod(i, j/2)%MOD)%MOD; ans = 1LL*del*ans%MOD; } } printf("%d\n", ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<stdio.h> typedef long long ll; const int mod=1000000007; int mul(int a,int b){return(ll)a*b%mod;} int a[100010],d[100010],p[100010],f[100010],c[100010]; bool v[100010]; int main(){ int n,i,j,c1,c2,ans; scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d",a+i); d[a[i]]++; } #define wrong {putchar('0');return 0;} ans=1; for(i=1;i<=n;i++){ if(d[i]>2)wrong if(v[i]||d[i]<2)continue; j=i; do{ if(v[j])wrong v[j]=1; p[a[j]]=j; j=a[j]; }while(j!=i); } for(i=1;i<=n;i++){ if(d[i]==0){ c1=0; for(j=i;d[j]!=2;j=a[j]){ c1++; v[j]=1; } c2=1; for(j=p[j];d[j]!=2;j=p[j])c2++; if(c1>c2)wrong if(c1<c2)ans=mul(ans,2); } } for(i=1;i<=n;i++){ if(!v[i]){ c1=0; j=i; do{ v[j]=1; j=a[j]; c1++; }while(j!=i); c[c1]++; } } for(i=1;i<=n;i++){ c1=1+(i!=1&&(i&1)); f[0]=1; f[1]=c1; for(j=2;j<=c[i];j++)f[j]=(mul(f[j-2],mul(j-1,i))+mul(f[j-1],c1))%mod; ans=mul(ans,f[c[i]]); } printf("%d",ans); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include"bits/stdc++.h" #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fr(x) freopen(x,"r",stdin) #define fw(x) freopen(x,"w",stdout) #define iout(x) printf("%d\n",x) #define lout(x) printf("%lld\n",x) #define REP(x,l,u) for(int x = (l);x<=(u);x++) #define RREP(x,l,u) for(int x = (l);x>=(u);x--) #define mst(x,a) memset(x,a,sizeof(x)) #define PII pair<int,int> #define PLL pair<ll,ll> #define MP make_pair #define se second #define fi first #define dbg(x) cout<<#x<<" = "<<(x)<<endl; #define sz(x) ((int)x.size()) #define cl(x) x.clear() typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; using namespace std; const int maxn = 100010; const int mod = 1e9+7; const int MAX = 1000000010; const double eps = 1e-6; const double PI = acos(-1); template<typename T> inline void read(T &x){ x=0;T f=1;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f; } template<typename A,typename B> inline void read(A&x,B&y){read(x);read(y);} template<typename A,typename B,typename C> inline void read(A&x,B&y,C&z){read(x);read(y);read(z);} template<typename A,typename B,typename C,typename D> inline void read(A&x,B&y,C&z,D&w){read(x);read(y);read(z);read(w);} template<typename A,typename B> inline A fexp(A x,B p){A ans=1;for(;p;p>>=1,x=1LL*x*x%mod)if(p&1)ans=1LL*ans*x%mod;return ans;} template<typename A,typename B> inline A fexp(A x,B p,A mo){A ans=1;for(;p;p>>=1,x=1LL*x*x%mo)if(p&1)ans=1LL*ans*x%mo;return ans;} int n,ans=1; int A[maxn],vi[maxn],Is[maxn],ch[maxn],has[maxn],f[maxn],deg[maxn]; int calc(int x,int y){ if(x<y)return 2; if(x>y)return 0; return 1; } void Work(){ REP(i,1,n)if(deg[i]>2||(deg[i]==2&&!Is[i]))return void(puts("0")); REP(i,1,n)if(!deg[i]){ int x=i,t=0;while(!Is[x])x=A[x],t++; ch[x]=t; } REP(i,1,n)if(Is[i]){ int x=i; do x=A[x];while(!ch[x]&&x!=i); if(x==i&&!ch[x]){ int t=0; do x=A[x],t++,Is[x]=0;while(x!=i); has[t]++; continue; } int j=x,now=1;x=A[x]; for(;;){ if(ch[x]){ ans=1ll*ans*calc(ch[x],now)%mod; now=0; } if(x==j)break; now++;x=A[x]; } while(Is[x])Is[x]=0,x=A[x]; } REP(i,1,n)if(has[i]){ int x=(i>1&&(i&1))+1; f[0]=1; REP(j,1,has[i]){ f[j]=1ll*f[j-1]*x%mod; if(j>=2)f[j]=(1ll*f[j-2]*(j-1)%mod*i+f[j])%mod; } ans=1ll*ans*f[has[i]]%mod; } iout(ans); } void Init(){ read(n); REP(i,1,n)read(A[i]); REP(i,1,n)deg[A[i]]++; REP(i,1,n)if(!vi[i]){ int x=i; while(!vi[x])vi[x]=i,x=A[x]; if(vi[x]==i){ while(!Is[x])Is[x]=1,x=A[x]; } } } int main(){ Init(); Work(); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <bits/stdc++.h> using namespace std; typedef double db; typedef long long ll; typedef long double ld; typedef pair<int,int> pa; typedef unsigned int uint; typedef unsigned long long ull; #define w1 first #define ls (x<<1) #define w2 second #define rs (x<<1|1) #define mp make_pair #define pb push_back #define mid ((l+r)>>1) #define SZ(x) (int((x).size())) #define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++) #define rep2(i,a,b) for(int (i)=(a);(i)<(b);(i)++) #define per(i,a,b) for(int (i)=(a);(i)>=(b);(i)--) #define Rep(p,x) for(int (p)=head[(x)];(p);(p)=nxt[(p)]) #define Rep2(p,x) for(int (p)=cur[(x)];(p);(p)=nxt[(p)]) template<class T>void read(T&num){ num=0;T f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar(); num*=f; } const int maxn=1e5+5,mod=1e9+7; int n,ans=1; int a[maxn],d[maxn],vis[maxn],len[maxn],q[maxn],cnt[maxn],f[maxn]; void solve(int n,int m){ int now=1+(n>1&&(n&1)); f[0]=1; f[1]=now; rep(i,2,m)f[i]=(1ll*now*f[i-1]%mod+1ll*(i-1)*n%mod*f[i-2]%mod)%mod; ans=1ll*ans*f[m]%mod; } int main(){ read(n); rep(i,1,n)read(a[i]),d[a[i]]++; rep(i,1,n)if(d[i]>2)ans=0; rep(i,1,n)if(!d[i]){ int j=i,l=0; for(;d[j]<2;j=a[j],l++)vis[j]=1; len[j]=l; } rep(i,1,n)if(!vis[i]){ int tot=0,j; for(j=i;!vis[j];j=a[j])vis[j]=1,q[++tot]=j; if(j!=i)ans=0; int cir=1; rep(j,1,tot)if(len[q[j]])cir=0; if(cir)cnt[tot]++; else{ int maxl=1; per(j,tot,1) if(!len[q[j]])maxl++; else break; rep(j,1,tot) if(len[q[j]]){ if(len[q[j]]<maxl)ans=2ll*ans%mod; if(len[q[j]]>maxl)ans=0; maxl=1; }else maxl++; } } rep(i,1,n)if(cnt[i])solve(i,cnt[i]); printf("%d\n",ans); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<stdio.h> #include<math.h> #include<algorithm> #include<queue> #include<string.h> #include<vector> #include<set> using namespace std; const long long mod=1000000007; const long long inf=mod*mod; long long fact[310000]; long long inv[310000]; long long finv[310000]; int p[110000]; int cnt[110000]; vector<int>g[110000]; vector<int>rev[110000]; int v[110000]; int cur; int num[110000]; long long pw(long long a,long long b){ long long ret=1; while(b){ if(b%2)ret=ret*a%mod; b/=2;a=a*a%mod; } return ret; } void dfs(int a){ for(int i=0;i<g[a].size();i++){ if(v[g[a][i]])continue; v[g[a][i]]=1; dfs(g[a][i]); } num[cur++]=a; } int scc[110000]; void dfs2(int a){ scc[a]=cur; for(int i=0;i<rev[a].size();i++){ if(v[rev[a][i]])continue; v[rev[a][i]]=1; dfs2(rev[a][i]); } } int dfs3(int a,int b){ int ret=1; for(int i=0;i<rev[a].size();i++){ if(b==scc[rev[a][i]])continue; ret+=dfs3(rev[a][i],b); } return ret; } int cy[110000]; int in[110000]; int out[110000]; int sz[110000]; int fi[110000]; int ren[110000]; long long dp[110000]; int main(){ fact[0]=finv[0]=1; inv[1]=1; for(int i=1;i<310000;i++){ fact[i]=fact[i-1]*i%mod; } for(int i=2;i<310000;i++){ inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod; } for(int i=1;i<310000;i++){ finv[i]=finv[i-1]*inv[i]%mod; } int a;scanf("%d",&a); for(int i=0;i<a;i++){ scanf("%d",p+i);p[i]--; cnt[p[i]]++; } for(int i=0;i<a;i++)if(cnt[i]>2){ printf("0\n");return 0; } for(int i=0;i<a;i++){ g[i].push_back(p[i]); rev[p[i]].push_back(i); } for(int i=0;i<a;i++){ if(v[i])continue; v[i]=1; dfs(i); } cur=0; for(int i=0;i<a;i++)v[i]=0; for(int i=a-1;i>=0;i--){ if(v[num[i]])continue; v[num[i]]=1; fi[cur]=num[i]; dfs2(num[i]); cur++; } for(int i=0;i<a;i++){ in[scc[p[i]]]++; sz[scc[i]]++; if(scc[p[i]]!=scc[i])out[scc[i]]++; } for(int i=0;i<a;i++){ if(cnt[i]>1&&scc[i]!=scc[p[i]]){ printf("0\n");return 0; } } long long ret=1; for(int i=0;i<cur;i++){ //printf("%d %d\n",in[i],sz[i]); if(out[i])continue; if(in[i]==sz[i]){ cy[sz[i]]++; continue; } if(out[i])continue; vector<int>L; int at=fi[i]; while(1){ L.push_back(dfs3(at,scc[at])-1); at=p[at]; if(at==fi[i])break; } int now=0; for(int j=0;j<L.size()*2;j++){ ren[j%L.size()]=now; if(L[j%L.size()]==0)now++; else now=1; } for(int j=0;j<L.size();j++){ if(L[j]==0)continue; if(ren[j]<L[j])ret*=0; else if(ren[j]>L[j])ret=ret*2%mod; } } for(int i=1;i<=a;i++){ long long tmp=0; for(int j=0;j*2<=cy[i];j++){ long long ks=pw(i,j)*fact[cy[i]]%mod*finv[cy[i]-j*2]%mod*finv[j]%mod*pw(500000004,j)%mod; if(i%2&&i!=1){ ks=ks*pw(2,cy[i]-j*2)%mod; } tmp=(tmp+ks)%mod; } ret=ret*tmp%mod; } printf("%lld\n",ret); }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; const int Q=1<<17,MOD=1e9+7; inline int add(int a,int b) {a+=b;return a>=MOD?a-MOD:a;} inline int mul(int a,int b) {return 1LL*a*b%MOD;} int a[Q]; int vis[Q]; int pi=1,maxn=0; int val[Q]; int cir[Q]; void dfs(int x,int Time) { if(vis[x]){ if(vis[x]!=Time||cir[x])return; cir[x]=1; for(int t=a[x];t!=x;t=a[t]) cir[t]=1; return; } vis[x]=Time; dfs(a[x],Time); } int in[Q]; #define GG return puts("0")&0 int f[Q]; int Ga(int x) {return cir[x]?x:(a[x]=Ga(a[x]));} int len[Q]; int l[Q]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),++in[a[i]]; for(int i=1;i<=n;i++) if(in[i]>2)GG; for(int i=1;i<=n;i++) if(!vis[i])dfs(i,i); for(int i=1;i<=n;i++){ if(!cir[i]) if(in[i]>1)GG; ++len[Ga(i)]; } for(int i=1;i<=n;i++) if(cir[i]&&vis[i]!=-1){ int mx=0; for(int t=i;vis[t]!=-1;t=a[t]){ vis[t]=-1; l[++mx]=len[t]-1; } for(int i=1;i<=mx;i++) l[mx+i]=l[i]; int lst=998244353,mus=0; for(int i=(mx<<1);i;--i) if(l[i]){ if(lst-i<mus)GG; if(lst-i>mus&&lst>mx&&lst<=(mx<<1))pi=mul(pi,2); lst=i; mus=l[i]; } if(lst>n)val[++maxn]=mx; } sort(val+1,val+maxn+1); f[0]=1; for(int i=1,had=0;i<=maxn;i++){ if(val[i]==val[i-1])++had; else had=0; f[i]=add(mul(f[i-1],((val[i]&1)&(val[i]>1))+1),i==1?0:mul(mul(had,val[i]),f[i-2])); } printf("%d",mul(f[maxn],pi)); return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
#include<bits/stdc++.h> #define LL long long using namespace std; const int mod=1e9+7; int fastpow(int x,int a){ int ret=1; while(a){ if(a&1) ret=(LL)ret*x%mod; x=(LL)x*x%mod;a>>=1; } return ret; } struct Combin{ #define N 200005 int fac[N],rv[N],facrv[N]; Combin(){ fac[0]=rv[1]=facrv[0]=1; for(int i=2;i<N;i++) rv[i]=((-(LL)(mod/i)*rv[mod%i]%mod)+mod)%mod; for(int i=1;i<N;i++) fac[i]=(LL)fac[i-1]*i%mod; for(int i=1;i<N;i++) facrv[i]=(LL)facrv[i-1]*rv[i]%mod; } int C(int r1,int n1){ if(r1>n1) return 0; return fac[n1]*(LL)facrv[r1]%mod*facrv[n1-r1]%mod; } #undef N }C; #define N 100005 int p[N],n,cnt,in[N],len[N],vis[N]; int ans=1; void tp(int x){ if(len[p[x]]!=0) ans=0; len[p[x]]=len[x]+1; len[x]=-1; in[p[x]]--; if(in[p[x]]==0) tp(p[x]); } int sz[N]; void solve(int id){ vis[id]=1; if(p[id]==id&&len[id]==0){sz[1]++;return;} deque<int> v; bool iscir=true; v.push_back(len[id]); for(int i=p[id];i!=id;i=p[i]) vis[i]=1,v.push_back(len[i]); for(int i:v) if(i!=0) iscir=false; if(iscir){ sz[v.size()]++; }else{ while(v.back()==0) v.push_front(0),v.pop_back(); while(!v.empty()){ int val=v.back();v.pop_back(); while(!v.empty() && v.back()==0) v.pop_back(),val--; if(val==1) continue; else if(val<1) ans=ans*2%mod; else ans=0; } } } int f(int j){//j = 2k //how many ways to matchs //if there k matchs in 2k points //ans = C(2,2k)*C(2,2k-2)...*C(2,4)*C(2,2)/(k!) // =C(2k-2,2k)*C(2k-4,2k-2)...C(0,2)/(k!) // =(2k)!/((2!)^k)/(k!) return C.fac[j]*1LL*C.facrv[j/2]%mod*fastpow(C.facrv[2],j/2)%mod; } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&p[i]),in[p[i]]++; for(int i=1;i<=n;i++) if(in[i]==0&&len[i]==0) tp(i); if(ans==0) return puts("0"),0; for(int i=1;i<=n;i++) if(vis[i]==0&&len[i]>=0) solve(i); for(int i=1;i<=n;i++){ int ansnow=0; if(i==1||i%2==0){ for(int j=0;j<=sz[i];j+=2) ansnow = (ansnow + C.C(j,sz[i]) *1LL* f(j)%mod *fastpow(i,j/2)%mod * fastpow(1,sz[i]-j) )%mod; }else{ for(int j=0;j<=sz[i];j+=2) ansnow = (ansnow + C.C(j,sz[i]) *1LL* f(j)%mod *fastpow(i,j/2)%mod * fastpow(2,sz[i]-j) )%mod; } ans = ans*1LL*ansnow%mod; } cout<<ans<<endl; return 0; }
CPP
p03842 AtCoder Grand Contest 008 - Next or Nextnext
You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition? * For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i. Find the count modulo 10^9 + 7. Constraints * 1 ≤ N ≤ 10^5 * a_i is an integer. * 1 ≤ a_i ≤ N Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7. Examples Input 3 1 2 3 Output 4 Input 2 1 1 Output 1 Input 3 2 1 1 Output 2 Input 3 1 1 1 Output 0 Input 13 2 1 4 3 6 7 5 9 10 8 8 9 11 Output 6
6
0
/* https://blog.csdn.net/litble/article/details/83118814 图论神啊,好题好题好题!!! i 向 pi 连条边,构成的图简化以后就都是 “i 向 ai 连条边” 这个图了!!! 注意:是由一个环和若干条指向环的链组成的基环内向树,至于为什么是链,详见样例4 现在手上只有 a,那么就**倒推**有多少种可能图吧 */ #include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) using namespace std; const int mod = 1e9 + 7, N = 1e5 + 10; typedef long long ll; int n, a[N], deg[N], vis[N], cir[N], L[N], sum[N]; ll ans, f[N]; void calc(int x) { int now = 0, fr = 0, ed = 0, l = 0; // fr: 第一个脚的位置 ed: 上一个脚的位置 l: 第一个脚的长度 now: 当前节点是从 x 开始走环走到的第几个点 while (cir[x]) { ++now, cir[x] = 0; if (L[x]) { if (!fr) ed = fr = now, l = L[x]; else { int tmp = (L[x] < now - ed) + (L[x] <= now - ed); ans = 1ll * ans * tmp % mod, ed = now; } } x = a[x]; } if (!fr) ++sum[now]; else { int tmp = (l < now - ed + fr) + (l <= now - ed + fr); ans = 1ll * ans * tmp % mod; } } void solve() { rep(i, 1, n) { if (deg[i]) continue; int x = i, len = 0; while (!cir[x]) ++len, x = a[x]; L[x] = len; } ans = 1; rep(i, 1, n) if (cir[i]) calc(i); rep(i, 1, n) { f[0] = 1; rep(j, 1, sum[i]) { if (i > 1 && (i & 1)) f[j] = f[j - 1] * 2 % mod; else f[j] = f[j - 1]; if (j > 1) f[j] = (f[j] + f[j - 2] * (j - 1) % mod * i % mod) % mod; } ans = 1ll * ans * f[sum[i]] % mod; } } int main() { cin >> n; rep(i, 1, n) scanf("%d", &a[i]), ++deg[a[i]]; rep(i, 1, n) { if (vis[i]) continue; int x = i; while (!vis[x]) vis[x] = i, x = a[x]; if (vis[x] != i) continue; while (!cir[x]) cir[x] = 1, x = a[x]; } rep(i, 1, n) if ((!cir[i] && deg[i] > 1) || (cir[i] && deg[i] > 2)) return puts("0"), 0; solve(); printf("%lld\n", ans); return 0; }
CPP