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
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> #define LL long long using namespace std; int N, M, A, B; int main() { scanf("%d%d%d%d", &N, &M, &A, &B); if (!(N % A) && !(M % B)) puts("No"); else { puts("Yes"); for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { if (i % A || j % B) printf("%d ", -1000); else printf("%d ", 1000 * (A * B - 1) - 1); } puts(""); } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; ll INF = 1e9 + 7; int main() { int H, W, h, w; cin >> H >> W >> h >> w; if(H % h == 0 && W % w == 0) { cout << "No" << endl; return 0; } cout << "Yes" << endl; for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(i % h == 0 && j % w == 0) cout << -(h*w-1) * 1000 -1 << " "; else cout << "1000 "; } cout << endl; } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int H = sc.nextInt(); int W = sc.nextInt(); long h = sc.nextLong(); long w = sc.nextLong(); long x = (-1000)*(h*w-1)-1; String[] ans = new String[H]; for(int i=0; i<H; ++i){ ans[i] = ""; } long sum = 0; for(int i=1; i<=H; ++i){ StringBuilder sb = new StringBuilder(); for(int j=1; j<=W; j++){ if(i%h==0 &&j%w==0){ sb.append(x); sb.append(" "); sum+=x; }else{ sb.append(1000); sb.append(" "); sum+=1000; } } sb.deleteCharAt(sb.length()-1); ans[i-1] = sb.toString(); } if(sum<=0) System.out.println("No"); else{ System.out.println("Yes"); for(int i=0; i<H; i++){ System.out.println(ans[i]); } } return; } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): H,W,h,w = LI() if W%w != 0: s = [0]+[100000]*(w-1) for i in range(W-w+1): s.append(s[-w]-1) a = [s[i]-s[i-1] for i in range(1,W+1)] print("Yes") for i in range(H): print(*a) elif H%h != 0: H,W,h,w = W,H,w,h s = [0]+[100000]*(w-1) for i in range(W-w+1): s.append(s[-w]-1) a = [s[i]-s[i-1] for i in range(1,W+1)] print("Yes") ans = [[a[i]]*H for i in range(W)] for i in range(W): print(*ans[i]) else: print("No") return #Solve if __name__ == "__main__": solve()
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int a[1000][1000],H,W,h,w; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>H>>W>>h>>w; for (int i=1;i<=H;i++) for (int j=1;j<=W;j++) if (i%h==0 && j%w==0) a[i][j]=-696*(h*w-1)-1; else a[i][j]=696; int sum=0; for (int i=1;i<=H;i++) for (int j=1;j<=W;j++) sum+=a[i][j]; if (sum<=0) { cout<<"No"; return 0; } cout<<"Yes"<<endl; for (int i=1;i<=H;i++) { for (int j=1;j<=W;j++) cout<<a[i][j]<<" "; cout<<endl; } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<stdio.h> int main(){ int H,W,h,w; scanf("%d%d%d%d",&H,&W,&h,&w); if(H%h||W%w){ puts("Yes"); for(int i=1;i<=H;++i,puts("")) for(int j=1;j<=W;++j) if(i%h||j%w) printf("1000 "); else printf("%d ", -1000*(h*w-1)-1); }else puts("No"); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,a) for(int i=0;i<(a);i++) const ll MOD=1000000007; int A[505][505]; int main(){ int H,W,h,w; cin>>H>>W>>h>>w; if(H%h==0&&W%w==0){ cout<<"No\n"; return 0; } ll a=(1e9-1)/(h*w-1); rep(i,H) rep(j,W){ if(i%h==h-1&&j%w==w-1) A[i][j]=-(a*(h*w-1)+1); else A[i][j]=a; } cout<<"Yes\n"; rep(i,H){ rep(j,W) cout<<A[i][j]<<" "; cout<<endl; } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <map> #include <set> #include <stdio.h> #include <cstring> using namespace std; const int mod = 1e9 + 7; int a, b, c, d; int main() { cin >> a >> b >> c >> d; if (a%c == 0 && b%d == 0) { cout << "No" << endl; } else { cout << "Yes" << endl; for (int i = 1; i <= a; ++i) { for (int j = 1; j <= b; ++j) { if (i%c == 0 && j%d == 0) { cout << 4000 * (c*d - 1)*-1 - 1 << " "; } else { cout << "4000 "; } } cout << endl; } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <cstdio> int H, W, h, w; int main() { scanf("%d%d%d%d", &H, &W, &h, &w); if(H%h + W%w == 0) return !puts("No"); puts("Yes"); for(int i=0; i<H; i++, puts("")) for(int j=0; j<W; j++) if(H%h) printf("%d ", i%h ? -1000 : 1000*(h-1)-1); else printf("%d ", j%w ? -1000 : 1000*(w-1)-1); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<cstdio> const int MAXN=505; int H,W,h,w; int arr[MAXN][MAXN]; int main() { scanf("%d%d%d%d",&H,&W,&h,&w); if(h==1&&w==1) { puts("No"); return 0; } long long sum=0; int k=999999999/(h*w-1); { sum=0; for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) { if(i%h==0&&j%w==0) arr[i][j]=-k*(h*w-1)-1; else arr[i][j]=k; sum+=arr[i][j]; } if(sum>0) { puts("Yes"); for(int i=1;i<=H;i++) { for(int j=1;j<=W;j++) printf("%d ",arr[i][j]); puts(""); } return 0; } } puts("No"); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H, W, sh, sw = map(int, input().split()) if H%sh == 0 and W%sw == 0: print("No") else: print("Yes") state = [[1]*W for _ in range(H)] INF = 10**9 for h in range(H): for w in range(W): if w%sw == sw-1 and h%sh == sh-1: state[h][w] = -INF elif w%sw == 0 and h%sh == 0: state[h][w] = INF - (sh*sw-1) for h in range(H): print(" ".join([str(a) for a in state[h]]))
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#!/usr/bin/env python3 H, W, h, w = map(int, input().split()) if W % w == 0 and H % h == 0: print('No') else: swapped = False if W % w == 0: H, W, h, w, swapped = W, H, w, h, True a = [ [ 0 for _ in range(W) ] for _ in range(H) ] for y in range(H): for x in range(0, W, w): a[y][x] = W for x in range(w - 1, W, w): a[y][x] = - W - 1 if swapped: H, W, h, w = W, H, w, h b = a a = [ [ 0 for _ in range(W) ] for _ in range(H) ] for y in range(H): for x in range(W): a[y][x] = b[x][y] print('Yes') for y in range(H): print(*a[y])
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H,W,h,w=[int(i) for i in input().split()] if H%h==0 and W%w==0: print("No") elif W%w!=0: print("Yes") for y in range(H): for x in range(W): if x!=0: print(" ",end="") if (x%w)!=(w-1): print(100000,end="") else: print(-100000*(w-1)-1,end="") print() else: print("Yes") for y in range(H): for x in range(W): if x!=0: print(" ",end="") if (y%h)!=(h-1): print(100000,end="") else: print(-100000*(h-1)-1,end="") print()
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H,W,h,w = map(int,input().split()) if not (H%h or W%w): print("No") exit() if H%h: flg = "tate" n = H r = h else: flg = "yoko" n = W r = w ans = [] if r%2 == 0: s = r//2 for i in range(n): if (i//s)%2 == 0: ans.append(2000) else: ans.append(-2001) else: for i in range(n): if (i%r) < r//2+1: ans.append(2000) else: ans.append(-(int(2000/(r//2)*(r//2+1)))-1) print("Yes") if flg == "tate": for i in range(H): print(*([ans[i]]*W)) else: for i in range(H): print(*ans)
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int main() { int H, W, h, w; cin >> H >> W >> h >> w; int nrlin = H / h; int nrcol = W / w; if (H % h == 0 && W % w == 0) { cout << "No\n"; return 0; } else cout << "Yes\n"; for (int i(1); i <= H; i++) for (int j(1); j <= W; j++) cout << (i % h == 0 && j % w == 0 ? -(h * w - 1) * 1000 - 1 : 1000) << " \n"[j == W]; return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import sys input = sys.stdin.readline def main(): H,W,h,w = map(int,input().split()) if H*W-(H//h)*(W//w)*w*h == 0: print("No") else: if W%w != 0: ans = [0 for _ in range(W)] co = [0 for _ in range(W+1)] for i in range(1,w): co[i] = (W-i)//w+1 for i in range(w,W+1): co[i] = co[i-w]-1 for i in range(W): ans[i] = co[i+1]-co[i] print("Yes") for i in range(H): print(*ans) else: ans = [0 for _ in range(H)] co = [0 for _ in range(H+1)] for i in range(1,h): co[i] = (H-i)//h+1 for i in range(h,H+1): co[i] = co[i-h]-1 for i in range(H): ans[i] = co[i+1]-co[i] print("Yes") for i in range(H): print(*list(ans[i] for _ in range(W))) if __name__ == "__main__": main()
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H,W,h,w=map(int,input().split()) if H%h==0 and W%w==0: print("No") else: print("Yes") for i in range(H): print(' '.join("-1000" if i%h or j%w else str(1000*(h*w-1)-1) for j in range(W)))
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<stdio.h> int main(){int H,W,h,w,i=1,j;scanf("%d%d%d%d",&H,&W,&h,&w);if(H%h||W%w)for(puts("Yes");i<=H;++i,puts(""))for(j=0;j<W;++j,printf("%d ",(i%h||j%w)?999:-999*(h*w-1)-1));else puts("No");return 0;}
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.*; import java.util.Arrays; import java.util.StringJoiner; import java.util.StringTokenizer; public class Main { static int H, W, h, w; public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); H = sc.nextInt(); W = sc.nextInt(); h = sc.nextInt(); w = sc.nextInt(); int[][] ans = solve(); if( ans != null ) { PrintWriter pw = new PrintWriter(System.out); pw.println("Yes"); for (int i = 0; i < H; i++) { StringJoiner sj = new StringJoiner(" "); for (int j = 0; j < W; j++) { sj.add( String.valueOf(ans[i][j]) ); } pw.println(sj.toString()); } pw.flush(); } else { System.out.println("No"); } } static int[][] solve() { // 負できっちり被覆されるのでどうにもならない if( H % h == 0 && W % w == 0 ) return null; // 倍数ではない行列毎に同じことをしても問題ない // aaabaaaba のように構成するとして aaab = -1となるように置く // aが十分に大きいなら全体として正になる int[][] ans = new int[H][W]; if( H % h == 0 ) { int a = 1000; int b = -(1000*(w-1) + 1); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if( j % w == w-1 ) { ans[i][j] = b; } else { ans[i][j] = a; } } } } else { int a = 1000; int b = -(1000*(h-1) + 1); for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) { if( j % h == h -1 ) { ans[j][i] = b; } else { ans[j][i] = a; } } } } // int sum = 0; // for (int i = 0; i < H; i++) { // for (int j = 0; j < W; j++) { // sum += ans[i][j]; // } // } // debug(sum); return ans; } @SuppressWarnings("unused") static class FastScanner { private BufferedReader reader; private StringTokenizer tokenizer; FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } long nextLong() { return Long.parseLong(next()); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArray(int n, int delta) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt() + delta; return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } static void writeLines(int[] as) { PrintWriter pw = new PrintWriter(System.out); for (int a : as) pw.println(a); pw.flush(); } static void writeLines(long[] as) { PrintWriter pw = new PrintWriter(System.out); for (long a : as) pw.println(a); pw.flush(); } static void writeSingleLine(int[] as) { PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < as.length; i++) { if (i != 0) pw.print(" "); pw.print(i); } pw.println(); pw.flush(); } static int max(int... as) { int max = Integer.MIN_VALUE; for (int a : as) max = Math.max(a, max); return max; } static int min(int... as) { int min = Integer.MAX_VALUE; for (int a : as) min = Math.min(a, min); return min; } static void debug(Object... args) { StringJoiner j = new StringJoiner(" "); for (Object arg : args) { if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg)); else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg)); else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg)); else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg)); else j.add(arg == null ? "null" : arg.toString()); } System.err.println(j.toString()); } static void printSingleLine(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) System.out.print(" "); System.out.print(array[i]); } System.out.println(); } static int lowerBound(int[] array, int value) { int low = 0, high = array.length, mid; while (low < high) { mid = ((high - low) >>> 1) + low; if (array[mid] < value) low = mid + 1; else high = mid; } return low; } static int upperBound(int[] array, int value) { int low = 0, high = array.length, mid; while (low < high) { mid = ((high - low) >>> 1) + low; if (array[mid] <= value) low = mid + 1; else high = mid; } return low; } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> using namespace std; int main() { int H, W, h, w; cin >> H >> W >> h >> w; if(H%h<1 && W%w<1) cout << "No" << endl; else{ cout << "Yes" << endl; int T=((H/h)*(W/w))/(W*(H%h)+H*(W%w)-(H%h)*(W%w))+1; for(int i=1; i<=H; ++i){ for(int j=1; j<=W; ++j){ if(i%h<1 && j%w<1) cout << -T*(h*w-1)-1; else cout << T; if(j<W) cout << ' '; else cout << endl; } } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> #define ll long long using namespace std; ll H, W, h, w; int main() { cin >> H >> W >> h >> w; if (H % h == 0 && W % w == 0) return printf("No"), 0; puts("Yes"); if (H % h != 0) { for (int i = 0; i < H; i ++) { for (int j = 0; j < W; j ++) if (i % h == 0) printf("%lld ", 1000 * h - 1001); else printf("-1000 "); puts(""); } } else { for (int i = 0; i < H; i ++) { for (int j = 0; j < W; j ++) if (j % w == 0) printf("%lld ", 1000 * w - 1001); else printf("-1000 "); puts(""); } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H, W, h, w = map(int, input().split()) if H % h == 0 and W % w == 0: print("No") exit(0) blocks = (H//h) * (W//w) left = H * W - blocks * h * w offset = blocks // left + 1 v = [offset, -offset * (h * w - 1) - 1] print("Yes") for i in range(H): a = [] for j in range(W): a.append(v[1 if i % h == h-1 and j % w == w-1 else 0]) print(" ".join(list(map(str, a))))
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int H, W, h, w, a[505][505]; long long ret; int main () { scanf("%d%d%d%d", &H, &W, &h, &w); for (int i = 1; i <= H; i += h) for (int j = 1; j <= W; j += w) { a[i][j] = 1e9 - 1; ret += 1e9 - 1; } for (int i = h; i <= H; i += h) for (int j = w; j <= W; j += w) { a[i][j] = -1e9; ret -= 1e9; } if (ret <= 0) return puts("No"), 0; puts("Yes"); for (int i = 1; i <= H; ++i) for (int j = 1; j <= W; ++j) printf("%d%c", a[i][j], j == W ? '\n' : ' '); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <cmath> #include <cstring> #include <cstdio> #include <algorithm> #define ll long long using namespace std; const int x=1000; int W,H,w,h,sum; int a[510][510]; int main(){ scanf("%d%d%d%d",&W,&H,&w,&h); for (int i=1;i<=W;++i) for (int j=1;j<=H;++j) a[i][j]=x; sum=W*H*x; for (int i=w;i<=W;i+=w) for (int j=h;j<=H;j+=h){ a[i][j]=-w*h*x+x-1; sum=sum-w*h*x-1; } if (sum<=0) puts("No"); else{ puts("Yes"); for (int i=1;i<=W;++i){ for (int j=1;j<=H;++j) printf("%d ",a[i][j]); puts(""); } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int main(){ int n,m,h,w; scanf("%d%d%d%d",&n,&m,&h,&w); int k=1000000000/h/w; long long ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(i%h==0&&j%w==0) ans-=(h*w-1)*k+1; else ans+=k; if(ans<=0) puts("No"); else{ puts("Yes"); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) if(i%h==0&&j%w==0) printf("%d ",-(h*w-1)*k-1); else printf("%d ",k); putchar('\n'); } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<cstdio> int main() { int n,m,h,w; scanf("%d%d%d%d",&n,&m,&h,&w); if( n%h==0&&m%w==0 ) return puts("No"),0; puts("Yes"); long long c=(n/h)*(m/w)/((n%h)*m+(m%w)*n-(n%h)*(m%w))+1; long long s=(h*w-1)*c+1,t=0;int i,j; for(i=1;i<=n;i++,putchar(10)) for(j=1;j<=m;j++) printf("%lld ",i%h==0&&j%w==0?-s:c),t+=i%h==0&&j%w==0?-s:c; if(t<=0)return 1; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H,W,h,w=map(int,input().split()) if H%h==0 and W%w==0: print("No") else: c=10**9 ans=[[(c-1)//(h*w-1) for i in range(W)] for j in range(H)] for i in range(H): for j in range(W): if (i+1)%h==0 and (j+1)%w==0: ans[i][j]=-c if sum(ans[i][j] for i in range(H) for j in range(W))>0: print("Yes") for i in range(H): print(*ans[i]) else: print("No")
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H, W, h, w = map(int, input().split()) a = [[0] * W for i in range(H)] INF = 10**9 for i in range(H): for j in range(W): if i % h == h - 1 and j % w == w - 1: a[i][j] = -INF elif i % h == 0 and j % w == 0: a[i][j] = INF - 1 if sum([sum(line) for line in a]) > 0: print("Yes") for i in range(H): print(*a[i]) else: print("No")
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H, W, a, b = map(int, input().split()) if H%a or W%b: print('Yes') k = (H//a) * (W//b) x = k+2 ansss = [[0]*(W) for _ in range(H)] for i in range(H//a): for j in range(W//b): ansss[(i+1)*a-1][(j+1)*b-1] = -x for i in range(-(-H//a)): for j in range(-(-W//b)): ansss[i*a][j*b] = x-1 print('\n'.join([' '.join(map(str, anss)) for anss in ansss])) else: print('No')
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include "bits/stdc++.h" using namespace std; int main() { int H, W, h, w; cin >> H >> W >> h >> w; if (H % h == 0 && W % w == 0) { cout << "No" << endl; return 0; } cout << "Yes" << endl; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (i % h == 0 && j % w == 0) cout << -1 * h * w * 250 + 249; else cout << 250; if (j == W) cout << endl; else cout << " "; } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<stdio.h> int main(){ int H, W, h, w; scanf("%d%d%d%d", &H,&W,&h,&w); if(H%h||W%w){ puts("Yes"); for(int i=0; i<H; ++i,puts("")) for(int j=0; j<W; ++j) if((i+1)%h || (j+1)%w) printf("1000 "); else printf("%d ", -1000*(h*w-1)-1); }else puts("No"); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int H,W,h,w; int main() { scanf("%d%d",&H,&W); scanf("%d%d",&h,&w); if(!(W % w) && !(H % h)) { printf("No\n"); return 0; } printf("Yes\n"); int base,exp; base = (H / h) * (W / w) + 1; exp = -1 * (base * (h * w - 1) + 1); for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(i % h == 0 && j % w == 0)printf("%d",exp); else printf("%d",base); if(j != W)printf(" "); } printf("\n"); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int main(){ int H,W,h,w; cin>>H>>W>>h>>w; if(H%h==0 && W%w==0) cout<<"No"<<endl; else{ cout<<"Yes"<<endl; vector<vector<int>>A(H,vector<int>(W)); for(int i=0;i<H;i++) for(int j=0;j<W;j++) if(i%h==h-1 && j%w==w-1) A[i][j]=-4000*(h*w-1)-1; else A[i][j]=4000; for(int i=0;i<H;i++){ for(int j=0;j<W-1;j++) cout<<A[i][j]<<" "; cout<<A[i][W-1]<<endl; } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; int main() { int A,B,a,b; cin>>A>>B>>a>>b; if(A%a==0&&B%b==0) return puts("No"),0; int k=999999999/(a*b-1); puts("Yes"); for(int i=1;i<=A;i++) { for(int j=1;j<=B;j++) { if(i%a==0&&j%b==0) printf("%d ",-(a*b-1)*k-1); else printf("%d ",k); } puts(""); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
def get_1d(H, h): ret = [0] * (H + 1) for s in range(h): for x, i in enumerate(range(s, H, h)): ret[i] = -x for x, i in enumerate(range(H, 0, -h)): ret[i] = x + 1 return [x1 - x0 for x0, x1 in zip(ret, ret[1:])] def solve(H, W, h, w): if H % h == 0 and W % w == 0: return False ans = [] if H % h > 0: col = get_1d(H, h) ans.extend([x] * W for x in col) else: row = get_1d(W, w) ans.extend([row] * H) return ans H, W, h, w = map(int, input().split()) ans = solve(H, W, h, w) if ans == False: print('No') else: print('Yes') for row in ans: print(*row)
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import sys input = sys.stdin.readline sys.setrecursionlimit(pow(10, 6)) def main(): H, W, h, w = map(int, input().split()) if H % h == 0 and W % w == 0: print("No") elif H % h == 0: print("Yes") ans = [[0 for _ in range(W)] for _ in range(H)] for i in range(H): for j in range(0, W, w): ans[i][j] = 99999 if j + w - 1 < W: ans[i][j + w - 1] = -100000 for a in ans: print(*a) else: print("Yes") ans = [[0 for _ in range(W)] for _ in range(H)] for i in range(W): for j in range(0, H, h): ans[j][i] = 99999 if j + h - 1 < H: ans[j + h - 1][i] = -100000 for a in ans: print(*a) if __name__ == '__main__': main()
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int H = in.nextInt(); int W = in.nextInt(); int h = in.nextInt(); int w = in.nextInt(); if (H % h == 0 && W % w == 0) { out.println("No"); } else { out.println("Yes"); for (int r = 0; r < H; ++r) { for (int c = 0; c < W; ++c) { if (r % h == 0 && c % w == 0) { out.print((int) 1e9 - 1); } else if (r % h == h - 1 && c % w == w - 1) { out.print((int) -1e9); } else { out.print(0); } out.print(" "); } out.println(); } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 mod = 10**9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): H,W,h,w = LI() f = False if H % h == 0 and W % w == 0: return 'No' if W % w == 0: H,W,h,w = W,H,w,h f = True p = 10**6*(w-1) - 1 m = -(10**6) a = [] for i in range(W): if i % w == 0: a.append(p) else: a.append(m) aa = [a] * H r = aa if f: r = [[aa[i][j] for i in range(H)] for j in range(W)] return 'Yes\n' + '\n'.join(' '.join(map(str,t)) for t in r) print(main())
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int H, W, HS, WS; #define BIG 4000 int main() { cin >> H >> W >> HS >> WS; if(H % HS == 0 && W % WS == 0) cout << "No" << endl; else { cout << "Yes" << endl; for(int i = 0; i < H; ++i) { for(int j = 0; j < W; ++j) { cout << (j == 0 ? "" : " ") << (i % HS == HS - 1 && j % WS == WS - 1 ? BIG - 1 - BIG * HS * WS : BIG); } cout << endl; } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> const int N=505; int H,W,h,w,i,j,a[N][N],s; int main(){ scanf("%d%d%d%d",&H,&W,&h,&w); for(i=0;i<H;++i)for(j=0;j<W;++j)s+=a[i][j]=i%h==h-1 && j%w==w-1?-h*w-(1<<20): (i%h==0 && j%w==0?h*w+(1<<20)-1:0); if(s<0){ puts("No"); return 0; } puts("Yes"); for(i=0;i<H;++i)for(j=0;j<W;++j)printf("%d%c",a[i][j],"\n "[j<W-1]); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
h, w, h0, w0 = map(int, input().split()) if h0 == w0 == 1: print("No") exit() x, y = h // h0, w // w0 a1cnt = h0 * w0 - 1 a1sum, a2sum = h * w - x * y, x * y a1 = ((pow(10, 9) - 1) // a1cnt) a2 = -(a1 * a1cnt + 1) asum = a1 * a1sum + a2 * a2sum print("Yes" if asum > 0 else "No") if asum > 0: for i in range(1, h + 1): a = [0] * w for j in range(1, w + 1): a[j - 1] = a2 if i % h0 == j % w0 == 0 else a1 print(*a)
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int n,m,h,w; int main() { scanf("%d%d%d%d",&n,&m,&h,&w); if(n%h||m%w) { puts("Yes"); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(i%h||j%w)printf("1000 ");else printf("%d ",-1000*(h*w-1)-1); } puts(""); } } else puts("No"); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
# coding: utf-8 import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time sys.setrecursionlimit(10 ** 7) INF = 10 ** 20 MOD = 10 ** 9 + 7 def II(): return int(input()) def ILI(): return list(map(int, input().split())) def IAI(LINE): return [ILI() for __ in range(LINE)] def IDI(): return {key: value for key, value in ILI()} def read(): H, W, h, w = ILI() return H, W, h, w def solve(H, W, h, w): if H % h == 0 and W % w == 0: return False, None else: matrix = [[10 ** 3] * W for __ in range(H)] val = - (h * w * 1000) + 999 for _h in range(h - 1, H, h): for _w in range(w - 1, W, w): matrix[_h][_w] = val return True, matrix def main(): params = read() ans_bool, matrix = solve(*params) if ans_bool: print("Yes") for row in matrix: print(" ".join(map(str, row))) else: print("No") if __name__ == "__main__": main()
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import sys def MI(): return map(int,sys.stdin.readline().rstrip().split()) H,W,h,w = MI() q1,r1 = H//h,H % h q2,r2 = W//w,W % w if r1 == 0 and r2 == 0: print('No') else: print('Yes') if r2 != 0: A = [] for _ in range(q2): A += [2*10**6]*(w-1)+[-(2*10**6)*(w-1)-1] A += [2*10**6]*r2 for _ in range(H): print(*A) else: for i in range(1,H+1): if i % h != 0: print(*([2*10**6]*W)) else: print(*([-(2*10**6)*(h-1)-1]*W))
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; #define rep(i,l,r) for(int i=l;i<=r;++i) const int K=500+5; int main() { //freopen("1.in","r",stdin); int H,W,h,w; cin>>H>>W>>h>>w; if(H%h==0&&W%w==0) { puts("No"); exit(0); } puts("Yes"); int v=-(((h*w)-1)*K+1); rep(i,1,H) { rep(j,1,W) printf("%d ",(i%h==0&&j%w==0)?v:K); puts(""); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<stdio.h> int main(){ int H, W, h, w; scanf("%d%d%d%d", &H,&W,&h,&w); if(H%h==0 && W%w==0){ puts("No"); return 0; } puts("Yes"); for(int i=0; i<H; ++i,puts("")) for(int j=0; j<W; ++j) if(i%h==h-1 && j%w==w-1) printf("-999999 "); else if(i%h==0 && j%w==0) printf("999998 "); else printf("0 "); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int H, W, h, w; int main() { scanf("%d %d %d %d", &H, &W, &h, &w); if(H % h == 0 && W % w == 0) { printf("No"); return 0; } printf("Yes\n"); int q1 = H / h; int q2 = W / w; int x = (q1 + 1) * (q2 + 1); int y = h*w*x - x - 1; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { if(i % h == 0 && j % w == 0) printf("%d ", y); else printf("%d ", -x); } printf("\n"); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> using namespace std; int H,W,h,w; int main() { cin >> H >> W >> h >> w; if (H%h != 0 || W%w != 0) { cout << "Yes\n"; int k=(H/h)*(W/w)+1; for (int i=0; i<H; i++) { for (int j=0; j<W; j++) { if (i%h == h-1 && j%w == w-1) cout << -k-1 << " "; else if (i%h == 0 && j%w == 0) cout << k << " "; else cout << "0 "; } cout << "\n"; } } else { cout << "No\n"; } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#!/usr/bin/env python3 import sys try: from typing import Any, Union, List, Tuple, Dict except ImportError: pass def debug(*args): print(*args, file=sys.stderr) def exit(): sys.exit(0) H, W, h, w = map(int, input().split()) yes, no = "Yes", "No" if (H%h == 0) and (W%w == 0): print(no) exit() print(yes) unit = 1000 if (W%w != 0): for i in range(H): # l_ = [str(unit)] * (w-1) # l_ += [str(-unit*(w-1)-1)] # l = l_ * (W//w) # l += [str(unit)] * (W%w) l = [] for j in range(W): if j % w == w-1: l.append(str(-unit*(w-1)-1)) else: l.append(str(unit)) print(" ".join(l)) exit() if (H%h != 0): for i in range(H): if i%h == h-1: l = [str(-unit*(h-1)-1)] * W else: l = [str(unit)] * W print(" ".join(l)) exit() raise Exception("hogeeee")
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; #define FOR(i,x,y) for(int i=(int)x; i<(int)y; ++i) int main(){ int H, W, h, w; cin >> H >> W >> h >> w; if(H%h==0 && W%w==0){ printf("No\n"); }else{ printf("Yes\n"); FOR(i,0,H){ FOR(j,0,W){ if(i%h==h-1 && j%w==w-1) printf("-999999 "); else if(i%h==0 && j%w==0) printf("999998 "); else printf("0 "); } printf("\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<bits/stdc++.h> using namespace std; #define mo 1000000007ll #define N 100010 #define int long long int n,d[N],a[N],vi[N],c[N],le[N],s[N],f[N],ans; void dfs(int x){ int v=0,y=0,z=0,p=0; while(c[x]){ v++; c[x]=0; if(le[x]){ if(!y){ z=y=v; p=le[x]; } else{ ans=ans*((le[x]<v-z)+(le[x]<=v-z))%mo; z=v; } } x=a[x]; } if(!y)s[v]++; else ans=ans*((p<v-z+y)+(p<=v-z+y))%mo; } void dp(){ for(int i=1;i<=n;i++){ if(d[i])continue; int x=i,l=0; while(!c[x]){ x=a[x]; l++; } le[x]=l; } ans=1; for(int i=1;i<=n;i++) if(c[i])dfs(i); for(int i=1;i<=n;i++) if(s[i]){ f[0]=1; for(int j=1;j<=s[i];j++){ if(i>1&&(i&1)) f[j]=f[j-1]*2ll%mo; else f[j]=f[j-1]; if(j>1)f[j]=(f[j]+f[j-2]*(j-1)%mo*i%mo)%mo; } ans=ans*f[s[i]]%mo; } } signed main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; d[a[i]]++; } for(int i=1;i<=n;i++){ if(vi[i])continue; int x=i; while(!vi[x]){ vi[x]=i; x=a[x]; } if(vi[x]!=i)continue; while(!c[x]){ c[x]=1; x=a[x]; } } for(int i=1;i<=n;i++) if((c[i]&&d[i]>2)||(!c[i]&&d[i]>1)){ puts("0"); return 0; } dp(); 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 <cstdio> typedef long long LL; const int Mod = 1000000007; const int MN = 100005; int N, p[MN], c[MN], d[MN], k[MN], g[MN], Ans; int q[MN], l, r; 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] > 2) return puts("0"), 0; for (int i = 1; i <= N; ++i) if (!d[i]) q[++r] = i; while (l < r) { int u = q[++l], x = p[u]; k[x] = k[u] + 1; if (++c[x] == d[x]) q[++r] = x; } for (int i = 1; i <= N; ++i) if (c[i] > 1) return puts("0"), 0; Ans = 1; for (int i = 1; i <= N; ++i) if (c[i] < d[i]) { q[r = 1] = i; for (int x = p[i]; x != i; x = p[x]) q[++r] = x; int lst = 0; for (int j = 1; j <= r; ++j) if (c[q[j]]) lst = j; for (int j = 1; j <= r; ++j) c[q[j]] = d[q[j]]; if (!lst) { ++g[r]; continue; } lst -= r; for (int j = 1; j <= r; ++j) if (k[q[j]]) { if (k[q[j]] > j - lst) return puts("0"), 0; if (k[q[j]] < j - lst) Ans = 2 * Ans % Mod; lst = j; } } for (int i = 1; i <= N; ++i) if (g[i]) { int x = 1, y = 1 + (i >= 3 && i & 1), z; for (int j = 2; j <= g[i]; ++j) z = ((1 + (i >= 3 && i & 1)) * y + (LL)x * (j - 1) * i) % Mod, x = y, y = z; Ans = (LL)Ans * y % 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> #include<vector> #define N 100100 #define Q 1000000007 using namespace std; bool vis[N]; int a[N],stk[N],cnt,num[N],cc,lf[N]; vector<int> cyc[N],g[N]; int dp[N]; int main(){ int n,ans=1; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),g[a[i]].push_back(i); for(int i=1;i<=n;i++){ if(!vis[i]){ int j; cnt=0; for(j=i;!vis[j];j=a[j]){ vis[j]=true; stk[cnt++]=j; } do{ cyc[cc].push_back(stk[--cnt]); }while(cnt&&stk[cnt]!=j); if(stk[cnt]==j) cc++; else cyc[cc].clear(); } } for(int i=0;i<cc;i++){ bool fx=false; for(int j=0;j<cyc[i].size();j++){ int u=cyc[i][j]; if(g[u].size()>2) ans=0; else if(g[u].size()==2){ fx=true; lf[j]=0; int v=g[u][0]^g[u][1]^(cyc[i][(j+1)%cyc[i].size()]); while(1){ lf[j]++; if(g[v].size()==1) v=g[v][0]; else{ if(g[v].size()>1) ans=0; break; } } } else lf[j]=0; } if(!fx) num[cyc[i].size()]++; else{ for(int j=0;j<cyc[i].size();j++){ if(lf[j]){ for(int k=(j+1)%cyc[i].size();g[cyc[i][k]].size()==1;k=(k+1)%cyc[i].size()){ lf[j]--; } if(lf[j]<=0) ans=(ans+ans)%Q; else if(lf[j]>1) ans=0; } } } } for(int i=1;i<=n;i++){ dp[0]=1; dp[1]=1+(i>1)*(i&1); for(int j=2;j<=num[i];j++){ dp[j]=(dp[1]*dp[j-1]+1LL*(j-1)*i%Q*dp[j-2])%Q; } ans=1LL*ans*dp[num[i]]%Q; } 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 int long long const int N=100005,M=1e9+7; int n,a[N],pre[N],deg[N],cnt[N],f[N],ans=1,vis[N]; signed main(){ scanf("%lld",&n); for (int i=1;i<=n;i++){ scanf("%lld",&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; while (p!=i||!vis[p]){ if (vis[p]){ printf("0\n"); return 0; } vis[p]=1; pre[a[p]]=p; p=a[p]; } } for (int i=1;i<=n;i++) if (!deg[i]){ int p=i,l1=0,l2=0; while (!vis[p])vis[p]=1,p=a[p],l1++; do { l2++; p=pre[p]; }while(deg[p]!=2); if (l1<l2)ans=ans*2%M; 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]=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]=(f[j-2]*(j-1)%M*i+f[j-1]*mul)%M; ans=ans*f[cnt[i]]%M; } 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 <bits/stdc++.h> const int MAXN = 100010; const int mod = 1000000007; typedef long long LL; void bye() { std::cout << 0 << std::endl; exit(0); } void reduce(int & x) { x += x >> 31 & mod; } int mul(int a, int b) { return (LL) a * b % mod; } int pow(int a, int b, int res = 1) { for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a); return res; } int A[MAXN], n, ind[MAXN], vis[MAXN]; int fac[MAXN], inv[MAXN], cnt[MAXN]; int C(int a, int b) { return (LL) fac[a] * inv[b] % mod * inv[a - b] % mod; } std::vector<int> in[MAXN]; int ans = 1; int main() { std::ios_base::sync_with_stdio(false), std::cin.tie(0); fac[0] = inv[0] = fac[1] = inv[1] = 1; for (int i = 2; i != MAXN; ++i) { fac[i] = mul(fac[i - 1], i); inv[i] = mul(inv[mod % i], mod - mod / i); } for (int i = 2; i != MAXN; ++i) inv[i] = mul(inv[i - 1], inv[i]); std::cin >> n; for (int i = 1; i <= n; ++i) std::cin >> A[i], ++ind[A[i]], in[A[i]].push_back(i); for (int i = 1; i <= n; ++i) if (ind[i] > 2) bye(); for (int i = 1; i <= n; ++i) if (!vis[i] && !ind[i]) { static int li[MAXN], bak, cir[MAXN]; bak = 0; auto pre = [] (int x) { return x == 1 ? bak : x - 1; }; int now = i; while (!vis[now]) { vis[now] = true; li[++bak] = now; now = A[now]; } int at = std::find(li + 1, li + 1 + bak, now) - li; bak -= at - 1; for (int i = 1; i <= bak; ++i) li[i] = li[i + at - 1]; for (int i = 1; i <= bak; ++i) { int u = li[i]; cir[i] = 0; for (auto t : in[u]) if (t != li[pre(i)]) { int now = t; while (true) { ++cir[i]; if (in[now].size() > 1) bye(); if (in[now].empty()) break; now = in[now][0]; } } } for (int i = 1; i <= bak; ++i) if (cir[i]) { int dis = 1, now = pre(i); while (!cir[now]) now = pre(now), ++dis; if (dis < cir[i]) bye(); ans = mul(ans, 1 + (dis > cir[i])); } } for (int i = 1; i <= n; ++i) if (!vis[i]) { int now = A[i], l = 1; vis[i] = true; while (now != i) ++l, vis[now] = true, now = A[now]; ++cnt[l]; } for (int i = 1; i <= n; ++i) { int t = 0; for (int j = 0; j <= cnt[i]; j += 2) { int t2 = (LL) C(cnt[i], j) * C(j, j / 2) % mod * fac[j / 2] % mod; t2 = pow(mul(i, mod + 1 >> 1), j / 2, t2); if (i > 1 && i % 2 == 1) t2 = pow(2, cnt[i] - j, t2); reduce(t += t2 - mod); } ans = mul(ans, t); } std::cout << ans << std::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; #define RI register int int read() { int q=0;char ch=' '; while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') q=q*10+ch-'0',ch=getchar(); return q; } const int mod=1e9+7,N=100005; int n,ans; int a[N],du[N],cir[N],vis[N],footL[N],sum[N],f[N]; int qm(int x) {return x>=mod?x-mod:x;} void workcir(int x) { int now=0,fr=0,ed=0,frL=0; //fr:第一个有脚的位置,ed:上一个找到的有脚的位置 //frL:第一个脚的长度,now:当前节点是从x开始走环走到的第几个点 while(cir[x]) { ++now,cir[x]=0; if(footL[x]) { if(!fr) ed=fr=now,frL=footL[x]; else {//塞脚 int kl=(footL[x]<now-ed)+(footL[x]<=now-ed); ans=1LL*ans*kl%mod,ed=now; } } x=a[x]; } if(!fr) ++sum[now];//是简单环 else {//考虑第一个脚 int kl=(frL<now-ed+fr)+(frL<=now-ed+fr); ans=1LL*ans*kl%mod; } } void work() { for(RI i=1;i<=n;++i) { if(du[i]) continue; int x=i,len=0;while(!cir[x]) x=a[x],++len; footL[x]=len;//算挂在每个点上的脚长 } ans=1; for(RI i=1;i<=n;++i) if(cir[i]) workcir(i); for(RI i=1;i<=n;++i) {//对每一种长度的简单环做DP if(!sum[i]) continue; f[0]=1; for(RI j=1;j<=sum[i];++j) { if(i>1&&(i&1)) f[j]=qm(f[j-1]+f[j-1]);//情况1,2 else f[j]=f[j-1];//情况1 if(j>1) f[j]=qm(f[j]+1LL*f[j-2]*(j-1)%mod*i%mod);//情况3 } ans=1LL*ans*f[sum[i]]%mod; } } int main() { n=read(); for(RI i=1;i<=n;++i) a[i]=read(),++du[a[i]]; for(RI 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;//说明i在一个脚上 while(!cir[x]) cir[x]=1,x=a[x];//给环打上是环标记 } for(RI i=1;i<=n;++i)//判无解 if((cir[i]&&du[i]>2)||(!cir[i]&&du[i]>1)) {puts("0");return 0;} work(); 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> using namespace std; const int N=5e5+500,mod=1000000007; int p[N],stack[N],inq[N],vis[N],inring[N],len[N],tmp[N],cnt[N],top,n,rd[N],fac[N],ifac[N],inv[N],pw2[N],ipw2[N]; void dfs(int u) { stack[++top]=u,inq[u]=1,vis[u]=1; int v=p[u]; if(inq[v]) { int t=top;stack[top+1]=0; while(stack[top+1]!=v)inring[stack[top--]]=1; top=t; } if(!vis[v])dfs(v);inq[u]=0; } void getdep(int u) { int dep=0;while(!inring[u])++dep,u=p[u]; len[u]=dep; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",p+i),rd[p[i]]++; for(int i=1;i<=n;i++)if(!vis[i])dfs(i); for(int i=1;i<=n;i++)if(rd[i]>=2+inring[i]){puts("0");return 0;} for(int i=1;i<=n;i++)if(!rd[i])getdep(i); int ans=1; for(int i=1;i<=n;i++) { if(!inring[i])continue; int u=i,tn=0,iscir=!len[u];inring[u]=0;tmp[++tn]=u; while(p[u]!=i)u=p[u],tmp[++tn]=u,iscir&=!len[u],inring[u]=0; if(iscir){++cnt[tn];continue;} int lst=0; for(int j=tn;j>=1;j--)if(len[tmp[j]]){lst=j-tn;break;} for(int j=1;j<=tn;j++) { if(!len[tmp[j]])continue; if(j-len[tmp[j]]<lst){puts("0");return 0;} if(j-len[tmp[j]]>lst)ans=(ans<<1)%mod; lst=j; } } fac[0]=1;for(int i=1;i<=n;i++)fac[i]=1ll*fac[i-1]*i%mod; inv[1]=1;for(int i=2;i<=n;i++)inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod; ifac[0]=1;for(int i=1;i<=n;i++)ifac[i]=1ll*ifac[i-1]*inv[i]%mod; pw2[0]=1;for(int i=1;i<=n;i++)pw2[i]=(pw2[i-1]<<1)%mod; ipw2[0]=1;for(int i=1;i<=n;i++)ipw2[i]=(ipw2[i-1]&1)?((ipw2[i-1]+mod)>>1):(ipw2[i-1]>>1); for(int i=1;i<=n;i++) { if(!cnt[i])continue; int tn=cnt[i];int s=0,pw=1; for(int j=0;j*2<=tn;j++) { int t=1ll*ifac[tn-2*j]*ifac[j]%mod*ipw2[j]%mod*pw%mod; if((i&1)&&i!=1)t=1ll*t*pw2[tn-2*j]%mod; s=(s+t)%mod;pw=1ll*pw*i%mod; } ans=1ll*ans*s%mod*fac[tn]%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 <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> P; template<typename T> inline void chkmin(T &a, const T &b) { a = a < b ? a : b; } template<typename T> inline void chkmax(T &a, const T &b) { a = a > b ? a : b; } const int MAXN = 100005, MOD = 1e9 + 7; int n, arr[MAXN], vis[MAXN], cir[MAXN], inc[MAXN]; int dep[MAXN], cnt[MAXN]; LL fac[MAXN], inv[MAXN]; vector<int> gra[MAXN]; LL modpow(LL a, int b) { LL res = 1; for (; b; b >>= 1) { if (b & 1) res = res * a % MOD; a = a * a % MOD; } return res; } int dfs(int u) { int cnt = 0, d = 0; for (int v : gra[u]) { if (inc[v]) continue; if (cnt++) { puts("0"); exit(0); } d = dfs(v) + 1; } return d; } int main() { scanf("%d", &n); fac[0] = 1; for (int i = 1; i <= n; i++) { scanf("%d", arr + i); gra[arr[i]].push_back(i); fac[i] = fac[i - 1] * i % MOD; } inv[n] = modpow(fac[n], MOD - 2); for (int i = n; i > 0; i--) inv[i - 1] = inv[i] * i % MOD; LL ans = 1; for (int i = 1; i <= n; i++) if (!vis[i]) { int j = i, tot = 0; for (; !vis[j]; j = arr[j]) vis[j] = i; if (vis[j] != i) continue; for (int k = arr[j]; k != j; k = arr[k]) cir[tot++] = k, inc[k] = 1; cir[tot++] = j, inc[j] = 1; memset(dep, 0, sizeof(int) * tot); bool flag = false; for (int k = 0; k < tot; k++) { int t = cir[k]; if (gra[t].size() == 1) continue; dep[k] = dfs(t); flag = true; } if (flag) { for (int k = 0; k < tot; k++) if (dep[k]) { int nxt = 0; for (int j = (k - 1 + tot) % tot;; j = (j - 1 + tot) % tot) { if (dep[j]) break; ++nxt; } if (nxt < dep[k] - 1) { puts("0"); return 0; } if (nxt >= dep[k]) ans = ans * 2 % MOD; } } else ++cnt[tot]; } for (int i = 1; i <= n; i++) if (cnt[i]) { LL pw = 1, t = (LL)i * (MOD + 1) / 2 % MOD, sum = 0; if (i & 1 && i > 1) pw = modpow(2, cnt[i]), t = (LL)i * (MOD + 1) / 8 % MOD; for (int j = 0; 2 * j <= cnt[i]; j++) { sum = (sum + pw * inv[cnt[i] - 2 * j] % MOD * inv[j]) % MOD; pw = pw * t % MOD; } ans = ans * sum % MOD * fac[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> typedef long long i64; const int N=100007,P=1e9+7; int n,fa[N],in[N],q[N],ql=0,qr=0,md[N],ss[N][2],t[N]; i64 pw2[N],fac[N],fiv[N],ans=1; i64 pw(i64 a,i64 n){ i64 v=1; for(;n;n>>=1,a=a*a%P)if(n&1)v=v*a%P; return v; } int main(){ scanf("%d",&n); for(int i=pw2[0]=1;i<=n;++i)pw2[i]=pw2[i-1]*2%P; for(int i=fac[0]=1;i<=n;++i)fac[i]=fac[i-1]*i%P; fiv[n]=pw(fac[n],P-2); for(int i=n;i;--i)fiv[i-1]=fiv[i]*i%P; for(int i=1;i<=n;++i)scanf("%d",fa+i),++in[fa[i]]; for(int i=1;i<=n;++i)if(!in[i])q[++qr]=i; while(ql!=qr){ int w=q[++ql],f=fa[w]; if(md[f])return puts("0"),0; md[f]=md[w]+1; if(!--in[f])q[++qr]=f; } for(int i=1;i<=n;++i)if(in[i]){ int c=0,sp=0; for(int w=i;in[w];in[w]=0,w=fa[w]){ ++c; if(md[w])ss[++sp][0]=md[w],ss[sp][1]=c; } if(sp){ ss[0][1]=ss[sp][1]-c; for(int j=1;j<=sp;++j){ int d=ss[j][1]-ss[j-1][1],d0=ss[j][0]; if(d<d0)return puts("0"),0; if(d>d0)ans=ans*2%P; } }else ++t[c]; } for(int i=1;i<=n;++i)if(t[i]){ i64 s1=1,s2=0; if(i>1&&(i&1)) for(int j=0,c=t[i];j<=c;j+=2){ s2=(s2+s1*pw2[c-j]%P*fiv[j/2])%P; s1=i64(c-j)*(c-j-1)/2%P*s1%P*i%P; } else for(int j=0,c=t[i];j<=c;j+=2){ s2=(s2+s1*fiv[j/2])%P; s1=i64(c-j)*(c-j-1)/2%P*s1%P*i%P; } ans=ans*s2%P; } 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 sz=1e5+7; const int mod=1e9+7; int n; int T; int ans; int cnt; bool flag; int b[sz]; int a[sz],r[sz],rr[sz]; int t[sz],rt[sz]; int qp[sz],_qp[sz]; int inv[sz],fac[sz],_fac[sz]; int del[sz],dep[sz]; queue<int>q; void upd(int &x,int y){ x=x+y>=mod?x+y-mod:x+y; } int qpow(int x,int y){ int ret=1; for(;y;y>>=1,x=1ll*x*x%mod) if(y&1) ret=1ll*x*ret%mod; return ret; } void find(int x){ if(dep[x]) flag=0; cnt++; del[x]=1; if(!del[a[x]]) find(a[x]); b[a[x]]=x; } void init(){ qp[0]=_qp[0]=1; fac[0]=_fac[0]=1; inv[1]=fac[1]=_fac[1]=1; for(int i=2;i<sz;i++){ inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod; fac[i]=1ll*i*fac[i-1]%mod; _fac[i]=1ll*inv[i]*_fac[i-1]%mod; } for(int i=1;i<sz;i++){ qp[i]=2ll*qp[i-1]%mod; _qp[i]=1ll*inv[2]*_qp[i-1]%mod; } } void build(){ for(int i=1;i<=n;i++) if(!r[i]) q.push(i); while(!q.empty()){ int x=q.front(); dep[a[x]]=dep[x]+1; r[a[x]]--; if(!r[a[x]]) q.push(a[x]); del[x]=1; q.pop(); } for(int i=1;i<=n;i++){ if(del[i]==1&&rr[i]>1) { printf("0\n"); exit(0); } if(del[i]==0&&rr[i]>2) { printf("0\n"); exit(0); } } for(int i=1;i<=n;i++) if(!del[i]){ cnt=0; flag=1; find(i); if(flag) t[cnt]++; else{ int x=i; while(!dep[x]) x=b[x]; rt[++T]=x; } } } void dfs(int x,int res){ if(dep[x]){ if(res>0) ans=0; if(res<0) ans=2ll*ans%mod; res=dep[x]; } if(!del[x]) return; del[x]=0; dfs(b[x],res-1); } int main(){ init(); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),r[a[i]]++,rr[a[i]]++; build(); ans=1; for(int i=1;i<=n;i++){ if(!t[i]) continue; int sum=0; for(int j=0;j<=t[i];j+=2){ int tot; tot=1ll*fac[t[i]]*_fac[t[i]-j]%mod*_fac[j/2]%mod*_qp[j/2]%mod*qpow(i,j/2)%mod; if(i%2==1&&i>1) tot=1ll*tot*qp[t[i]-j]%mod; upd(sum,tot); } ans=1ll*ans*sum%mod; } for(int i=1;i<=T;i++) dfs(rt[i],0); 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 N 100000 + 5 #define Mod 1000000007 #define rep(i, l, r) for(int i = l; i <= r; ++i) bool cir[N]; int n, ans, a[N], f[N], p[N], in[N], deg[N], vis[N], sum[N], inv[N], invp[N]; int read(){ char c; int x = 0, f = 1; c = getchar(); while(c > '9' || c < '0'){ if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int Inc(int a, int b){ return (a += b) >= Mod ? a - Mod : a;} int Mul(int a, int b){ return 1ll * a * b % Mod;} int Qpow(int a, int b){ int ans = 1; while(b){ if(b & 1) ans = Mul(ans, a); a = Mul(a, a), b >>= 1; } return ans; } int C(int n, int m){ if(m > n) return 0; return Mul(f[n], Mul(inv[m], inv[n - m])); } void solve(int p){ int x = p, fi = 0, fl = 0, last = 0, llen = 0, now = 0, nlen = 0, cnt = 0; while(cir[x]){ cir[x] = false, cnt++; if(!in[x]){ x = a[x]; continue;} if(!last) last = fi = cnt, llen = fl = in[x]; else{ if(!now) now = cnt, nlen = in[x]; else last = now, llen = nlen, now = cnt, nlen = in[x]; if(cnt - nlen < last) ans = 0; if(cnt - nlen > last) ans = Mul(ans, 2); } x = a[x]; } if(!fi) sum[cnt]++; else{ if(!now) now = fi; int len = cnt - (now - fi); if(len < fl) ans = 0; if(len > fl) ans = Mul(ans, 2); } } int main(){ n = read(); rep(i, 1, n) a[i] = read(), deg[a[i]]++; f[0] = inv[0] = p[0] = invp[0] = 1; rep(i, 1, n){ f[i] = Mul(f[i - 1], i), inv[i] = Qpow(f[i], Mod - 2); p[i] = Mul(p[i - 1], 2), invp[i] = Qpow(p[i], Mod - 2); } rep(i, 1, n) if(!vis[i]){ int x = i; while(!vis[x]) vis[x] = i, x = a[x]; if(vis[x] == i) while(!cir[x]) cir[x] = true, x = a[x]; } rep(i, 1, n) if((cir[i] && deg[i] >= 3) || (!cir[i] && deg[i] >= 2)){ puts("0"); return 0;} rep(i, 1, n) if(!deg[i]){ int x = i, cnt = 0; while(!cir[x]) x = a[x], cnt++; in[x] = cnt; } ans = 1; rep(i, 1, n) if(cir[i]) solve(i); rep(i, 1, n) if(sum[i]){ int res = 0; rep(j, 0, sum[i] / 2){ int cnt = 0; cnt = C(sum[i], 2 * j); cnt = Mul(cnt, C(2 * j, j)); cnt = Mul(cnt, f[j]); cnt = Mul(cnt, invp[j]); cnt = Mul(cnt, Qpow(i, j)); if((i & 1) && i != 1) cnt = Mul(cnt, p[sum[i] - 2 * j]); res = Inc(res, cnt); } ans = Mul(ans, res); } 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> const int mod=1e9+7; using namespace std; int n,du[100010],vis[100010],nxt[100010],L[100010],ans=1,cir_len[100010],f[100010]; bool cir[100010]; void work_cir(int u){ int now=0,pre=0,f_pos=0,f_L=0; while(cir[u]){ cir[u]=false,now++; if(L[u]){ if(!f_pos) f_pos=pre=now,f_L=L[u]; else{ int k=(L[u]<now-pre)+(L[u]<=now-pre); ans=1ll*ans*k%mod,pre=now; } } u=nxt[u]; } if(!f_pos) ++cir_len[now]; else{ int k=(f_L<now-pre+f_pos)+(f_L<=now-pre+f_pos); ans=1ll*ans*k%mod; } } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&nxt[i]),++du[nxt[i]]; for(int i=1;i<=n;i++){ if(vis[i]) continue; int now=i; while(!vis[now]) vis[now]=i,now=nxt[now]; if(vis[now]!=i) continue; while(!cir[now]) cir[now]=true,now=nxt[now]; } for(int i=1;i<=n;i++){ if((cir[i]&&du[i]>2)||(!cir[i]&&du[i]>1)){ puts("0\n"); return 0; } } for(int i=1;i<=n;i++){ if(du[i]) continue; int now=i,len=0; while(!cir[now]) ++len,now=nxt[now]; L[now]=len; } for(int i=1;i<=n;i++) if(cir[i]) work_cir(i); for(int i=1;i<=n;i++){ if(!cir_len[i]) continue; f[0]=1; for(int j=1;j<=cir_len[i];j++){ if(i>1&&(i&1)) f[j]=(2ll*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[cir_len[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<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]; // cout<<"get "<<i<<' ' <<j<<'\n'; if(vis[j]^i) continue; // cout<<"getnew "<<i<<' ' <<j<<'\n'; cntcir++; while(!cir[j]){ // if(cntcir==20) cout<<"getcir "<<j<<' '<<a[j]<<' '<<cntcir<<'\n'; 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; } // cout<<"find "<<i<<' '<<j<<'\n'; 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; // cout<<"finish add "<<cir[j]<<' '<<c<<'\n'; } for(i=1;i<=cntcir;i++){ // cout<<"check link "<<i<<' '<<bst[i]<<'\n'; 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++; // cout<<" end "<<c<<' '<<j<<' '<<cc<<' '<<vis[nd[i][c]]<<'\n'; if(vis[nd[i][c]]>cc){ puts("0");return 0; } // cout<<"survive "<<ans<<"\n"; 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++; // cout<<"circles "<<i<<' '<<j<<'\n'; 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; // cout<<' '<<j<<' '<<tmp<<'\n'; } 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 {{{ #include <iostream> #include <cassert> #include <cstring> #include <cstdlib> #include <cstdio> #include <cctype> #include <cmath> #include <ctime> #include <queue> #include <set> #include <map> #include <stack> #include <string> #include <bitset> #include <vector> #include <complex> #include <algorithm> using namespace std; // }}} // #define {{{ typedef long long ll; typedef double db; typedef pair<int,int> pii; typedef vector<int> vi; #define de(x) cout << #x << "=" << x << endl #define rep(i,a,b) for(int i=a;i<(b);++i) #define per(i,a,b) for(int i=(b)-1;i>=(a);--i) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define fi first #define se second // }}} const int N = 1e5 + 10 , P = 1e9 + 7; int n , a[N] , du[N] , in[N] , sz[N]; int circlecnt[N] , f[N]; int main(){ cin >> n; rep(i,1,n+1) cin >> a[i] , in[a[i]]++; rep(i,1,n+1) du[i] = in[i] , sz[i] = 1; vi q; rep(i,1,n+1) if(!du[i]) q.pb(i); rep(i,0,sz(q)) { int c=q[i]; sz[a[c]]+=sz[c]; if(!--du[a[c]]) q.pb(a[c]); } rep(i,1,n+1) if(in[i]>2||(in[i]==2&&!du[i])) return puts("0") , 0; int ans = 1; rep(i,1,n+1) if(du[i]) { vi cir; for(int c=i;du[c];c=a[c]) cir.pb(c),du[c]=0; vi branch; rep(i,0,sz(cir)) if(sz[cir[i]]!=1) branch.pb(i); if(sz(branch)) { branch.pb(branch[0]); rep(i,1,sz(branch)) { int from = branch[i - 1] , to = branch[i]; int dis = to - from; if(dis <= 0) dis += sz(cir); if(dis < sz[cir[to]] - 1) return puts("0") , 0; else if(dis > sz[cir[to]] - 1) (ans *= 2) %= P; } } else circlecnt[sz(cir)]++; } rep(i,1,n+1) if(circlecnt[i]) { int x = circlecnt[i]; f[0] = 1; rep(j,1,x+1) { f[j] = f[j - 1]; if(i >= 3 && i % 2 == 1) (f[j] += f[j - 1]) %= P; if(j >= 2) (f[j] += ll(f[j - 2]) * (j - 1) % P * i % P) %= P; } ans = ll(ans) * f[x] % P; } 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; #define ll long long #define f(a,b,c) for(int a=(b);a<=(c);++a) const int mod=1e9+7,maxn=1e5+5; int a[maxn],d[maxn],ci[maxn],vis[maxn],fl[maxn],sum[maxn],f[maxn]; int add(int a,int b){a+=b;return a>=mod?a-mod:a;} int n,ans=1; #define GG exit((puts("0"),0)) void solve(int x)//基环树 { int cur,ths,lst,fst; cur=ths=lst=fst=0; while(ci[x]) { ++cur,ci[x]=0; if(fl[x]) { if(ths==0)ths=lst=cur,fst=fl[x]; else { if(fl[x]>cur-lst)GG; if(fl[x]==cur-lst)ans=ans; if(fl[x]<cur-lst)ans=add(ans,ans); lst=cur; } } x=a[x]; } if(!ths)sum[cur]++; else { if(fst>cur-lst+ths)GG; if(fst==cur-lst+ths)ans=ans; if(fst<cur-lst+ths)ans=add(ans,ans); } } int main() { scanf("%d",&n); f(i,1,n)scanf("%d",a+i),d[a[i]]++; int x,l; f(i,1,n) { if(!vis[i]) { for(x=i;!vis[x];vis[x]=i,x=a[x]); if(vis[x]!=i)continue; while(!ci[x])ci[x]=1,x=a[x]; } } f(i,1,n){ if(ci[i]&&d[i]>=3)GG; if(!ci[i]&&d[i]>=2)GG; } f(i,1,n) { if(d[i])continue; for(x=i,l=0;!ci[x];x=a[x],l++); fl[x]=l; } f(i,1,n)if(ci[i])solve(i);//基环树 f(i,1,n)//简单环 { f[0]=1; f(j,1,sum[i]) { if(i>1&&i%2==1)f[j]=add(f[j-1],f[j-1]); else f[j]=f[j-1]; if(j>1)f[j]=add(f[j],(ll)f[j-2]*(j-1)%mod*i%mod); } ans=(ll)ans*f[sum[i]]%mod; } 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<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<vector> #include<set> #include<map> #include<bitset> #include<cmath> #include<string> #define ls (t<<1) #define rs ((t<<1)+1) #define mid ((l+r)>>1) #define fi first #define se second #define mk make_pair #define pb push_back #define N 100005 #define M 200005 #define Mo 1000000007 using namespace std; int i,j,m,n,p,k,a[N],chun[N],deg[N],D[N],vis[N],Q[N],size[N],tot,ans,Qt[N]; int fac[N],inv[N]; vector<int>tree[N],v[N]; void work(int x) { int i; Q[0]=0; for (i=x;!vis[x];x=a[x]) vis[x]=1,Q[++Q[0]]=x; for (i=1;i<=Q[0];++i) if (size[Q[i]]) break; if (i<=Q[0]) { ++tot; for (i=1;i<=Q[0];++i) tree[tot].pb(Q[i]); } else chun[Q[0]]++; } void jia(int &x,int y) { x+=y; if (x>=Mo) x-=Mo; } int dp() { int i,la=0,lb=0,fa=1; for (i=1;i<=Qt[0];++i) if (size[Qt[i]]) { int na=i+size[Qt[i]]-1,nb=i+size[Qt[i]],ga=0; if (la<i) jia(ga,fa); if (lb&&lb<i) jia(ga,fa); fa=ga; la=na; lb=nb; } int now=0; if (la<=Qt[0]) jia(now,fa); if (lb<=Qt[0]) jia(now,fa); return now; } int power(int x,int y) { int sum=1; for (;y;y>>=1) { if (y&1) sum=1ll*sum*x%Mo; x=1ll*x*x%Mo; } return sum; } int C(int x,int y) { return 1ll*fac[x]*inv[y]%Mo*inv[x-y]%Mo; } void pre() { fac[0]=inv[0]=1; for (i=1;i<N;++i) fac[i]=1ll*fac[i-1]*i%Mo,inv[i]=power(fac[i],Mo-2); } int Count(int x) { Q[0]=0; int i; for (i=0;i<(int)tree[x].size();++i) Q[++Q[0]]=tree[x][i]; if (Q[0]==1) { if (size[Q[1]]==1) return 1; return 0; } for (i=1;i<=Q[0];++i) if (size[Q[i]]) break; Qt[0]=0; for (j=i;j>=1;--j) Qt[++Qt[0]]=Q[j]; for (j=Q[0];j>i;--j) Qt[++Qt[0]]=Q[j]; return dp(); } int main() { pre(); scanf("%d",&n); for (i=1;i<=n;++i) scanf("%d",&a[i]),v[i].pb(a[i]),deg[a[i]]++; for (i=1;i<=n;++i) D[i]=deg[i]; for (i=1;i<=n;++i) if (!deg[i]) Q[++Q[0]]=i; for (i=1;i<=n;++i) if (deg[i]>2) { puts("0"); return 0; } int l; for (l=1;l<=Q[0];++l) { int p=Q[l]; vis[p]=1; size[p]++; for (i=0;i<(int)v[p].size();++i) { int k=v[p][i]; deg[k]--; size[k]+=size[p]; if (deg[k]==0) Q[++Q[0]]=k; } } for (i=1;i<=n;++i) if (vis[i]&&D[i]==2) { puts("0"); return 0; } for (i=1;i<=n;++i) if (!vis[i]) work(i); ans=1; for (i=1;i<=n;++i) if (chun[i]) { int now=0; for (j=0;j<=chun[i];j+=2) jia(now, 1ll*((i>1&&(i&1))?power(2,chun[i]-j):1)*C(chun[i],j)%Mo*C(j,j/2)%Mo*fac[j/2]%Mo*power(power(2,j/2),Mo-2)%Mo*power(i,j/2)%Mo); ans=1ll*ans*now%Mo; } for (i=1;i<=tot;++i) { int now=Count(i); ans=1ll*ans*now%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<iostream> #include<cstdio> #define MN 100000 #define mod 1000000007 using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int head[MN+5],cnt=0,n,vis[MN+5],Fa[MN+5],dep[MN+5],c[MN+5],top,in[MN+5],ans=1; int A,B,C,num[MN+5],vnum,cirnum[MN+5],p[MN+5],inv[MN+5],pw[MN+5],Out[MN+5]; struct edge{int to,next;}e[MN*2+5]; inline void ins(int f,int t) { e[++cnt]=(edge){t,head[f]};head[f]=cnt; e[++cnt]=(edge){f,head[t]};head[t]=cnt; } void dfs(int x,int fa) { Fa[x]=fa;vis[x]=1;++vnum; for(int i=head[x];i;i=e[i].next) if(e[i].to!=fa) { if(vis[e[i].to]) A=x,B=e[i].to; else dep[e[i].to]=dep[x]+1,dfs(e[i].to,x); } } inline int lca(int x,int y) { for(;x!=y;x=Fa[x]) if(dep[x]<dep[y]) swap(x,y); return x; } int GetChain(int x,int fa) { int son=0,val=1; for(int i=head[x];i;i=e[i].next) if(e[i].to!=fa) { ++son; int res=GetChain(e[i].to,x); if(res==0) return 0; else val+=res; } return son>1?0:val; } inline int CC(int n,int m){return 1LL*p[n]*inv[m]%mod*inv[n-m]%mod;} int main() { n=read();pw[0]=p[0]=inv[0]=inv[1]=p[1]=1;pw[1]=2; for(int i=2;i<=n;++i) p[i]=1LL*p[i-1]*i%mod,inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod,pw[i]=2*pw[i-1]%mod; for(int i=2;i<=n;++i) inv[i]=1LL*inv[i-1]*inv[i]%mod; for(int i=1,j;i<=n;++i) ++num[j=read()],ins(i,j); for(int i=1;i<=n;++i) if(num[i]>2) return 0*puts("0"); for(int i=1;i<=n;++i) if(!vis[i]) { vnum=top=0;dfs(i,0);C=lca(A,B); int len=dep[A]+dep[B]-2*dep[C]+1; if(len==vnum) {++cirnum[vnum];continue;} for(;A!=C;A=Fa[A]) c[++top]=A;c[++top]=C;top=len; for(;B!=C;B=Fa[B]) c[len--]=B; for(int j=1;j<=top;++j) in[c[j]]=1; for(int j=head[c[1]];j;j=e[j].next) if(e[j].to==c[2]) { if(~j&1) for(int l=1,r=top;l<r;++l,--r) swap(c[l],c[r]); break; } for(int j=1;j<=top;++j) { Out[j]=0; for(int k=head[c[j]];k;k=e[k].next) if(!in[e[k].to]) { Out[j]=GetChain(e[k].to,c[j]); if(!Out[j]) return 0*puts("0"); } } int last,way=1; for(int j=top;j;--j) if(Out[j]) {last=top-j;break;} for(int j=1;j<=top;++j) { ++last; if(!Out[j]) continue; if(Out[j]>last) return 0*puts("0"); else if(Out[j]<last) way=way*2%mod; last=0; } ans=1LL*ans*way%mod; } for(int i=1;i<=n;++i) if(cirnum[i]) { int way=(i==1||i%2==0)?1:pw[cirnum[i]]; for(int j=1,k=i,i2=mod+1>>1;j<<1<=cirnum[i];++j,k=1LL*k*i%mod,i2=1LL*i2*(mod+1)/2%mod) { int thway=1LL*CC(cirnum[i],j*2)*CC(j*2,j)%mod*p[j]%mod*i2%mod*k%mod; if(i==1||i%2==0);else thway=1LL*thway*pw[cirnum[i]-2*j]%mod; way=(way+thway)%mod; } ans=1LL*ans*way%mod; } 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; const int N=100010,mo=int(1e9+7); struct edge{int s,t,n;}e[N]; int n,a[N],h[N],d[N],vis[N],inc[N],len[N],cnt[N],fac[N],ifac[N],ans; vector <int> V; int qpow(int a,int b) { int x=a; a=1; while (b) { if (b&1) a=1LL*a*x%mo; x=1LL*x*x%mo,b>>=1; } return a; } int C(int n,int m){return 1LL*fac[n]*ifac[m]%mo*ifac[n-m]%mo;} int go(int x) { if (vis[x]) return x; vis[x]=1; int _=go(a[x]); if (_) inc[x]=1,V.push_back(x); return _==x?0:_; } int dfs(int x,int f) { int _=0; vis[x]=1; for (int i=h[x],y; y=e[i].t,i; i=e[i].n) if ((y!=f)&&(!inc[y])) { if (_) return -1<<30; else _=dfs(y,x)+1; } return _; } void work() { scanf("%d",&n),ans=1; for (int i=1; i<=n; i++) scanf("%d",&a[i]),e[i]=(edge){a[i],i,h[a[i]]},h[a[i]]=i,d[a[i]]++; for (int i=1,s; i<=n; i++) if (!vis[i]) { V.clear(),go(i); for (int i=0; i<V.size(); i++) if ((len[i+1]=dfs(V[i],0))<0) puts("0"),exit(0); s=0; for (int i=1; i<=V.size(); i++) if (!len[i]) s++; else break; if (s==V.size()) {cnt[s]++; continue;} for (int i=V.size(); i; i--) { s++; if (len[i]) { if (s<len[i]) puts("0"),exit(0); if (s>len[i]) ans=2*ans%mo; s=0; } } } fac[0]=1; for (int i=1; i<=n; i++) fac[i]=1LL*fac[i-1]*i%mo; ifac[n]=qpow(fac[n],mo-2); for (int i=n; i; i--) ifac[i-1]=1LL*ifac[i]*i%mo; for (int i=1,s; i<=n; i++) if (cnt[i]) { s=0; for (int j=0,_; j<=cnt[i]/2; j++) { _=1LL*C(cnt[i],2*j)*fac[2*j]%mo*ifac[j]%mo*qpow(2,mo-1-j)%mo*qpow(i,j)%mo; if ((i&1)&&(i>1)) _=1LL*_*qpow(2,cnt[i]-2*j)%mo; s=(s+_)%mo; } ans=1LL*ans*s%mo; } printf("%d",ans); } int main() { 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; const int RLEN=1<<18|1; inline char nc() { static char ibuf[RLEN],*ib,*ob; (ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin)); return (ib==ob) ? -1 : *ib++; } inline int rd() { char ch=nc(); int i=0,f=1; while(!isdigit(ch)) {if(ch=='-')f=-1; ch=nc();} while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=nc();} return i*f; } const int N=1e5+50, mod=1e9+7; inline int add(int x,int y) {return (x+y>=mod) ? (x+y-mod) : (x+y);} inline int dec(int x,int y) {return (x-y<0) ? (x-y+mod) : (x-y);} inline int mul(int x,int y) {return (long long)x*y%mod;} inline int power(int a,int b,int rs=1) {for(;b;b>>=1,a=mul(a,a)) if(b&1) rs=mul(rs,a); return rs;} inline int cinv(int a) {return power(a,mod-2);} int n,ans=1,fa[N],incir[N],vis[N]; int cnt[N]; vector <int> edge[N]; struct combin { int fac[N],ifac[N]; combin() { fac[0]=1; for(int i=1;i<N;i++) fac[i]=mul(fac[i-1],i); ifac[N-1]=cinv(fac[N-1]); for(int i=N-2;~i;i--) ifac[i]=mul(ifac[i+1],i+1); } inline int C(int a,int b) {return (a<b) ? 0 : (mul(fac[a],mul(ifac[b],ifac[a-b])));} } C; inline void dfs(int x) { vis[x]=1; for(auto v:edge[x]) { if(!vis[v]) fa[v]=x, dfs(v); else { for(int u=x;;u=fa[u]) { if(!incir[u]) incir[u]=1; else {puts("0"); exit(0);} if(u==v) break; } } } } inline int calc_dep(int x) { if(edge[x].size()>=2) return -1e9; if(!edge[x].size()) return 1; return calc_dep(edge[x][0])+1; } vector <int> cir; vector <int> ft; int pos[N],dep[N],f[N]; inline int calc(int x) { cir.clear(); ft.clear(); int now=x; while(1) { cir.push_back(now); pos[now]=cir.size(); vis[now]=1; if(edge[now].size()>=3) return 0; for(auto v:edge[now]) { if(!incir[v]) { int t=calc_dep(v); if(t>0) ft.push_back(now), dep[now]=t; else return 0; } } for(auto v:edge[now]) if(incir[v]) {now=v; break;} if(now==x) break; } if(!ft.size()) return ++cnt[cir.size()],1; else { int sum=1; for(int i=0;i<ft.size();++i) { int dis=(ft.size()==1) ? cir.size() : (ft[i]==ft.back() ? cir.size()-pos[ft[i]]+pos[ft[0]] : pos[ft[i+1]]-pos[ft[i]]); if(dis<dep[ft[i]]) return 0; else if(dis>dep[ft[i]]) sum=mul(sum,2); } return sum; } } int main() { n=rd(); for(int i=1;i<=n;i++) edge[rd()].push_back(i); for(int i=1;i<=n;i++) if(!vis[i] && edge[i].size()>=2) dfs(i); for(int i=1;i<=n;i++) if(!vis[i]) dfs(i); memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) if(incir[i] && !vis[i] && ans) ans=mul(ans,calc(i)); f[0]=f[2]=1; for(int i=4;i<=n;i+=2) f[i]=add(f[i-2],mul(mul(i-2,i-3),f[i-4])); for(int i=1;i<=n;i++) { int sum=0; for(int j=0;j<=cnt[i];j+=2) { int tp=mul(C.C(cnt[i],j),f[j]); if(i!=1) { if(i&1) tp=mul(tp,power(2,cnt[i]-j)); tp=mul(tp,power(i,j/2)); } sum=add(sum,tp); } ans=mul(ans,sum); } 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 <bits/stdc++.h> using namespace std; #define RI register int int read() { int q = 0; char ch = ' '; while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') q = q * 10 + ch - '0', ch = getchar(); return q; } const int mod = 1e9 + 7, N = 100005; int n, ans; int a[N], du[N], cir[N], vis[N], footL[N], sum[N], f[N]; int qm(int x) { return x >= mod ? x - mod : x; } void workcir(int x) { int now = 0, fr = 0, ed = 0, frL = 0; while (cir[x]) { ++now, cir[x] = 0; if (footL[x]) { if (!fr) ed = fr = now, frL = footL[x]; else { int kl = (footL[x] < now - ed) + (footL[x] <= now - ed); ans = 1LL * ans * kl % mod, ed = now; } } x = a[x]; } if (!fr) ++sum[now]; else { int kl = (frL < now - ed + fr) + (frL <= now - ed + fr); ans = 1LL * ans * kl % mod; } } void work() { for (RI i = 1; i <= n; ++i) { if (du[i]) continue; int x = i, len = 0; while (!cir[x]) x = a[x], ++len; footL[x] = len; } ans = 1; for (RI i = 1; i <= n; ++i) if (cir[i]) workcir(i); for (RI i = 1; i <= n; ++i) { if (!sum[i]) continue; f[0] = 1; for (RI j = 1; j <= sum[i]; ++j) { if (i > 1 && (i & 1)) f[j] = qm(f[j - 1] + f[j - 1]); else f[j] = f[j - 1]; if (j > 1) f[j] = qm(f[j] + 1LL * f[j - 2] * (j - 1) % mod * i % mod); } ans = 1LL * ans * f[sum[i]] % mod; } } int main() { n = read(); for (RI i = 1; i <= n; ++i) a[i] = read(), ++du[a[i]]; for (RI 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 (RI i = 1; i <= n; ++i) if ((cir[i] && du[i] > 2) || (!cir[i] && du[i] > 1)) return puts("0"), 0; work(); 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 using namespace std; const int N=100005,P=1e9+7; typedef long long ll; int n,ans=1,crc[N],nx[N],f0,f1,f2; vector<int>e[N]; bool vis[N],in[N]; inline void Assert(bool f){if(!f)puts("0"),exit(0);} int find(int x){if(in[x])return x;in[x]=true;int R=find(nx[x]);return in[x]=false,R;} int dfs(int x){vis[x]=true;Assert(e[x].size()<=1);for(int v:e[x])return dfs(v)+1;return 1;} void check(int x){ int u=find(x),m=1; static vector<int>cr,le; cr.clear();cr.pb(u);in[u]=vis[u]=true; for(int p=nx[u];p!=u;p=nx[p])cr.pb(p),in[p]=vis[p]=true,m++; le.resize(cr.size()); int f=-1; for(int i=0,v;i<m;i++){ Assert(e[v=cr[i]].size()<=2); for(int p:e[v])if(!in[p])le[i]=dfs(p),f=i; if(e[v].size()==1)le[i]=0; } if(f==-1){crc[m]++;return;} for(int i=0;i<m;i++){ if(le[i]){ int dis=(i-f+m)%m; if(f==i)dis=m; Assert(dis>=le[i]); if(dis>le[i])ans=(ans<<1)%P; f=i; } } } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&nx[i]),e[nx[i]].pb(i); for(int i=1;i<=n;i++)if(!vis[i])check(i); for(int i=1;i<=n;i++)if(crc[i]){ int c0=1+(i%2==1&&i>=3); f0=1,f1=c0; for(int j=2;j<=crc[i];j++){ f2=((ll)(j*i-i)*f0%P+c0*f1)%P; f0=f1,f1=f2; } ans=(ll)ans*f1%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 <cstdio> #include <cstring> #include <algorithm> #define MAXN 200010 #define LL long long using namespace std; const LL P=1000000007; LL getPow(LL x,LL y){ LL res=1; while(y){ if(y&1) res=res*x%P; x=x*x%P; y>>=1; } return res; } struct edge{ int to,next; edge(int _to=0,int _next=0):to(_to),next(_next){} }e[MAXN]; int n; int g[MAXN],nume; int p[MAXN]; bool visit0[MAXN],visit[MAXN]; int b[MAXN],l[MAXN],sl[MAXN],numb; int cnt[MAXN]; LL f[MAXN],fac[MAXN],invfac[MAXN]; LL ans=1; void addEdge(int u,int v){ e[nume]=edge(v,g[u]); g[u]=nume++; } int dfs(int x){ visit[x]=1; int numch=0; int res=0; for(int i=g[x];~i;i=e[i].next) if(!visit[e[i].to]){ numch++; res=dfs(e[i].to)+1; } if(numch>=2) ans=0; return res; } void gao(int x){ numb=0; while(!visit[x]){ visit[x]=1; b[++numb]=x; x=p[x]; } int pos=0; for(int i=1;i<=numb;i++){ l[i]=dfs(b[i]); if(l[i]) pos=i; } if(!pos){ cnt[numb]++; return; } for(int i=1;i<=numb;i++){ b[i+numb]=b[i]; l[i+numb]=l[i]; b[i]=b[i+pos-1]; l[i]=l[i+pos-1]; } for(int i=1;i<=numb;i++) sl[i]=sl[i-1]+l[i]; LL res1=0,res2=0; if(sl[numb]-sl[numb-(l[1]-1)]==0){ res1=1; for(int i=2;i<=numb;i++){ if(!l[i]) continue; LL temp=0; if(sl[i-1]-sl[max(0,i-l[i])]==0) temp++; if(sl[i-1]-sl[max(0,i-l[i]-1)]==0) temp++; res1=res1*temp%P; } } if(sl[numb]-sl[numb-l[1]]==0){ res2=1; for(int i=2;i<=numb;i++){ if(!l[i]) continue; LL temp=0; if(sl[i-1]-sl[max(0,i-l[i])]==0) temp++; if(sl[i-1]-sl[max(0,i-l[i]-1)]==0) temp++; res2=res2*temp%P; } } ans=ans*(res1+res2)%P; } void init(){ f[0]=1; for(int i=2;i<=n;i++) f[i]=f[i-2]*(i-1)%P; fac[0]=1; for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%P; invfac[n]=getPow(fac[n],P-2); for(int i=n-1;i>=0;i--) invfac[i]=invfac[i+1]*(i+1)%P; } LL getC(int x,int y){ return fac[x]*invfac[y]%P*invfac[x-y]%P; } int main(){ #ifdef DEBUG freopen("E.in","r",stdin); #endif memset(g,-1,sizeof g); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",p+i); addEdge(p[i],i); } for(int i=1;i<=n;i++) if(!visit[i]){ int x=i; for(;!visit0[x];x=p[x]) visit0[x]=1; gao(x); } init(); for(int i=1;i<=n;i++) if(cnt[i]){ LL res=0; for(int j=0;j<=cnt[i];j+=2) if(i>=3 && (i&1)) res=(res+getC(cnt[i],j)*f[j]%P*getPow(i,j/2)%P*getPow(2,cnt[i]-j))%P; else res=(res+getC(cnt[i],j)*f[j]%P*getPow(i,j/2))%P; ans=ans*res%P%P; } 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; int read(){ int x=0; char ch=getchar(); while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return x; } const int N=100005,mod=1e9+7; int n,a[N],in[N],size[N],vis[N],cro[N]; int q[N],head,tail; int k[N],m; int r[N]; void add(int &x,int y){ if ((x+=y)>=mod) x-=mod; } int sc=0; int Pre(int k,int n){ return k==1?n:(k-1); } int solve(int n){ int ans=1,f=0; for (int i=1;i<=n;i++){ if (k[i]==0) continue; f=1; int x=Pre(i,n); while (!k[x]) x=Pre(x,n); int t=(i-x+n)%n; if (t==0) t+=n; if (t<k[i]) return 0; if (t>k[i]) ans=2LL*ans%mod; } if (!f) r[n]++; return ans; } int dp[N]; int main(){ n=read(); memset(in,0,sizeof in); for (int i=1;i<=n;i++){ in[a[i]=read()]++; size[i]=1; } head=tail=0; for (int i=1;i<=n;i++){ if (in[i]>2) return puts("0"),0; if (in[i]==0) q[++tail]=i; if (in[i]==2) cro[i]=1; } while (head!=tail){ int x=q[++head],y=a[x]; size[y]+=size[x]; if (!(--in[y])) q[++tail]=y; } for (int i=1;i<=n;i++) if (cro[i]&&!in[i]) return puts("0"),0; int ans=1; memset(vis,0,sizeof vis); for (int i=1;i<=n;i++){ if (in[i]==0||vis[i]) continue; m=0; for (int x=i;!vis[x];x=a[x]) vis[x]=1,k[++m]=size[x]-1; ans=1LL*ans*solve(m)%mod; } for (int i=1;i<=n;i++) if (r[i]>0){ dp[0]=1,dp[1]=i>1&&(i&1)?2:1; for (int j=2;j<=r[i];j++) dp[j]=(1LL*(j-1)*i%mod*dp[j-2]+dp[j-1]*(i>1&&(i&1)?2:1))%mod; ans=1LL*ans*dp[r[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; const int N=1e5+100; 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,i,k,t,s,cnt=0,top,ans=1,p2=0; bool is; for (i=1,cin>>n;i<=n;i++) cin>>p[i],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> #define N 100005 #define ll long long #define P 1000000007 using namespace std; int n,fa[N],b[N],rd[N],fl[N],sum[N]; void biu() { puts("0"); exit(0); } void dfs(int x) { if (fl[fa[x]]) biu(); rd[fa[x]]--;fl[fa[x]]=fl[x]+1;rd[x]=-1; if (rd[fa[x]]==0) dfs(fa[x]); } int F(int x,int k) { static int f[N]; f[0]=1;f[1]=2; for (int i=2;i<=x;i++) f[i]=(f[i-1]*2+(ll)f[i-2]*(i-1)%P*k)%P; return f[x]; } int G(int x,int k) { // cout<<x<<' '<<k<<endl; static int g[N]; g[0]=1;g[1]=1; for (int i=2;i<=x;i++) g[i]=(g[i-1]+(ll)g[i-2]*(i-1)%P*k)%P; return g[x]; } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&fa[i]),rd[fa[i]]++; for (int i=1;i<=n;i++) if (rd[i]==0) dfs(i); int Ans=1; for (int i=1;i<=n;i++) if (rd[i]==1) { int x=i,y=fa[i],cnt=0,flag=0; b[++cnt]=fl[i]; if (fl[i]) flag=cnt; for (;y!=i;y=fa[y],x=fa[x]) { b[++cnt]=fl[y];rd[y]=0; if (fl[y]) flag=cnt; } //cout<<cnt<<' '<<flag<<endl; if (!flag) sum[cnt]++; else { flag=flag-cnt; for (int j=1;j<=cnt;j++) { //cout<<b[j]<<endl; if (b[j]) { if (b[j]>j-flag) biu(); if (b[j]<j-flag) Ans=Ans*2%P; flag=j; } } } } // cout<<Ans<<endl; for (int i=1;i<=n;i++) { // cout<<i<<' '<<sum[i]<<endl; if ((i&1)&&i!=1) Ans=(ll)Ans*F(sum[i],i)%P; else Ans=(ll)Ans*G(sum[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 <cstdio> #define MOD 1000000007 long long dp[100005], ans = 1; int pre[100005], nxt[100005], in[100005], cnt[100005]; bool vis[100005]; int main() { // freopen("AGC008-E.in", "r", stdin); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", nxt + i); in[--nxt[i]]++; } for (int i = 0; i < n; i++) { if (in[i] > 2) { puts("0"); return 0; } if (in[i] != 2 || vis[i]) continue; int u = i; do { if (vis[u]) { puts("0"); return 0; } vis[u] = true; pre[nxt[u]] = u; u = nxt[u]; } while (u != i); } for (int i = 0; i < n; i++) { if (in[i]) continue; int u = i, link_len = 0, cyc_len = 0; while (!vis[u]) { vis[u] = true; u = nxt[u]; link_len++; } do { u = pre[u]; cyc_len++; } while (in[u] == 1); if (link_len < cyc_len) ans = ans * 2 % MOD; else if (link_len > cyc_len) { puts("0"); return 0; } } for (int i = 0; i < n; i++) { if (vis[i]) continue; int u = i, len = 0; do { vis[u] = true; u = nxt[u]; len++; } while (u != i); cnt[len]++; } for (int i = 1; i <= n; i++) { int coef = ((i & 1) && i != 1) + 1; dp[0] = 1; dp[1] = coef; for (int j = 2; j <= cnt[i]; j++) dp[j] = (dp[j - 2] * (j - 1) * i + dp[j - 1] * coef) % MOD; ans = ans * dp[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; #define RI register int int read() { int q=0;char ch=' '; while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') q=q*10+ch-'0',ch=getchar(); return q; } const int mod=1e9+7,N=100005; int n,ans; int a[N],du[N],cir[N],vis[N],footL[N],sum[N],f[N]; int qm(int x) {return x>=mod?x-mod:x;} void workcir(int x) { int now=0,fr=0,ed=0,frL=0; while(cir[x]) { ++now,cir[x]=0; if(footL[x]) { if(!fr) ed=fr=now,frL=footL[x]; else { int kl=(footL[x]<now-ed)+(footL[x]<=now-ed); ans=1LL*ans*kl%mod,ed=now; } } x=a[x]; } if(!fr) ++sum[now]; else { int kl=(frL<now-ed+fr)+(frL<=now-ed+fr); ans=1LL*ans*kl%mod; } } void work() { for(RI i=1;i<=n;++i) { if(du[i]) continue; int x=i,len=0;while(!cir[x]) x=a[x],++len; footL[x]=len; } ans=1; for(RI i=1;i<=n;++i) if(cir[i]) workcir(i); //cout<<ans<<endl; for(RI i=1;i<=n;++i) { if(!sum[i]) continue; f[0]=1; for(RI j=1;j<=sum[i];++j) { if(i>1&&(i&1)) f[j]=qm(f[j-1]+f[j-1]); else f[j]=f[j-1]; if(j>1) f[j]=qm(f[j]+1LL*f[j-2]*(j-1)%mod*i%mod); } ans=1LL*ans*f[sum[i]]%mod; } } int main() { n=read(); for(RI i=1;i<=n;++i) a[i]=read(),++du[a[i]]; for(RI 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(RI i=1;i<=n;++i) if((cir[i]&&du[i]>2)||(!cir[i]&&du[i]>1)) {puts("0");return 0;} work(); 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 maxn 100005 #define mod 1000000007 int n,a[maxn],deg[maxn],pre[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; #define N 1111116 #define mod 1000000007 int n,a[N],d[N],h[N],g[N]; long long ans,f[N]; queue<int> q; int read(){ int x=0,f=1;char ch=getchar(); for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-f; for (;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; return x*f; } int main(){ n=read();ans=1; for (int i=1;i<=n;i++) a[i]=read(),d[a[i]]++; for (int i=1;i<=n;i++) if (!d[i]) q.push(i); while (!q.empty()){ int t=q.front();q.pop(); if (h[a[t]]) ans=0;h[a[t]]=h[t]+1; if (--d[a[t]]==0) q.push(a[t]); } for (int i=1;i<=n;i++) if (d[i]){ int t=i;vector<int> q; while (a[t]!=i&&!h[t]) t=a[t]; while (d[t]){ q.push_back(h[t]); d[t]=0;t=a[t]; } if (!q[0]) g[q.size()]++; else { q.push_back(q[0]); for (int i=1,j=0;i<(int)q.size();i++) if (q[i]){ if (q[i]<i-j) ans=ans*2%mod; if (q[i]>i-j) ans=0; j=i; } } } for (int i=1;i<=n;i++){ f[0]=1;f[1]=(i>1&&i%2)+1; for (int j=2;j<=g[i];j++) f[j]=(f[j-1]*f[1]+f[j-2]*(j-1)*i)%mod; ans=(ans*f[g[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; #define ll long long #define inf 0x3f3f3f3f #define N 100020 #define mod 1000000007 inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f; } int n,a[N],du[N],col[N],len[N],cnt[N]; ll ans=1,dp[N]; bool cir[N]; inline int calc(int l1,int l2){ return (l1<l2)+(l1<=l2); } int main(){ // freopen("a.in","r",stdin); n=read();for(int i=1;i<=n;++i) a[i]=read(),du[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]&&du[i]>1)||(cir[i]&&du[i]>2)){puts("0");return 0;} for(int i=1;i<=n;++i){//计算foot的长度 if(du[i]) continue;int x=i,res=0; while(!cir[x]) ++res,x=a[x];len[x]=res; }for(int i=1;i<=n;++i){//处理基环内向树 if(!cir[i]) continue;int x=i,fir=0,firlen=0,k=0,last=0; while(cir[x]){ ++k;cir[x]=0; if(len[x]){ if(!fir){fir=last=k;firlen=len[x];x=a[x];continue;} ans=ans*calc(len[x],k-last)%mod;if(!ans){puts("0");return 0;}last=k; }x=a[x]; }if(!fir) cnt[k]++;else ans=ans*calc(firlen,fir+k-last)%mod; }if(!ans){puts("0");return 0;} for(int i=1;i<=n;++i){//处理大小为i的所有环 if(!cnt[i]) continue;dp[0]=1;int op=(i&1)+(i>1);//奇环且大于1有两种 for(int j=1;j<=cnt[i];++j){ dp[j]=dp[j-1]*op%mod; if(j>1) dp[j]=(dp[j]+(ll)(j-1)*i*dp[j-2]%mod)%mod; }ans=ans*dp[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; typedef long long ll; const int N=1e5+10,mod=1e9+7; int n,ans=1; int t[N],a[N],vs[N],d[N],dr[N],f[N]; bool cir[N]; inline int ad(int x,int y){x+=y;return x>=mod?x-mod:x;} void sol(int x) { int nw=0,fi=0,sc,pre; for(;cir[x];x=a[x]){ ++nw;cir[x]=false; if(!dr[x]) continue; if(!fi) {fi=sc=nw;pre=dr[x];} else{ if(nw-sc<dr[x]) ans=0; else if(nw-sc>dr[x]) ans=ad(ans,ans); sc=nw; } } if(!fi) t[nw]++; else{ nw=fi-sc+nw; if(nw<pre) ans=0; else if(nw>pre) ans=ad(ans,ans); } } int main(){ int i,j,k; scanf("%d",&n); for(i=1;i<=n;++i) {scanf("%d",&a[i]);d[a[i]]++;} for(i=1;i<=n;++i) if(!vs[i]){ vs[i]=i; for(j=a[i];!vs[j];j=a[j]) vs[j]=i; if(vs[j]^i) continue; for(;!cir[j];j=a[j]) cir[j]=true; } for(i=1;i<=n;++i) if((cir[i] && d[i]>2)||((!cir[i])&& d[i]>1)) {puts("0");return 0;} for(i=1;i<=n;++i) if(!d[i]){ for(k=0,j=i;(!cir[j]);j=a[j]) k++; dr[j]=k; } for(i=1;i<=n;++i) if(cir[i]) sol(i); if(!ans) {puts("0");return 0;} f[0]=1; for(i=1;i<=n;++i) if(t[i]){ for(j=1;j<=t[i];++j){ if(i>1 && (i&1)) f[j]=ad(f[j-1],f[j-1]); else f[j]=f[j-1]; if(j>1) f[j]=ad(f[j],(ll)f[j-2]*(j-1)*i%mod); } ans=(ll)ans*f[t[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<bits/stdc++.h> using namespace std; #define Set(a,b) memset(a,b,sizeof(a)) typedef long long ll; const int N=1e5+10; const int mod=1e9+7; int fa[N],n,son[N],len[N]; bool cir[N];int vis[N]; int num[N];int dp[N]; template<class T>inline void Inc(T&x,int y){x+=y;if(x>=mod) x-=mod;return;} inline int Mod(int x){if(x>=mod) x-=mod;return x;} int main() { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&fa[i]),++son[fa[i]]; for(int i=1;i<=n;++i) { if(vis[i]) continue; int u=i; while(!vis[u]) vis[u]=i,u=fa[u]; if(vis[u]==i) {while(!cir[u]) cir[u]=1,u=fa[u];} } for(int i=1;i<=n;++i) if((cir[i]&&son[i]>2)||(!cir[i]&&son[i]>1)) return puts("0"),0; for(int i=1;i<=n;++i) { if(!son[i]) { int u=i,l=0; while(!cir[u]) ++l,u=fa[u]; if(len[u]) return puts("0"),0; len[u]=l; } }int ans=1; for(int i=1;i<=n;++i){ if(!cir[i]) continue; int size=0,fir=0,pfir=0,now=0; for(int u=i;cir[u];u=fa[u]){ ++size;cir[u]=0; if(len[u]) { if(!fir) fir=size,pfir=u,now=size; else { int pre=now;now=size;int d=now-pre; if(d>len[u]) Inc(ans,ans); else if(d<len[u]) {puts("0");return 0;} } } } if(!fir) ++num[size]; else { int d=size-now+fir; if(d>len[pfir]) Inc(ans,ans); else if(d<len[pfir]) {puts("0");return 0;} } } for(int i=1;i<=n;++i){ if(!num[i]) continue;dp[0]=1; for(int j=1;j<=num[i];++j) { dp[j]=dp[j-1];//keep the same if(i>1&&(i&1)) Inc(dp[j],dp[j]);//two kinds if(j>1) Inc(dp[j],(ll)dp[j-2]*(j-1)%mod*i%mod);// union } ans=(ll)ans*dp[num[i]]%mod; } printf("%d\n",ans%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> using namespace std; template<typename T>inline T read(){ T f=0,x=0;char c=getchar(); while(!isdigit(c)) f=c=='-',c=getchar(); while(isdigit(c)) x=x*10+c-48,c=getchar(); return f?-x:x; } namespace run{ const int N=1e5+9,mod=1e9+7; inline int add(int x,int y){return x+y>=mod?x-mod+y:x+y;} int n,a[N],cir[N],vis[N],deg[N],len[N],top[N],sum[N],f[N]; int ans=1; inline void solve(int x){ int tmp=a[x],las=0,now=0,ret=1; while(cir[tmp]==1){ now++; if(len[tmp]){ int res=(now-las>=len[tmp])+(now-las>len[tmp]); ret=1LL*ret*res%mod,las=now; } cir[tmp]=2,tmp=a[tmp]; } ans=1LL*ans*ret%mod; } int main(){ n=read<int>(); for(int i=1;i<=n;i++) deg[a[i]=read<int>()]++; for(int i=1;i<=n;i++){ int tmp=i; while(!vis[tmp]) vis[tmp]=i,tmp=a[tmp]; if(vis[tmp]==i){ int now=tmp; do{cir[now]=1,now=a[now];}while(now!=tmp); } } for(int i=1;i<=n;i++) if((cir[i] && deg[i]>2) || (!cir[i] && deg[i]>1)) puts("0"),exit(0); for(int i=1;i<=n;i++) if(!deg[i]){ int tmp=i,siz=0; while(!cir[tmp]) siz++,tmp=a[tmp]; len[tmp]=siz,top[i]=tmp; } for(int i=1;i<=n;i++) if(!deg[i] && cir[top[i]]==1) solve(top[i]); for(int i=1;i<=n;i++) if(cir[i]==1){ int tmp=i,siz=0; while(cir[tmp]==1) cir[tmp]=2,siz++,tmp=a[tmp]; sum[siz]++; } for(int i=1;i<=n;i++) if(sum[i]){ 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]=(1LL*f[j-2]*i%mod*(j-1)+f[j])%mod; } ans=1LL*ans*f[sum[i]]%mod; } printf("%d\n",ans); return 0; } } int main(){ #ifdef my freopen(".in","r",stdin); freopen(".out","w",stdout); #endif return run::main(); }
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 + 5); const int mod(1e9 + 7); inline void Inc(int &x, int y) { x = x + y >= mod ? x + y - mod : x + y; } inline void Dec(int &x, int y) { x = x - y < 0 ? x - y + mod : x - y; } inline int Add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; } inline int Sub(int x, int y) { return x - y < 0 ? x - y + mod : x - y; } int n, cir[maxn], cnt, fa[maxn], vis[maxn], len, d[maxn], in[maxn]; int ans, f[maxn], a[maxn], chain[maxn], que[maxn << 1]; inline void GetCircle() { int i; for (i = 1; i <= len; ++i) if (d[que[i]] ^ 1) return; cir[++cnt] = len; for (i = 1; i <= len; ++i) vis[que[i]] = 2; } void Dfs1(int u) { int cur; vis[u] = 1, in[u] = 1; if (!vis[a[u]]) fa[a[u]] = u, Dfs1(a[u]); else if (in[a[u]]) { len = 0; for (cur = u; ; cur = fa[cur]) { que[++len] = cur, vis[cur] = 3; if (cur == a[u]) break; } GetCircle(); } in[u] = 0; } void Dfs2(int u) { chain[a[u]] = chain[u] + 1; if (vis[a[u]] > 1) return; Dfs2(a[u]); } int Solve(int x) { int cur, i, j, ret = 1; que[len = 1] = x; for (cur = a[x]; cur ^ x; cur = a[cur]) que[++len] = cur; reverse(que + 1, que + len + 1), cur = len + len; for (i = 1; i <= len; ++i) vis[que[i]] = 4, que[len + i] = que[i]; for (i = 1; i <= len; ++i) if (chain[que[i]]) { for (j = i + 1; j <= cur && !chain[que[j]]; ++j); if (chain[que[i]] > j - i) puts("0"), exit(0); if (chain[que[i]] < j - i) Inc(ret, ret); i = j - 1; } return ret; } int main() { int i, j, k, ret = 1; scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%d", &a[i]), ++d[a[i]]; for (i = 1; i <= n; ++i) if (!d[i]) Dfs1(i); for (i = 1; i <= n; ++i) if (!vis[i]) Dfs1(i); for (i = 1; i <= n; ++i) if ((vis[i] > 1 && d[i] > 2) || (vis[i] == 1 && d[i] > 1)) return puts("0"), 0; sort(cir + 1, cir + cnt + 1); for (i = 1; i <= n; i = j) { for (j = i; j <= n && cir[j] == cir[i]; ++j); f[i - 1] = 1; for (k = i; k < j; ++k) { f[k] = f[k - 1]; if (cir[i] > 1 && (cir[i] & 1)) Inc(f[k], f[k - 1]); if (k > i) Inc(f[k], (ll)f[k - 2] * (k - i) % mod * cir[i] % mod); } ret = (ll)ret * f[j - 1] % mod; } for (i = 1; i <= n; ++i) if (vis[i] == 1) vis[i] = 0; for (i = 1; i <= n; ++i) if (!d[i]) Dfs2(i); for (i = 1; i <= n; ++i) if (vis[i] == 3) ret = (ll)ret * Solve(i) % mod; printf("%d\n", ret); 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> #define N 100005 using namespace std; typedef long long ll; const int mod=1e9+7; int n,a[N],D,IN[N],siz,sum[N],len[N],b[N]; bool inc[N]; ll f[N]; bool vis[N],used[N]; ll calc(ll x){ return x*(x-1)/2%mod; } 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(vis[i]) continue; int g=i; while(!vis[g]) vis[g]=1,g=a[g]; if(used[g]){ g=i; while(!used[g]) used[g]=1,g=a[g]; continue; } int x=a[g];inc[g]=1;b[a[g]]=g; while(x!=g) inc[x]=1,b[a[x]]=x,x=a[x]; g=i; while(!used[g]) used[g]=1,g=a[g]; } for(int i=1;i<=n;i++){ if(inc[i]){if(IN[i]>2) return puts("0"),0;} else if(IN[i]>1) return puts("0"),0; } for(int i=1;i<=n;i++){ if(!IN[i]){ int g=i,l=0; while(!inc[g]) l++,g=a[g]; len[g]=l; } } memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++){ if(!vis[i]&&inc[i]){ int g=i,t=1,L=0; if(len[i]) t=0; while(!vis[g]){ if(len[g]) t=0; L++; vis[g]=1; g=a[g]; } if(t) sum[L]++; } } ll ans=1; for(int i=1;i<=n;i++){ ll g=1+(i&1); if(i==1) g--; f[0]=1; for(int p=1;p<=sum[i];p++){ f[p]=f[p-1]*g; if(p>1) f[p]=(f[p]+f[p-2]*(p-1)%mod*i)%mod; } ans=ans*f[sum[i]]%mod; } memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++){ if(inc[i]&&len[i]){ int g=i; int x=b[g],L=1; while(!len[x]) x=b[x],L++; if(len[g]>L) return puts("0"),0; if(len[g]<L) ans=ans*2%mod; g=a[g];L=1; while(g!=i){ if(!len[g]){ L++;g=a[g];continue; } if(len[g]>L) return puts("0"),0; if(len[g]<L) ans=ans*2%mod; L=1;g=a[g]; } g=i; while(inc[g]) inc[g]=0,g=a[g]; } } 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 <stdio.h> #include <iostream> #include <algorithm> #include <memory.h> using namespace std; typedef long long LL; const int maxn = 200005; const int mod = 1e9+7; bool vis[maxn],ins[maxn];int n,a[maxn],deg[maxn];int ans=1; bool onring[maxn];int stk[maxn],top,len[maxn],cnt[maxn]; int fac[maxn],inc[maxn],pw[maxn],ffac[maxn]; int fpm(LL p,int k) { LL res=1ll; while (k) { if (k&1) (res*=p)%=mod; (p*=p)%=mod;k>>=1; } return res; } LL comb(int n,int m) { return (LL)fac[n]*inc[m]%mod*(LL)inc[n-m]%mod; } void dfs(int u) { stk[++top]=u;ins[u]=vis[u]=true; if (ins[a[u]]) { onring[a[u]]=true; for (int j=top;stk[j]!=a[u];j--) onring[stk[j]]=true; } if (!vis[a[u]]) dfs(a[u]); top--;ins[u]=false; } void dfs2(int u,int dis) { if (onring[u]) len[u]=dis; else dfs2(a[u],dis+1); } void calc(int u) { vis[u]=true; for (int v=a[u];v!=u;v=a[v]) vis[v]=true; if (!len[u]) { int v=a[u],dis=1; while (v!=u&&!len[v]) {v=a[v],++dis;} if (v==u) {cnt[dis]++;return ;} u=v; } for (int co=0,i=u,j;!co||i!=u;co++,i=j) { int dis=1;j=a[i];while (!len[j]) j=a[j],++dis; if (dis<len[j]) {puts("0");exit(0);} if (dis>len[j]) (ans<<=1)%=mod; } } int main() { #ifdef Amberframe freopen("agc008e.in","r",stdin); freopen("agc008e.out","w",stdout); #endif scanf("%d",&n);fac[0]=inc[0]=ffac[0]=pw[0]=1; for (int i=1;i<=n;i++) scanf("%d",&a[i]),deg[a[i]]++; for (int i=1;i<=n;i++) if (!vis[i]) dfs(i); for (int i=1;i<=n;i++) if (onring[i]+1<deg[i]) {puts("0");return 0;} for (int i=1;i<=n;i++) if (!deg[i]) dfs2(i,0); memset(vis,0,sizeof vis); for (int i=1;i<=n;i++) if (onring[i]&&!vis[i]) calc(i); for (int i=1;i<=n;i++) ffac[i]=(LL)(2*i-1)*ffac[i-1]%mod; for (int i=1;i<=n;i++) fac[i]=(LL)fac[i-1]*i%mod; inc[n]=fpm(fac[n],mod-2); for (int i=n;i>=2;i--) inc[i-1]=(LL)inc[i]*i%mod; for (int i=1;i<=n;i++) pw[i]=(LL)pw[i-1]*2ll%mod; for (int i=1;i<=n;i++) if (i&1) { int o=cnt[i],res=0,lx=1; for (int x=0;x*2<=o;x++,lx=(LL)lx*i%mod) res=(res+(LL)comb(o,x*2)*ffac[x]%mod*(LL)lx%mod*(i>1?pw[o-2*x]:1))%mod; ans=(LL)ans*res%mod; } else { int o=cnt[i],res=0,lx=1; for (int x=0;x*2<=o;x++,lx=(LL)lx*i%mod) res=(res+(LL)comb(o,x*2)*ffac[x]%mod*(LL)lx)%mod; ans=(LL)ans*res%mod; } printf("%d",ans<0?ans+mod: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 int long long const int mod=1000000007,N=100005; bool in[N]; int vis[N],a[N],du[N],len[N],num[N],f[N]; signed main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); // ios::sync_with_stdio(false); int n,ans=1; cin>>n; for(int i=1;i<=n;i++) { // cin>>a[i]; scanf("%lld",&a[i]); du[a[i]]++; } for(int i=1;i<=n;i++) { if(!vis[i]) { int p=i; while(!vis[p]) { vis[p]=i; p=a[p]; } if(vis[p]==i) { while(!in[p]) { in[p]=true; p=a[p]; } } } } // return 0; for(int i=1;i<=n;i++) { // cout<<"W "<<i<<" "<<p<<endl; if(du[i]==0) { int p=i,res=0; while(!in[p]) { ++res; p=a[p]; // if(res>100000) break; } //cout<<"N "<<p<<" "<<in[11]<<endl; len[p]=res; } } // return 0; for(int i=1;i<=n;i++) { if((in[i]&&du[i]>2)||(in[i]==false&&du[i]>1)) { cout<<0; return 0; } } // return 0; for(int i=1;i<=n;i++) { if(in[i]) { int p=i,res=0,pos=0,sum; while(in[p]) { ++res; if(len[p]) { if(!pos) { pos=p; sum=res; } else { // cout<<"P "<<p<<" "<<len[p]<<" "<<res<<endl; if(res>len[p]) ans=ans*2%mod; else if(res<len[p]) ans=0; } res=0; } in[p]=false; p=a[p]; } if(pos) { res+=sum; // cout<<"Q "<<pos<<" "<<len[pos]<<" "<<res<<endl; if(res>len[pos]) ans=ans*2%mod; else if(res<len[pos]) ans=0; } else num[res]++; } } // cout<<ans<<endl; for(int i=1;i<=n;i++) { if(num[i]) { f[0]=1; for(int j=1;j<=num[i];j++) { if(i%2==1&&i!=1) f[j]=f[j-1]*2%mod; else f[j]=f[j-1]; if(j-2>=0) f[j]=(f[j]+(j-1)*i%mod*f[j-2]%mod)%mod; } // cout<<i<<" "<<f[num[i]]<<endl; ans=ans*f[num[i]]%mod; } } 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; const int mod = 1e9+7; int ad(int x,int y) { x+=y; return x>=mod?x-mod:x; } int sb(int x,int y) { x-=y; return x<0?x+mod:x; } int mu(int x,int y) { return 1ll*x*y%mod; } void up(int &x,int y) { x+=y; if(x>=mod)x-=mod; } int ksm(int a,int b) { int ans = 1; for(;b;b>>=1,a=mu(a,a)) if(b&1) ans=mu(ans,a); return ans; } int n; const int maxn = 2e5+5; int a[maxn],rd[maxn],from[maxn],f[maxn]; bool vis[maxn]; int ans = 1; int CR[maxn],cnt; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); rd[a[i]]++; } for(int i=1;i<=n;i++) { if(rd[i]>2) { puts("0"); return 0; } else if(rd[i]<2||vis[i]) continue; int p = i; do{ if(vis[p]) { puts("0"); return 0; } vis[p] = 1; from[a[p]] = p; p = a[p]; }while(p!=i); } for(int i=1;i<=n;i++) { if(!rd[i]) { int p = i; int l1 = 0; int l2 = 0; while(!vis[p]) vis[p] = 1 , l1++ , p = a[p]; do{ l2++; p = from[p]; }while(rd[p]!=2); if(l1==l2) continue; else if(l1<l2) ans = mu(ans,2); else { puts("0"); return 0; } } } for(int i=1;i<=n;i++) { if(!vis[i]) { int p = i; int cd = 0; do{ p = a[p]; cd++; vis[p] = 1; }while(p!=i); CR[++cnt] = cd; } } sort(CR+1,CR+1+cnt); for(int i=1,j;i<=n;i=j+1) { j = i; while(j<n&&CR[j+1]==CR[i]) j++; f[i-1] = 1; for(int k=i;k<=j;k++) { f[k] = f[k-1]; if(CR[i]>1&&(CR[i]&1)) up(f[k],f[k-1]); if(k!=i) up(f[k],mu(f[k-2],mu(k-i,CR[i]) ) ); } ans = mu(ans,f[j]); } 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 <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; typedef long long s64; inline int getint() { static char c; while ((c = getchar()) < '0' || c > '9'); int res = c - '0'; while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0'; return res; } const int MaxN = 100000; const int M = 1000000007; inline int modpow(int a, const int &n) { int res = 1; for (int i = n; i; i >>= 1) { if (i & 1) res = (s64)res * a % M; a = (s64)a * a % M; } return res; } int n; int link[MaxN + 1]; int fact[MaxN + 1]; int rfact[MaxN + 1]; int prePow[MaxN + 1]; inline int binom(const int &n, const int &m) { return (s64)fact[n] * rfact[m] % M * rfact[n - m] % M; } int dep[MaxN + 1]; int deg[MaxN + 1]; int ori[MaxN + 1]; int q_n, q[MaxN]; bool vis[MaxN + 1]; int cir_l, cir[MaxN + 1]; int sum[MaxN + 1]; int main() { cin >> n; prePow[0] = 1; for (int i = 1; i <= n; ++i) prePow[i] = prePow[i - 1] * 2 % M; fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = (s64)fact[i - 1] * i % M; rfact[n] = modpow(fact[n], M - 2); for (int i = n; i; --i) rfact[i - 1] = (s64)rfact[i] * i % M; for (int u = 1; u <= n; ++u) { int v = getint(); ++deg[link[u] = v]; ++ori[v]; } q_n = 0; for (int u = 1; u <= n; ++u) if (!deg[u]) q[q_n++] = u; for (int i = 0; i < q_n; ++i) { int u = q[i]; dep[link[u]] = dep[u] + 1; if (!--deg[link[u]]) q[q_n++] = link[u]; } for (int u = 1; u <= n; ++u) if (ori[u] - deg[u] > 1) { cout << 0 << endl; return 0; } int res = 1; for (int u = 1; u <= n; ++u) if (!vis[u] && deg[u]) { cir_l = 0; int sv = u; bool is_single = true; while (!vis[sv]) { if (dep[sv] > 0) is_single = false; vis[sv] = true; cir[++cir_l] = sv; sv = link[sv]; } if (is_single) { ++sum[cir_l]; continue; } for (int i = 1; i <= cir_l; ++i) { int sv = cir[i]; if (dep[sv] > 0) { int pos = i, len = 0; for (int k = 1; ; ++k) { pos = pos == 1 ? cir_l : pos - 1; if (dep[cir[pos]] > 0) { len = k; break; } } if (len > dep[sv]) res = res * 2 % M; else if (len < dep[sv]) { cout << 0 << endl; return 0; } } } } for (int l = 1; l <= n; ++l) if (sum[l] > 0) { s64 p = 1, r = 0; for (int i = 0; i <= sum[l] / 2; ++i) { s64 ways = (s64)p * fact[i] % M; ways = (s64)ways * binom(sum[l], i * 2) % M; ways = (s64)ways * binom(i * 2, i) % M; ways = (s64)ways * modpow(prePow[i], M - 2) % M; if (l > 1 && (l & 1)) ways = (s64)ways * prePow[sum[l] - i * 2] % M; r = (r + ways) % M; p = (s64)p * l % M; } res = (s64)res * r % M; } cout << res << 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<iostream> #include<cmath> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<map> using namespace std; vector<int>A[110000]; int n,x[110000],pd[110000],len,B[110000],num[110000],f[110000]; map<long long,int>M; const int mo=1e9+7; void dfs(int k){ if (pd[k]) return; pd[k]=1; B[++len]=k; dfs(x[k]); for (int i=0;i<A[k].size();i++) dfs(A[k][i]); } int getans(int k1,int k2,int k3){ long long st=1ll*k1*n+k2; if (M.count(st)) return M[st]; int ans=0; if (A[k1].size()+A[k2].size()>2||A[k1].size()>1||(A[k1].size()==0&&A[k2].size()==0)){ M[st]=0; return 0; } int flag=0; for (int i=0;i<A[k1].size();i++) if (A[k1][i]==k3) flag=1; for (int i=0;i<A[k2].size();i++) if (A[k2][i]==k3) flag=2; if (flag==1){ ans=1; if (A[k2].size()>0&&A[A[k2][0]].size()>0) ans=0; // cout<<"getans1 "<<k1<<" "<<k2<<" "<<k3<<" "<<ans<<endl; M[st]=ans; return ans; } if (flag==2){ int ex=0; ans=1; if (A[k1].size()>0) ex=A[k1][0]; if (ex) ans=0; if (A[k2].size()>1) ex=A[k2][0]+A[k2][1]-k3; if (ex&&A[ex].size()>0) ans=0; // cout<<"getans2 "<<k1<<" "<<k2<<" "<<k3<<" "<<ans<<endl; M[st]=ans; return ans; } if (A[k1].size()==0){ if (A[k2].size()==1){ ans=(ans+getans(0,A[k2][0],k3))%mo; } else { ans=(ans+getans(A[k2][0],A[k2][1],k3))%mo; ans=(ans+getans(A[k2][1],A[k2][0],k3))%mo; } } else if (A[k2].size()==0) ans=(ans+getans(0,A[k1][0],k3))%mo; else ans=(ans+getans(A[k1][0],A[k2][0],k3))%mo; M[st]=ans; // cout<<"getans "<<k1<<" "<<k2<<" "<<k3<<" "<<ans<<endl; return ans; } int solve(){ int flag=0,where=0; M.clear(); for (int i=1;i<=len;i++) if (A[B[i]].size()!=1) flag=1; //for (int i=1;i<=len;i++) cout<<B[i]<<" "; cout<<endl; if (flag==0){ num[len]++; return 1; } for (int i=1;i<=len;i++) if (A[B[i]].size()==2) where=B[i]; int ans=getans(0,where,where); return ans; } int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&x[i]),A[x[i]].push_back(i); for (int i=1;i<=n;i++) if (A[i].size()>2){ printf("0\n"); return 0; } int ans=1; for (int i=1;i<=n;i++) if (pd[i]==0){ len=0; dfs(i); ans=1ll*ans*solve()%mo; } //cout<<ans<<endl; for (int i=1;i<=n;i++){ f[0]=1; f[1]=1; if ((i&1)&&(i>1)) f[1]=2; for (int j=2;j<=num[i];j++) f[j]=(1ll*f[j-2]*(j-1)%mo*i+1ll*f[j-1]*(1+(i&1)-(i==1)))%mo; ans=1ll*ans*f[num[i]]%mo; } 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 <cstdlib> #include <cstring> #include <iostream> #include <algorithm> typedef long long ll ; #define rep(i, a, b) for (int i = a; i <= b; ++ i) const int N = 100005, M = N << 1, mo = 1e9 + 7 ; using namespace std ; int n, e, ter[M], nxt[M], lnk[N], opt[N], h[N], ans, d[N], q[N], f[N], deg[N] ; bool vis[N] ; void add(int x, int y) { ter[++ e] = y, nxt[e] = lnk[x], lnk[x] = e ; } bool check(int x) { vis[x] = false ; int he = 0, ta = 1 ; opt[1] = x ; for ( ; he != ta ; ) { ++ he ; int u = opt[he] ; for (int i = lnk[u] ; i; i = nxt[i]) { int v = ter[i] ; if (vis[v]) { vis[v] = false ; opt[++ ta] = v ; } } } rep(i, 1, ta) { int u = opt[i] ; for (int j = lnk[u]; j; j = nxt[j]) if (j & 1) { int v = ter[j] ; ++ deg[v] ; } } bool flg = true ; rep(i, 1, ta) { int u = opt[i] ; flg &= deg[u] > 0 ; } if (flg) { ++ h[ta] ; return true ; } int heq = 0, taq = 0 ; rep(i, 1, ta) { int u = opt[i] ; d[u] = deg[u] ; if (!d[u]) q[++ taq] = u ; if (d[u] > 2) return false ; } for ( ; heq != taq ; ) { ++ heq ; int u = q[heq] ; for (int i = lnk[u]; i; i = nxt[i]) if (i & 1) { int v = ter[i] ; -- d[v] ; if (!d[v]) q[++ taq] = v ; } } rep(i, 1, ta) { int u = opt[i] ; if (!d[u] && deg[u] > 1) return false ; } heq = taq = 0 ; rep(i, 1, ta) { int u = opt[i] ; if (!deg[u]) q[++ taq] = u, f[u] = 0 ; } for ( ; heq != taq ; ) { ++ heq ; int u = q[heq] ; for (int i = lnk[u]; i; i = nxt[i]) if (i & 1) { int v = ter[i] ; -- deg[v] ; f[v] = f[u] + 1 ; if (!deg[v]) q[++ taq] = v ; } } rep(i, 1, ta) { int u = opt[i] ; if (deg[u]) { x = u ; break ; } } d[0] = 0 ; d[++ d[0]] = x ; for ( ; ; ) { int u = d[d[0]] ; for (int i = lnk[u]; i; i = nxt[i]) if (i & 1) { int v = ter[i] ; d[++ d[0]] = v ; break ; } if (d[d[0]] == d[1]) { -- d[0] ; break ; } } int las = 0 ; rep(i, 1, d[0]) if (f[d[i]]) las = i ; rep(i, 1, d[0]) if (f[d[i]]) { int l1 = f[d[i]], l2 = las >= i ? d[0] - las + i : i - las ; if (l1 < l2) ans = ans * 2 % mo ; else if (l1 > l2) ans = 0 ; las = i ; } return true ; } inline void upd(int &x, int y) { x = (x + y) % mo ; } int main() { scanf("%d", &n) ; int x ; rep(i, 1, n) { scanf("%d", &x) ; add(i, x), add(x, i) ; } rep(i, 1, n) vis[i] = true ; ans = 1 ; rep(i, 1, n) if (vis[i]) { if (!check(i)) { printf("0\n") ; return 0 ; } } rep(L, 1, n) if (h[L]) { f[0] = 1 ; rep(i, 1, h[L]) { f[i] = 0 ; upd(f[i], f[i - 1]) ; if ((L > 1) && (L & 1)) upd(f[i], f[i - 1]) ; if (i > 1) upd(f[i], (ll) (i - 1) * f[i - 2] % mo * L % mo) ; } ans = (ll) ans * f[h[L]] % 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 <algorithm> #include <vector> const int MOD = 1000000007; int N, a[100001], v[100001], bel[100001], dep[100001], pure[100001], O = 1; std::vector < int > V[100001]; std::vector < std::vector < int > > cirs; int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) scanf("%d", a + i); for (int i = 1; i <= N; i++) if (!v[i]) { static int way[100001]; int L = 1; v[way[L] = i] = 1; while (!v[a[way[L]]]) { v[way[L + 1] = a[way[L]]] = 1; L++; } way[L + 1] = a[way[L]]; if (v[a[way[L]]] == 2) for (int i = L; i; i--) { bel[way[i]] = bel[way[i + 1]]; dep[way[i]] = dep[way[i + 1]] + 1; } else { int S = std::find(way + 1, way + L + 1, way[L + 1]) - way; for (int i = S; i <= L; i++) { bel[way[i]] = way[i]; dep[way[i]] = 0; } for (int i = S - 1; i; i--) { bel[way[i]] = bel[way[i + 1]]; dep[way[i]] = dep[way[i + 1]] + 1; } cirs.push_back({ way + S, way + L + 1 }); } for (int i = 1; i <= L; i++) v[way[i]] = 2; } for (int i = 1; i <= N; i++) V[bel[i]].push_back(dep[i]); for (int i = 1; i <= N; i++) { std::sort(V[i].begin(), V[i].end()); int S = V[i].size(); for (int j = 0; j < S; j++) if (V[i][j] != j) { puts("0"); return 0; } } for (auto &cir : cirs) { for (int &i : cir) i = V[i].size() - 1; if (int(std::count(cir.begin(), cir.end(), 0)) == int(cir.size())) pure[cir.size()]++; else { int *A = cir.data(), L = cir.size(); for (int i = 0; i < L; i++) if (A[i]) { for (int j = 1; j < A[i]; j++) if (A[(i - j % L + L) % L]) { puts("0"); return 0; } if (!A[(i - A[i] % L + L) % L]) O = (O + O) % MOD; } } } for (int i = 1; i <= N; i++) { static int f[100001]; f[0] = 1; for (int j = 1; j <= pure[i]; j++) f[j] = (f[j - 1] * (i > 1 && (i & 1) ? 2 : 1) + (long long)f[j - 2] * (j - 1) * i) % MOD; O = (long long)O * f[pure[i]] % MOD; } printf("%d\n", O); 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> const int MaxN=100010,mod=1000000007; int N,a[MaxN]; struct edge{int to;edge*next;}E[MaxN],*fir[MaxN]; int C[MaxN],pos[MaxN],dep[MaxN],cnt[MaxN],ways[MaxN]; bool vis[MaxN]; int solve(){ int res=1; for(int i=0;i<N;i++)if(!vis[i]){ int len=0,tot=0,j=i,k; for(;!vis[j];j=a[j])vis[j]=1; for(k=i;vis[k];k=a[k])vis[k]=0; for(;!vis[j];j=a[j])vis[C[len++]=j]=1; for(j=0,k=len-1;j<k;j++,k--){ int t=C[j];C[j]=C[k],C[k]=t; } for(j=0;j<len;j++){ int x=C[j],d=0; for(;;){ int c=0,t; for(edge*e=fir[x];e;e=e->next)if(!vis[e->to])c++,t=e->to; if(c>1)return 0; if(!c)break; vis[x=t]=1;d++; } if(d)pos[tot]=j,dep[tot++]=d; } if(tot){ for(j=0;j<tot;j++){ int d=(pos[(j+1)%tot]-pos[j])%len; if(d<=0)d+=len; if(d<dep[j])return 0; if(d>dep[j])res=res*2%mod; } } else cnt[len]++; } for(int i=1;i<=N;i++){ ways[0]=1; for(int j=1;j<=cnt[i];j++){ ways[j]=ways[j-1]*(i>1&&i%2?2:1)%mod; if(j>1)ways[j]=(ways[j]+(j-1ll)*ways[j-2]%mod*i)%mod; } res=1ll*res*ways[cnt[i]]%mod; } return res; } int main(){ while(scanf("%d",&N)==1){ for(int i=0;i<N;i++)scanf("%d",a+i),--a[i],fir[i]=0,vis[i]=0; for(int i=0;i<N;i++)E[i]=(edge){i,fir[a[i]]},fir[a[i]]=E+i; for(int i=N;i;i--)cnt[i]=0; printf("%d\n",solve()); } }
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; int read(){ int x=0; char ch=getchar(); while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return x; } const int N=100005,mod=1e9+7; int n,a[N],in[N],size[N],vis[N],cro[N]; int q[N],head,tail; int k[N],m; int r[N]; void add(int &x,int y){ if ((x+=y)>=mod) x-=mod; } int sc=0; int Pre(int k,int n){ return k==1?n:(k-1); } int solve(int n){ int ans=1,f=0; // printf("solve :: n = %d ",n); // for (int i=1;i<=n;i++) // printf(" %d",k[i]); // puts(""); for (int i=1;i<=n;i++){ if (k[i]==0) continue; f=1; int x=Pre(i,n); while (!k[x]) x=Pre(x,n); int t=(i-x+n)%n; if (t==0) t+=n; // printf("%d :: t = %d\n",i,t); if (t<k[i]) return 0; if (t>k[i]) ans=2LL*ans%mod; } if (!f) r[n]++; return ans; } int dp[N]; int main(){ n=read(); memset(in,0,sizeof in); for (int i=1;i<=n;i++){ in[a[i]=read()]++; size[i]=1; } head=tail=0; for (int i=1;i<=n;i++){ if (in[i]>2) return puts("0"),0; if (in[i]==0) q[++tail]=i; if (in[i]==2) cro[i]=1; } while (head!=tail){ int x=q[++head],y=a[x]; size[y]+=size[x]; if (!(--in[y])) q[++tail]=y; } for (int i=1;i<=n;i++) if (cro[i]&&!in[i]) return puts("0"),0; int ans=1; memset(vis,0,sizeof vis); for (int i=1;i<=n;i++){ if (in[i]==0||vis[i]) continue; m=0; for (int x=i;!vis[x];x=a[x]) vis[x]=1,k[++m]=size[x]-1; ans=1LL*ans*solve(m)%mod; } for (int i=1;i<=n;i++) if (r[i]>0){ dp[0]=1,dp[1]=i>1&&(i&1)?2:1; for (int j=2;j<=r[i];j++) dp[j]=(1LL*(j-1)*i%mod*dp[j-2]+dp[j-1]*(i>1&&(i&1)?2:1))%mod; ans=1LL*ans*dp[r[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; const int MaxN=1e5+5; const int Mod=1e9+7; int N,Ans=1; int A[MaxN],Deg[MaxN]; int Pre[MaxN],Cnt[MaxN],F[MaxN]; bool Vis[MaxN]; int main(){ int i,j,l,l1,l2; scanf("%d",&N); for(i=1;i<=N;i++) scanf("%d",&A[i]),Deg[A[i]]++; for(i=1;i<=N;i++){ if(Deg[i]>2){ puts("0"); return 0; } if(Deg[i]<2||Vis[i]) continue; j=i; do{ if(Vis[j]){ puts("0"); return 0; } Vis[j]=true; Pre[A[j]]=j; j=A[j]; }while(i^j); } for(i=1;i<=N;i++) if(!Deg[i]){ l1=l2=0; for(j=i;!Vis[j];j=A[j]) Vis[j]=true,l1++; do j=Pre[j],l2++; while(Deg[j]==1); if(l1>l2){ puts("0"); return 0; } if(l1<l2) Ans=2*Ans%Mod; } for(i=1;i<=N;i++) if(!Vis[i]){ l=0; for(j=i;!Vis[j];j=A[j]) Vis[j]=true,l++; Cnt[l]++; } for(i=1;i<=N;i++){ l=((i&1)&&i>1)+1; F[0]=1,F[1]=l; for(j=2;j<=Cnt[i];j++) F[j]=(1ll*F[j-2]*(j-1)*i+F[j-1]*l)%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<cstdio> #include<cstring> #include<algorithm> using namespace std; const int mxn=1000010,md=1000000007; int power(int x,int p,int num=1){ for (;p;p>>=1,x=1ll*x*x%md) if (p&1) num=1ll*num*x%md; return num; } int ans,c[mxn],stk[mxn],tp,son[mxn],vis[mxn],cnt[mxn]; int solve_tr(){ int lst=0; for (int i=1;i<=tp;++i) cnt[i]=0; for (int i=1;i<=tp;++i) for (int x=son[stk[i]];x;++cnt[i],vis[x]=1,x=son[x]); for (int i=1;i<=tp;++i) if (cnt[i]) {lst=i;break;} if (!lst) return ++c[tp],1; int ret=1; for (int i=tp;i;++lst,--i) if (cnt[i]){ if (lst>cnt[i]) ret=ret*2%md; if (lst<cnt[i]) return 0; lst=0; } return ret; } int f[mxn],pw[mxn],fct[mxn],ifc[mxn]; int cal(int x){ return (x-1ll)*x/2%md; } int solve_lp(int n,int k){ f[0]=1; bool flg=((k&1)&&k>1); int ret=pw[flg*n]; for (int i=1;i<<1<=n;++i){ f[i]=1ll*f[i-1]*cal(n-(i<<1)+2)%md*k%md; ret=(ret+1ll*f[i]*ifc[i]%md*pw[flg*(n-(i<<1))])%md; } return ret; } void init(int n){ pw[0]=1; for (int i=1;i<=n;++i) pw[i]=pw[i-1]*2%md; fct[0]=ifc[0]=1; for (int i=1;i<=n;++i) fct[i]=1ll*fct[i-1]*i%md; ifc[n]=power(fct[n],md-2); for (int i=n-1;i;--i) ifc[i]=(i+1ll)*ifc[i+1]%md; } int n,fa[mxn],stkk[mxn],tpp,tag[mxn],exist[mxn]; bool init2(){ for (int i=1;i<=n;++i) if (!vis[i]){ tpp=0; for (int x=i;x;x=fa[x]) if (vis[x]){ if (!exist[x]) break; for (;stkk[tpp+1]!=x;tag[stkk[tpp]]=1,exist[stkk[tpp--]]=0); break; } else vis[x]=exist[x]=1,stkk[++tpp]=x; for (;tpp;exist[stkk[tpp--]]=0); } memset(vis,0,sizeof(vis)); for (int i=1;i<=n;++i) if (!tag[i]){ if (son[fa[i]]) return 0; son[fa[i]]=i; } return 1; } int main() { scanf("%d",&n); init(n); for (int i=1;i<=n;++i) scanf("%d",&fa[i]); if (!init2()) return puts("0"),0; int ans=1; for (int i=1;i<=n;++i) if (tag[i]&&!vis[i]){ tp=tpp=0; for (int x=i;x;x=fa[x]) if (vis[x]){ for (;stkk[tpp+1]!=x;stk[++tp]=stkk[tpp--]); break; } else vis[x]=1,stkk[++tpp]=x; ans=1ll*ans*solve_tr()%md; } for (int i=1;i<=n;++i) ans=1ll*ans*solve_lp(c[i],i)%md; 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<bits/stdc++.h> using namespace std; #define ll long long #define f(i, x, n) for(int i = x; i < (int)n; ++i) int an = 1, cy[100001], md = 1e9 + 7, pairs[100001], fact[100001], invf[100001], inv[100001]; pair<int, int> y[100001], ln[100001]; bool vis[100001]; void no(){ printf("0\n"); exit(0); } int V; bool go(int v, int m = 0, int d = 0){ if (vis[v]){ if (v == V) { ln[m].first = d; return true; } return false; } vis[v] = true; if (!y[v].first) { ln[m].second = d; return false; } bool k = go(y[v].first, y[v].second ? v : m, d + 1); if (y[v].second)ln[m].first = d, k |= go(y[v].second, v, d + 1); return k; } inline int ch(int n, int r) { return (ll)fact[n] * invf[r] % md * invf[n - r] % md; } int pw(int x, int p){ if (!p)return 1; int t = pw(x, p >> 1); t = (ll)t * t % md; if (p & 1)t = (ll)t * x % md; return t; } inline void ad(int s, int l){ int a = 0, z = (s & 1 && s != 1) + 1; for (int i = 0; i <= l; i += 2){ a += (ll)pw(z, l - i) * ch(l, i) % md * pw(s, i >> 1) % md * pairs[i] % md; if (a >= md)a -= md; } an = (ll)an * a % md; } int main(){ int n; scanf("%d", &n); fact[0] = fact[1] = inv[1] = invf[1] = pairs[0] = invf[0] = 1; f(i, 2, n + 1){ fact[i] = (ll)fact[i - 1] * i % md; inv[i] = md - md / i * (ll)inv[md % i] % md; invf[i] = (ll)invf[i - 1] * inv[i] % md; if (!(i & 1))pairs[i] = (ll)pairs[i - 2] * (i - 1) % md; } f(i, 1, n + 1){ int t; scanf("%d", &t); if (y[t].first){ if (y[t].second)no(); y[t].second = i; } else y[t].first = i; } f(i, 1, n + 1)if (y[i].second){ V = i; if (!vis[i] && !go(i))no(); if (!ln[i].second)no(); if (ln[i].first > ln[i].second){ if ((an <<= 1) >= md)an -= md; } else if (ln[i].first < ln[i].second)no(); } f(i, 1, n + 1)if (!vis[i]){ V = i; if (go(i, i))++cy[ln[i].first]; } f(i, 1, n + 1)if (cy[i])ad(i, cy[i]); printf("%d\n", an); }
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 <algorithm> #include <iostream> #include <cstdio> using namespace std; typedef long long i64; class no_solution: public std::exception { public: no_solution() { } const char * what() const noexcept { return "no solution under such constrains"; } }; 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]; void findCycle() { for (int i = 1; i <= N; i++) { if (degree[i] > 2) throw no_solution(); if (degree[i] != 2 || vis[i]) continue; int cur = i; do { if (vis[cur]) throw no_solution(); 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) throw no_solution(); } } 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]]++; try { findCycle(); processCircleBasedTree(); countCycle(); 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 ; } catch (const std::exception & x) { cout << "0" << endl; cerr << x.what() << 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 <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 st[N<<1], deg[N], d[N], n, vis[N], a[N], tot, num[N], l[N], ans=1, fac[N], inv[N]; typedef long long LL; int power(int x, int y){ int an = 1; for(; y; y >>= 1, x = (LL)x * x % mod) if(y & 1) an = (LL)an * x % mod; return an; } int C(int x, int y){ if(x < 0 || y < 0 || x < y) return 0; return (LL)fac[x] * inv[y] % mod * inv[x - y] % mod; } void renew(int &x, const int y){ x += y; if(x < 0) x += mod; if(x >= mod) x -= mod; } void init(){ fac[0] = inv[0] = fac[1] = inv[1] = 1; rep(i, 2, n){ fac[i] = (LL)fac[i - 1] * i % mod; inv[i] = power(fac[i], mod - 2); } } 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); 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]) 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++; l[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 &= !l[u]; vis[u] = 1; u = a[u]; }while(u != i); if(cu){ num[tot] ++; res = 1; }else{ res = getdp(); } ans = (LL)ans * res % mod; } rep(i, 1, n) if(num[i]) { int tot = num[i]; int res = 0; for(int x = 0; x <= tot; x += 2){ int cb = 1; cb = (LL)cb * power(i, (x >> 1)) % mod; cb = (LL)cb * C(tot, x) % mod; cb = (LL)cb * fac[x] % mod; cb = (LL)cb * inv[x >> 1] % mod; cb = (LL)cb * power((mod + 1) >> 1, x >> 1) % mod; if((i > 1) && (i & 1)){ cb = (LL)cb * power(2, tot - x) % mod; } renew(res, cb); } ans = (LL)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 <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 { fu.clear(); //if(l[i]) fu.pb(mp(l[i], 0)); //for(int p=1, x=a[i]; x!=i; x=a[x]) if(l[x]) fu.pb(mp(l[x], p)); 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; } // 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