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
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.*; public class MainClass { public static void main(String[] args)throws IOException { Reader in = new Reader(); int t = in.nextInt(); StringBuilder stringBuilder = new StringBuilder(); while (t-- > 0) { int n = in.nextInt(); TreeSet<Integer> h = new TreeSet<>(); int[] B = new int[n]; int[] A = new int[2 * n]; for (int i=1;i<=2 * n;i++) h.add(i); for (int i=0;i<n;i++) { A[2 * i] = B[i] = in.nextInt(); h.remove(B[i]); } boolean flag = true; for (int i=0;i<n;i++) { int x = A[2 * i]; if (h.higher(x) == null) { flag = false; break; } else { int jj = h.higher(x); A[2 * i + 1] = jj; h.remove(jj); } } if (!flag) stringBuilder.append("-1\n"); else { for (int i=0;i<2 * n;i++) stringBuilder.append(A[i]).append(" "); stringBuilder.append("\n"); } } System.out.println(stringBuilder); } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub FastReader s = new FastReader(); int tc = s.nextInt(); while(tc-- > 0) { int n = s.nextInt(); int[] input = new int[n]; boolean[] taken = new boolean[2*n + 1]; for(int i = 0;i < n;i++) { input[i] = s.nextInt(); taken[input[i]] = true; } ArrayList<Integer> arr = new ArrayList<>(); int i = 0; for(i = 0;i < n;i++) { int val = nextGreater(input[i] , taken); if(val == -1) { System.out.println("-1"); break; } arr.add(input[i]); arr.add(val); } if(i == n) { for(int j : arr) System.out.print(j + " "); System.out.println(); } } } public static int nextGreater(int val , boolean[] taken) { int n = taken.length/2; for(int i = val + 1;i <= 2*n;i++) { if(!taken[i]) { taken[i] = true; return i; } } return -1; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
//package cf; import java.io.*; import java.util.*; public class T_class { static int m=2147483647;///this is 2^32 -1 (is prime number) and should be used for hashing static int p=1000000007; static int max=(int)2e3+10; static boolean prime[]=new boolean[max+5]; static int dp[][]=new int[(int)max][max]; static List<Integer> total_prime=new ArrayList<>(); public static void main(String[] args) throws Exception { BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(FileDescriptor.out), "ASCII"), 512); FastReader sc=new FastReader(); int t=sc.nextInt(); StringBuilder sb2=new StringBuilder(); while(t-- >0) { int n=sc.nextInt(); int ar[]=new int[n]; TreeSet<Integer> consume=new TreeSet<>(); for(int i=0;i<n;i++) { ar[i]=sc.nextInt(); consume.add(ar[i]); } for(int i=1;i<=2*n;i++) { if(consume.contains(i)) consume.remove(i); else consume.add(i); } StringBuilder sb=new StringBuilder(); for(int i:ar) { sb.append(i+" "); int nxt=-1; for(int j:consume) { if(j>i) { nxt=j; consume.remove(nxt); break; } } if(nxt==-1) n=-1; else sb.append(nxt+" "); } if(n==-1) sb2.append("-1\n"); else sb2.append(sb.toString()+"\n"); } System.out.println(sb2.toString()); out.flush(); } public static long gethash(long pow[], long hash[], int l, int r) { return (hash[r+1] - (hash[l ] * pow[r - l +1]) % m + m) % m; } private static String long_pre(char[] ch) { int n=ch.length; char cpy[]=new char[n]; for(int i=n-1;i>=0;i--) { cpy[n-1-i]=ch[i]; } long pow[] = new long[n + 1]; long hash[] = new long[n + 1]; pow[0] = 1; int p = 31; for (int i = 1; i <= n; i++) { pow[i] = (pow[i - 1] % m * p % m) % m; } for (int i = 1; i <= n; i++) { hash[i] = (hash[i - 1] * p % m + (ch[i - 1]) % m) % m; } long pow1[] = new long[n + 1]; long hash1[] = new long[n + 1]; pow1[0] = 1; //int p = 31; for (int i = 1; i <= n; i++) { pow1[i] = (pow1[i - 1] % m * p % m) % m; } for (int i = 1; i <= n; i++) { hash1[i] = (hash1[i - 1] * p % m + (cpy[i - 1]) % m) % m; } int l=0,r=n-1,lst=0; for(int i=n-1;i>=1;i--) { int mid=i%2; int midp=i/2; if(mid==0&&gethash(pow,hash,0,midp-1)==gethash(pow1,hash1,n-1-i,n-1-(midp+1))) { lst=i; break; } else if(mid==1&&gethash(pow,hash,0,midp)==gethash(pow1,hash1,n-1-i,n-1-(midp+1))) { lst=i; break; } } StringBuilder sb=new StringBuilder(); for(int i=0;i<=lst;i++) { sb.append(ch[i]); } return sb.toString(); } private static int Solve(int n, int l, int r,int h, int i, int s, int[] ar) { if(i==n) return 0; if(dp[i][s]!=-1) return dp[i][s]; int ans=Math.max(con((s+ar[i])%h,l,r)+Solve(n,l,r, h,i+1,(s+ar[i])%h,ar) ,con((s+ar[i]-1)%h,l,r)+Solve(n,l,r,h,i+1,(s+ar[i]-1+h)%h,ar)); dp[i][s]=ans; return dp[i][s]; } private static int con(int i,int l,int r) { if(l<=i&&i<=r) return 1; else return 0; } static long childs[]=new long[max]; static long tot=0; public static void dfs_val(List<Integer> adj[],int u,int p,long val) { int in_max=-1; val=val+childs[u]+1; for(int v:adj[u]) { if(v!=p) { in_max=v; dfs_val(adj,v,p,val); } } if(in_max==-1) { tot=Math.max(tot,val); } } public static void dfs(List<Integer> adj[],int u,int p) { for(int v:adj[u]) { if(v!=p) { childs[u]++; dfs(adj,v,u); childs[u]+=childs[v]; } } } public static int ans(int ar[],int i,int n,int t) { if(i>=n) return 0; if(t>=410) return 200000; if(dp[i][t]!=-1) return dp[i][t]; //System.out.println(t+" "+i); int cur=Math.min(Math.abs(ar[i]-t)+ans(ar,i+1,n,t+1),ans(ar,i,n,t+1)); dp[i][t]=cur; return dp[i][t]; } private static long ncr(int n, int r, long[] fact) { if(r>n||n < 0 || r < 0) return 0; if(n==1||n==0) return 1; // System.out.println(n+" c "+r); return (((fact[n]*pow_inverse(fact[n-r],p))%p)*pow_inverse(fact[r],p))%p; } static class BIT { long[] tree; public BIT(int size) { tree = new long[size + 1]; } public long sum(int i) { long ans = 0; while (i > 0) { ans += tree[i]; i -= i & -i; } return ans; } public long query(int i, int j) { return sum(j) - sum(i - 1); } public void add(int i, long val) { while (i < tree.length) { tree[i] += val; i += i & -i; } } public void set(int i, long val) { add(i, val - query(i, i)); } } static class vert implements Comparator<vert> { int v; TreeSet<Integer> set; public vert(int v, TreeSet<Integer> set) { this.v = v; this.set = set; } @Override public int compare(vert o1, vert o2) { return -(o1.set.size()-o2.set.size()); } } public static long pow(long a,long b,long m) { long r=1; while(b!=1) { if(b%2!=0) r=(r*a)%m; b=b>>1; a=(a*a)%m; } return (r*a)%m; } public static long pow_inverse(long a,long m) { return pow(a,m-2,m); }public static long gcd(long a,long b) { if(b>a) { a=a^b; b=a^b; a=a^b; } if(b==0) return a; return gcd(b,a%b); } public static void extgcd(long a,long b) {/* if(a==0) { x=0; y=1; d=b; // return ; } else { extgcd(b%a,a); long t=y; y=x; x=t-(b/a)*x; }*/ } public static void seive() { for(int i=0;i<max;i++) prime[i] = true; for(int p = 2; p*p <=max; p++) { // If prime[p] is not changed, then it is a prime if(prime[p] == true) { // Update all multiples of p for(int i = p*p; i <=max; i += p) prime[i] = false; } } // Print all prime numbers for(int i = 2; i <= max; i++) { if(prime[i] == true) { total_prime.add(i); } } } public static boolean union(int a, int b, int p[], int r[]){ // add your code here int pa=find(a,p,r); int pb=find(b,p,r); if(pa==pb) return true; if(pa!=pb) { if(r[pa]<r[pb]) { p[pa]=pb; } else if(r[pa]>r[pb]) { p[pb]=pa; } else { p[pa]=pb; r[pb]++; } } return false; } public static int find(int x,int p[],int r[]) { if(p[x]==x) return x; p[x]=find(p[x],p,r); return p[x]; } public static int binary_Search_upper(Integer[] ar, Integer x,int l,int r) { int res=-1,f=0; while(l<=r) { int mid=(l+r)>>1; if(ar[mid]==x) { f=1; res=mid; l=mid+1; } else if(ar[mid]>x) { res=res!=-1&&ar[res]==x?res:mid; r=mid-1; } else { l=mid+1; } } return f==1?res:res+1; } public static int binary_Search_lower(int[] ar, long x, int l, int r) { int res=0,f=0; //int l=0;int r=ar.length-1; /*public static int binary(Long[] ar, int l, int r, long v) { int n=r; int m=(l+r)/2; int re=-1; while(l<=r) { if(ar[m]>=v) { re=m; r=m-1; } else if(ar[m]<v) { l=m+1; } m=(l+r)/2; } if(l<=n&&ar[l]==v) return l; return re; }*/ // System.out.println(Arrays.toString(ar)+" "+x); while(l<=r) { int mid=(l+r)>>1; // System.out.println(l+" "+r+" "+mid+" = "+ar[mid]); if(ar[mid]==x) { f=1; res=mid; r=mid-1; } else if(ar[mid]>x) { r=mid-1; } else { res=res!=0&&ar[res]==x?res:mid; l=mid+1; } } // System.out.println(res); return f==1?res:res+1; } public static int binary_Search_lower_suf(int[] ar, int x,int l,int r) { int res=-1,f=0; //int l=0;int r=ar.length-1; while(l<=r) { int mid=(l+r)>>1; if(ar[mid]==x) { f=1; res=mid; r=mid-1; } else if(ar[mid]<x) { r=mid-1; } else { // res=mid; l=mid+1; } } return f==0?res+1:res; } int bit[]=new int[(int)1e6]; public void update(int n,int val,int i) { i++; while(i<n) { bit[i]+=val; i+=(i)&(-i); } } public long query(int n,int i) { i++; long sum=0; while(i>0) { sum+=bit[i]; i-=(i)&(-i); } return sum; } /////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////// static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for __ in range(int(input())): n = int(input()) # a = [-1]*(n+1) b = [-1]*(2*n) a = [int(x) for x in input().split()] m1 = {} for i in range(n): m1[a[i]] = 1 b[2*i] = a[i] for i in range(1,2*n,2): k = a[i//2] while k in m1: k += 1 b[i] = k m1[k]=1 #print(m1) f=0 for x in b: if x > 2*n: f=1 break if f == 1: print(-1) else: for x in b: print(x,end=' ') print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int N = 1e5; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc, ca = 0; cin >> tc; while (tc--) { int n, el; vector<int> b; vector<int> baki; map<int, int> m1; map<int, int> m2; cin >> n; for (int i = 0; i < n; i++) { cin >> el; m1[el] = 1; b.push_back(el); } for (int i = 1; i <= n + n; i++) { if (!m1[i]) baki.push_back(i); } m1.clear(); bool ok = false; vector<int> a; for (int i = 0; i < n; i++) { int p1 = b[i]; for (int j = 0; j < n; j++) { if (baki[j] && p1 <= baki[j]) { a.push_back(p1); a.push_back(baki[j]); baki[j] = 0; break; } } } if (a.size() != n + n) cout << "-1" << '\n'; else { for (int i = 0; i < n + n; i++) { cout << a[i]; if (i != (n + n) - 1) cout << ' '; } cout << '\n'; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; public class realfast implements Runnable { private static final int INF = (int) 1e9; long in= (long)Math.pow(10,9); public void solve() throws IOException { int t = readInt(); for(int f =0;f<t;f++) { int n = readInt(); int arr[]= new int [n]; boolean check = true; boolean map[]= new boolean[2*n+1]; for(int i =0;i<n;i++) { arr[i]= readInt(); if(map[arr[i]]) { check=false; } map[arr[i]]=true; } int fin[]= new int[2*n]; for(int i=0;i<n;i++) { fin[2*i]=arr[i]; } for(int i=0;i<n;i++) { int val= arr[i]; if(val==2*n) { check=false; break; } for(int j=val+1;j<=2*n;j++) { if(!map[j]) { fin[2*i+1]= j; map[j]=true; break; } if(j==2*n) { check=false; break; } } } if(!check) out.println(-1); else { for(int i=0;i<2*n;i++) out.print(fin[i]+" "); out.println(); } } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new Thread(null, new realfast(), "", 128 * (1L << 20)).start(); } private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter out; @Override public void run() { try { if (ONLINE_JUDGE || !new File("input.txt").exists()) { reader = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { reader = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } solve(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (IOException e) { // nothing } out.close(); } } private String readString() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } @SuppressWarnings("unused") private int readInt() throws IOException { return Integer.parseInt(readString()); } @SuppressWarnings("unused") private long readLong() throws IOException { return Long.parseLong(readString()); } @SuppressWarnings("unused") private double readDouble() throws IOException { return Double.parseDouble(readString()); } } class edge implements Comparable<edge>{ int u ; int v ; int val; edge(int u1, int v1 , int val1) { this.u=u1; this.v=v1; this.val=val1; } public int compareTo(edge e) { return this.val-e.val; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys input = sys.stdin.readline t = int(input()) import bisect for _ in range(t): n = int(input()) a = list(map(int,input().split())) b = [0 for _ in range(2*n)] for x in a: b[x-1] = 1 c = [] for i in range(2*n): if b[i] == 0: c.append(i+1) try: d = [] for x in a: bi = bisect.bisect_left(c,x) tmp = c[bi] del c[bi] d.append(tmp) #print(a) #print(d) ans = '' for i in range(n): ans += str(a[i]) ans += ' ' ans += str(d[i]) if i != n-1: ans += ' ' print(ans) except: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.HashSet; import java.util.InputMismatchException; import java.util.TreeSet; /** * @author Mubtasim Shahriar */ public class ResPer { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); int t = sc.nextInt(); // int t = 1; while(t--!=0) { solver.solve(sc, out); } out.close(); } static class Solver { public void solve(InputReader sc, PrintWriter out) { int n = sc.nextInt(); int[] arr = new int[n+1]; TreeSet<Integer> ts = new TreeSet(); for(int i = 1; i <= 2*n; i++) ts.add(i); HashSet<Integer> hs = new HashSet(); for(int i = 1; i <= n; i++) { arr[i] = sc.nextInt(); if(hs.contains(arr[i])) { out.println(-1); return; } hs.add(arr[i]); ts.remove(arr[i]); } int[] ans = new int[2*n+1]; for(int i = 1; i <= n; i++) { // if(ts.contains(arr[i])) { // ans[2*i-1] = arr[i]; //// ts.remove(arr[i]); // if(ts.ceiling(arr[i])!=null) { // int now = ts.ceiling(arr[i]); // ans[2*i] = now; // ts.remove(now); // } else { // out.println(-1); // return; // } // } else { // out.println(-1); // return; // } ans[2*i-1] = arr[i]; // ts.remove(arr[i]); if(ts.ceiling(arr[i])!=null) { int now = ts.ceiling(arr[i]); ans[2*i] = now; ts.remove(now); } else { out.println(-1); return; } } for(int i = 1; i <= 2*n; i++) { out.print(ans[i] + " "); } out.println(); } } static class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] nextIntArray(int n){ int[] array=new int[n]; for(int i=0;i<n;++i)array[i]=nextInt(); return array; } public int[] nextSortedIntArray(int n){ int array[]=nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n){ int[] array=new int[n]; array[0]=nextInt(); for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt(); return array; } public long[] nextLongArray(int n){ long[] array=new long[n]; for(int i=0;i<n;++i)array[i]=nextLong(); return array; } public long[] nextSumLongArray(int n){ long[] array=new long[n]; array[0]=nextInt(); for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt(); return array; } public long[] nextSortedLongArray(int n){ long array[]=nextLongArray(n); Arrays.sort(array); return array; } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n = int(input()) l = [*map(int,input().split())] s = set() s1 = set(l) for i in range(1,2*n + 1): if(i not in s1): s.add(i) ans = [] for i in range(n): ans.append(l[i]) for j in range(l[i] + 1, 2 * n + 1,1): if(j in s): ans.append(j) s.remove(j) break if(len(ans) == 2 * n): print(*ans) else: print("-1")
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; long long int fast_expo(long long int x, long long int p) { if (p == 0) return 1; else if (p % 2 == 0) { long long int t = fast_expo(x, p / 2) % 1000000007; return (t * t) % 1000000007; } else return (x * (fast_expo(x, p - 1)) % 1000000007) % 1000000007; } int main() { int t, i; cin >> t; while (t--) { int n; cin >> n; map<int, int> mp; vector<int> b(n), a(2 * n); for (i = 0; i < n; i++) { cin >> b[i]; mp[b[i]] = 1; } vector<int> v; for (i = 1; i <= 2 * n; i++) { if (!mp[i]) v.push_back(i); } int j = 0; for (i = 0; i < 2 * n; i += 2) { a[i] = b[j]; j++; } int flag = 0; sort(v.begin(), v.end()); for (i = 0; i < 2 * n - 1; i += 2) { auto it = lower_bound(v.begin(), v.end(), a[i]) - v.begin(); auto it1 = lower_bound(v.begin(), v.end(), a[i]); if (it1 != v.end()) { a[i + 1] = v[it]; if (v.size() > 0) v.erase(it1); } else { flag = 1; break; } } if (flag) cout << -1; else { for (i = 0; i < 2 * n; i++) cout << a[i] << " "; } cout << endl; } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) for _ in range(t): n=int(input()) b=list(map(int, input().split() )) s=set(b) ans=[] possible=True for x in b: y=x+1 while y<=2*n: if y not in s: break y=y+1 if y>2*n: possible=False break s.add(y) ans.append(x) ans.append(y) if possible: print(" ".join(map(str, ans))) else: print("-1")
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.*; public class MyClass { public static void main(String args[]) { FastReader sc = new FastReader(); //For Fast IO //func f = new func(); //Call func for swap , permute, upper bound and for sort. StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int b[] = new int[n]; int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for(int i=0;i<n;i++){ b[i]=sc.nextInt(); if(max<b[i]){ max=b[i]; } if(min>b[i]){ min=b[i]; } } if(max==2*n){ System.out.println(-1); continue; } else if(min!=1){ System.out.println(-1); continue; } else{ int arr[] = new int[2*n]; int count[] = new int[2*n+1]; int ind=0; for(int i=0;i<n;i++){ arr[ind]=b[i]; count[arr[ind]]=1; ind+=2; } int f=0; for(int i=0;i<2*n;i++){ if(arr[i]>0){continue;} else{ int x = arr[i-1]+1; while(x<=2*n && count[x]==1){ x++; } if(x>2*n){ x=1; while(x<=2*n && count[x]==1){ x++; } if(x<arr[i-1]){ f=1; break; } arr[i]=x; count[x]=1; } else{ arr[i]=x; count[x]=1; } } } if(f==1){ System.out.println(-1); continue; } for(int i=0;i<2*n;i++){ System.out.print(arr[i]+" "); } System.out.println(); } } } } class Pair{ int x; int y; Pair(int i,int j){ x=i; y=j; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair key = (Pair) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31*result + y; return result; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class func{ static ArrayList<String> al = new ArrayList<>(); public static void sort(long[] arr) { int n = arr.length, mid, h, s, l, i, j, k; long[] res = new long[n]; for (s = 1; s < n; s <<= 1) { for (l = 0; l < n - 1; l += (s << 1)) { h = Math.min(l + (s << 1) - 1, n - 1); mid = Math.min(l + s - 1, n - 1); i = l; j = mid + 1; k = l; while (i <= mid && j <= h) res[k++] = (arr[i] <= arr[j] ? arr[i++] : arr[j++]); while (i <= mid) res[k++] = arr[i++]; while (j <= h) res[k++] = arr[j++]; for (k = l; k <= h; k++) arr[k] = res[k]; } } } public static void permute(char a[] , int i , int n){ if(i==n-1){ String s = new String(a); al.add(s); // al stores all permutations of string. return; } for(int j=i;j<n;j++){ swap(a,i,j); permute(a,i+1,n); swap(a,i,j); } } public static void swap(char a[],int i, int j){ char temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void swap(int a[],int i, int j){ int temp = a[i]; a[i] = a[j]; a[j] = temp; } //-It returns index of first element which is strictly greater than searched value. public static int upperBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; if (value >= array[mid]) { low = mid + 1; } else { high = mid; } } return low; } /*If searched element doesn't exist function returns index of first element which is bigger than searched value.<br> * -If searched element is bigger than any array element function returns first index after last element.<br> * -If searched element is lower than any array element function returns index of first element.<br> * -If there are many values equals searched value function returns first occurrence.<br>*/ public static int lowerBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; if (value <= array[mid]) { high = mid; } else { low = mid + 1; } } return low; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for nt in range(int(input())): n=int(input()) b=list(map(int,input().split())) d=set(b) ansflag=0 ans=[] d1={} for i in range(n): ans.append(b[i]) k=1 flag=0 #print (ans) while True: if (b[i]+k)>2*n: flag=1 break if b[i]+k not in d and b[i]+k not in d1: ans.append(b[i]+k) d1[b[i]+k]=1 break else: k+=1 if flag==1: print (-1) ansflag=1 break if ansflag!=1: print (*ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.math.*; public class Codeforces { public static void main(String[] args) throws IOException { Scanner s = new Scanner(System.in); int t=s.nextInt(); for(int i=0;i<t;i++) { int n=s.nextInt(); int[] arr=new int[n]; HashSet<Integer> set=new HashSet<>(); for(int j=0;j<n;j++) { arr[j]=s.nextInt(); set.add(arr[j]); } int flag=0; int[] ans=new int[2*n]; for(int j=0;j<n;j++) { int val=arr[j]; int minReq=-1; for(int k=val+1;k<=2*n;k++) { if(!set.contains(k)) { minReq=k; break; } } // System.out.println(val+" "+minReq); if(minReq==-1) { flag=1; break; } { ans[2*j]=arr[j]; ans[(2*j)+1]=minReq; set.add(minReq); } } if(flag==1) { // for(int j:ans) // { // System.out.print(j+" "); // } System.out.println("-1"); } else { for(int j:ans) { System.out.print(j+" "); } System.out.println(); } } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; unordered_set<int> us; for (int i = 0; i < n; i++) { cin >> arr[i]; us.insert(arr[i]); } bool flag = false; multiset<int> m; for (int i = 1; i <= 2 * n; i++) { if (us.find(i) == us.end()) { m.insert(i); } } int ans[2 * n + 1]; for (int i = 1; i <= n; i++) { int temp = arr[i - 1]; if (m.upper_bound(temp) == m.end()) { flag = true; break; } else { auto y = m.upper_bound(temp); ans[2 * i - 1] = temp; ans[2 * i] = (*y); m.erase(*y); } } if (flag == true) { cout << -1 << "\n"; } else { for (int i = 1; i <= 2 * n; i++) { cout << ans[i] << " "; } cout << "\n"; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys import math #to read string get_string = lambda: sys.stdin.readline().strip() #to read list of integers get_list = lambda: list( map(int,sys.stdin.readline().strip().split()) ) #to read integers get_int = lambda: int(sys.stdin.readline()) #to print fast #pt = lambda x: sys.stdout.write(str(x)+'\n') #--------------------------------WhiteHat010--------------------------------------# for _ in range(get_int()): n = get_int() lst1 = get_list() lst2 = [i for i in range(1,2*n+1) if i not in lst1] res = [0]*(2*n) valid = True for i in range(n): a = lst1[i] b = -1 for j in range(n): if lst2[j] > a: b = lst2[j] lst2[j] = 0 break if b == -1: valid = False break res[2*i],res[2*i+1] = a,b if valid: print(*res) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n = int(input()) arr = list(map(int,input().split())) ans = [0 for i in range(2*n)] visited = [False for i in range(2*n)] ind = 0 for i in range(0,2*n,2): ans[i] = arr[ind] visited[arr[ind]-1]=True ind+=1 # print(visited) for i in range(1,2*n,2): f = 0 cur = ans[i-1] # print(cur,visited) for j in range(cur,2*n): if visited[j]==False: visited[j] = True f = 1 ans[i] = j+1 break if f==0: break if f==0: # print(ans) print(-1) else: print(*ans) # print(cur,visited)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
whatever=int(input()) for i in range (whatever) : k=int(input()) bs=list(map(int,input().split())) a=[0]*2*k avail=[] for t in range (1,(2*k)+1) : if t not in bs : avail.append(t) avail=sorted(avail) for l in range (0,(2*k),2) : a[l]=bs[int(l/2)] flag=0 for hello in avail : if hello>bs[int(l/2)] and flag==0 : a[l+1]=hello avail.remove(hello) flag=1 if 0 in a : print (-1) else : print (*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int b[105], vis[205]; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int pd = 0; memset(vis, 0, sizeof(vis)); int maxx = 0; for (int i = 0; i < n; i++) { scanf("%d", &b[i]); maxx = max(maxx, b[i]); vis[b[i]] = 1; } if (maxx >= 2 * n) pd = 1; if (pd) { printf("-1\n"); continue; } vector<int> e; int pos = 1; for (int i = 0; i < n; i++) { pos = 1; while (pos <= b[i] || vis[pos] == 1) pos++; if (pos > 2 * n) break; vis[pos] = 1; e.push_back(pos); } if (e.size() != n) { printf("-1\n"); continue; } for (int i = 0; i < n; i++) printf("%d %d ", b[i], e[i]); printf("\n"); } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import bisect import collections t = int(input()) for test in range(t): canDo = True n = int(input()) # elements in sequence b = [] a = list(range(1,2*n+1)) for i in input().split(): b.append(int(i)) a.remove(int(i)) a.sort() c = [] for i, elem in enumerate(b): placeholder = bisect.bisect_left(a, elem) if placeholder == len(a): canDo = False break else: c.append(str(b[i])) c.append(str(a[placeholder])) del a[placeholder] if not canDo: print(-1) continue print(' '.join(c))
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in " "*int(input()): a=int(input()) if a==1: z=int(input()) if z==1:print("1 2") else:print(-1) continue b=list(map(int,input().split())) if 1 not in b or 2*a in b:print(-1);continue c=[ i for i in range(1,(2*a)+1) if i not in b] r=[] for i in range(a): for j in c: if j>b[i]:r+=[j];c.remove(j);break if len(r)!=a:print(-1) else: for i in range(a): print(b[i],r[i],end=" ") print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.io.*; import java.math.*; public class Main7 { static class Pair { int x; int y; int z; Pair(int x, int y,int z) { this.x=x; this.y=y; this.z=z; } } static int mod=1000000007; public static int[] sort(int[] a) { int n=a.length; ArrayList<Integer> ar=new ArrayList<>(); for(int i=0;i<a.length;i++) { ar.add(a[i]); } Collections.sort(ar); for(int i=0;i<n;i++) { a[i]=ar.get(i); } return a; } static ArrayList<ArrayList<Integer>> graph; static public void main(String args[])throws IOException { int tt=i(); StringBuilder sb=new StringBuilder(); while(tt-->0) { int n=i(); int[] b=new int[n]; for(int i=0;i<n;i++) { b[i]=i(); } int flag=0; TreeSet<Integer> ts=new TreeSet<>(); for(int i=1;i<=2*n;i++) { int cc=0; for(int j=0;j<n;j++) { if(b[j]!=i) { cc++; } } if(cc==n) ts.add(i); } int[] a=new int[2*n]; for(int i=0;i<n;i++) { int num=b[i]; Integer num1=ts.higher(num); if(num1==null) { flag=1; break; } else { a[2*i]=Math.min((int)num1,num); a[2*i+1]=Math.max(num,(int)num1); ts.remove(num1); } } if(flag==1) { pln("-1"); } else { for(int i=0;i<2*n;i++) { System.out.print(a[i]+" "); } pln(""); } } System.out.print(sb.toString()); } /**/ static InputReader in=new InputReader(System.in); static OutputWriter out=new OutputWriter(System.out); public static long l() { String s=in.String(); return Long.parseLong(s); } public static void pln(String value) { System.out.println(value); } public static int i() { return in.Int(); } public static String s() { return in.String(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars== -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.Int(); return array; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; vector<long long> vprime; void SieveOfEratosthenes(int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int p = 2; p <= n; p++) if (prime[p]) vprime.push_back(p); } int countFreq(string &pat, string &txt) { int M = pat.length(); int N = txt.length(); int res = 0; for (int i = 0; i <= N - M; i++) { int j; for (j = 0; j < M; j++) if (txt[i + j] != pat[j]) break; if (j == M) { res++; j = 0; } } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long a, b, c, d, e, g, h, k, l, m, n, t, f, f1 = 0, flag2 = 0, flag = 0, flag1 = 0, co = 0, co1 = 0, co2 = 0, sum = 0, sum1 = 0, ma = 0, ma1 = 0, ma2 = 0, mi = 100000000000, mi1 = 100000000000, mi2 = 100, mi3 = 1000; long long co3 = 0, co4 = 0, co5 = 0, co6 = 0, co7 = 0, co8 = 0, mul = 1, sum2 = 0; long long arr[100001] = {0}, arr1[100001] = {0}; long long a1, a2, b1, b2, c1, c2, a3, a4, b3, b4, b5, b6, m1, m2, k1, l1, m3, m4, d1, d2; double pi = 2 * acos(0.0); char ch; string str, str1, str2, str3, str4, str5; set<long long> s1; set<long long> s2; set<long long> s3; stack<long long> st; int x[] = {1, 0, 0, -1}; int y[] = {0, 1, -1, 0}; cin >> t; while (t--) { cin >> n; vector<long long> v(n); vector<long long> v1, v2, v3; map<long long, long long> mp, mp1; for (int i = 0; i < n; i++) { cin >> v[i]; mp[v[i]] = 1; v1.push_back(v[i]); } for (int i = 1; i <= 2 * n; i++) { if (mp[i] == 0) { v2.push_back(i); } } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); for (int i = 0; i < v1.size(); i++) { mp1[v1[i]] = v2[i]; } flag = 0; for (int i = 0; i < v.size(); i++) { if (mp1[v[i]] > v[i]) { v3.push_back(v[i]); v3.push_back(mp1[v[i]]); } else { flag = 1; break; } } if (flag == 1) { cout << -1 << endl; continue; } for (int i = 1; i < v3.size(); i += 2) { for (int j = i; j < v3.size(); j += 2) { if (v3[i] > v3[j]) { if (v3[j] > v3[i - 1]) { k = v3[i]; v3[i] = v3[j]; v3[j] = k; } } } } for (int i = 0; i < v3.size(); i++) cout << v3[i] << " "; cout << endl; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.*; public class ProblemC { public static InputStream inputStream = System.in; public static OutputStream outputStream = System.out; public static void main(String[] args) { Scanner scanner = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int t = scanner.nextInt(); for (int p = 0; p < t; p++) { int n = scanner.nextInt(); List<Integer> list = new ArrayList<>(); List<Integer> ans = new ArrayList<>(); List<Integer> aList = getAList(n); for (int i = 0; i < n; i++) { int x = scanner.nextInt(); list.add(x); aList.remove(new Integer(x)); } boolean cant = false; for (int i = 0; i < n; i++) { int x = list.get(i); int y = getSmallest(x, aList); if (y == -1) { out.println(-1); cant = true; break; } ans.add(x); ans.add(y); } if (!cant) { for (int x : ans) { out.print(x + " "); } out.println(); } out.flush(); } out.flush(); } private static int getSmallest(int x, List<Integer> list) { for (int a : list) { if (a > x) { list.remove(new Integer(a)); return a; } } return -1; } private static List<Integer> getAList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; i <= 2 * n; i++) { list.add(i); } return list; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) for _ in range(t): n=int(input()) b=list(map(int,input().split())) s=set(range(2 * n + 1)) s.remove(0) l=[201]*(2*n) flag = True for i in range(0, 2*n, 2): l[i] = b[i//2] s.remove(l[i]) for i in range(1, 2*n, 2): for num in s: if num > l[i - 1] and num < l[i]: l[i] = num if (l[i] == 201): print(-1) flag = False break s.remove(l[i]) if flag: print(*l)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> const int N = 1e6; const double eps = 0.00000001; const long long mod = 1e9 + 7; using namespace std; long long read() { long long 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 a[1000], b[1000]; bool vis[1000]; int main() { int t = read(); while (t--) { memset(vis, 0, sizeof vis); memset(b, 0, sizeof b); int n = read(); bool r = 1; for (int i = 1; i <= n; ++i) { a[i] = read(), vis[a[i]] = 1; if (a[i] == 2 * n) r = 0; } if (r == 0) { puts("-1"); continue; } for (int i = 1; i <= n; ++i) { b[2 * i - 1] = a[i]; r = 1; for (int j = a[i]; j <= 2 * n; ++j) if (!vis[j]) { b[2 * i] = j; vis[j] = 1; r = 0; break; } if (r == 1) break; } r = 1; for (int i = 1; i <= 2 * n; ++i) if (b[i] == 0) { r = 0; break; } if (r == 0) puts("-1"); else { for (int i = 1; i <= 2 * n; ++i) cout << b[i] << ' '; cout << endl; } } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t, n, a[100], b[100], i, j, f; cin >> t; while (t--) { cin >> n; int temp[201] = {0}; for (i = 0; i < n; i++) { cin >> a[i]; temp[a[i]]++; } for (i = 0; i < n; i++) { f = 0; j = a[i] + 1; while (j <= 2 * n && f == 0) { if (temp[j] == 0) { b[i] = j; temp[j]++; f++; } j++; } if (f == 0) { cout << -1 << endl; break; } } if (i == n) { for (j = 0; j < n; j++) cout << a[j] << " " << b[j] << " "; cout << endl; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.ArrayList; import java.util.List; public class Solution { public static void main(String[] args) throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(System.out); int testNum = nextInt(in); int numCount; int[] resArray; for (int i = 0; i < testNum; i++) { numCount = nextInt(in); resArray = new int[numCount * 2]; List<Integer> usedNums= new ArrayList<>(); for (int j = 0; j < numCount; j++) { resArray[(j + 1) * 2 - 2] = nextInt(in); usedNums.add(resArray[(j + 1) * 2 - 2]); } solve(numCount, resArray, usedNums); } } private static void solve(int numCount, int[] resArray, List<Integer> usedNums) { int tmpNum; for (int j = 1; j < numCount * 2; j += 2) { tmpNum = resArray[j - 1] + 1; while (usedNums.contains(tmpNum)) { tmpNum++; } if (tmpNum > numCount * 2) { System.out.println(-1); return; } else { usedNums.add(tmpNum); resArray[j] = tmpNum; } } for (int i = 0; i < numCount * 2; i++) { System.out.print(resArray[i] + " "); } System.out.println(); } private static int nextInt(StreamTokenizer in) throws IOException { in.nextToken(); return (int) in.nval; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) s=[] for njj in range(0,t): n=int(input()) b=list(map(int,input().split( ))) if 1 not in b or 2*n in b: s.append([-1]) else: a=[] for i in b: g=1 f=i while i+g in a or i+g in b : g+=1 if i+g>2*n: s.append([-1]) break else: a.append(f) a.append(i+g) if len(s)!=njj+1: s.append(a) for i in s: for j in i: print(j,end=' ') print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; long long arr[n]; map<long long, long long> mp; for (long long i = 0; i < n; i++) { cin >> arr[i]; mp[arr[i]] = 1; } vector<long long> ans; set<long long> s; for (long long i = 1; i <= 2 * n; i++) { if (!mp[i]) s.insert(i); } long long ok = 1; for (long long i = 0; i < n; i++) { ans.push_back(arr[i]); set<long long>::iterator it = s.lower_bound(arr[i]); if (it == s.end()) { ok = 0; break; } ans.push_back(*it); s.erase(it); } if (ok) { for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " "; cout << "\n"; } else { cout << -1 << "\n"; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> v(2 * n + 1, 0), A; for (int i = 0; i < n; i++) { int x; cin >> x; v[x] = 1; A.push_back(x); } int justice = 0; for (int i = 1; i <= 2 * n; i++) { if (justice < 0 && v[i] == 1) { cout << -1 << "\n"; return; } if (v[i] == 0) justice -= 1; else justice += 1; } for (int i = 0; i < n; i++) { int j = A[i]; while (v[j] != 0) { j++; } v[j] = 1; cout << A[i] << " " << j << " "; } cout << "\n"; } int main() { int t; cin >> t; while (t--) { solve(); } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from sys import exit from bisect import bisect def iis(): return map(int, input().split()) def ii(): return int(input()) def liis(): return list(map(int, input().split())) t = ii() for _ in range(t): n = ii() b = liis() todos = [] duplas = [] for i in range(1, 2*n+1): if i not in b: todos.append(i) flag = False for i in b: a = bisect(todos, i) if a >= len(todos): flag = True break duplas.append([i, todos[a]]) todos[a] = -1 todos = sorted(todos) if flag: print(-1) else: for dupla in duplas: print(" ".join(map(str, dupla)), end=" ") print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from collections import * for i in range(int(input())): n, a, ans = int(input()), list(map(int, input().split())), [] mem = Counter(a) for j in range(n): ans.append(a[j]) for k in range(a[j] + 1, 2 * n + 1): if not mem[k]: ans.append(k) mem[k] = 1 break if ans[-1] == a[j]: ans = [-1] break print(*ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) ansls=[0]*(2*n) used=set(b) flag=True for i in range(n): ansls[2*i]=b[i] for j in range(b[i]+1, 2*n+1): if not j in used: ansls[2*i+1]=j used.add(j) break else: flag=False if flag: print(*ansls) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) for x in range(t): n = int(input()) b = list(map(int, input().split())) numbers_used = [False for i in range(2*n)] a = [] for i in b: numbers_used[i-1] = True for i in b: a.append(i) for j in range(i, 2*n): if numbers_used[j]==False: numbers_used[j]=True a.append(j+1) break if a[-1]==i: a = [-1] break print(*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n, b = int(input()), list(map(int, input().split())) d = {e: True for e in range(1, 2*n + 1)} a = [0] * (2 * n) for i in range(n): a[2*i] = b[i] d[b[i]] = False ok = True for i in range(1, 2*n, 2): e = a[i-1] + 1 while e <= 2*n: if d[e]: a[i] = e d[e] = False break e += 1 if e > 2*n: ok = False break if not ok: print(-1) else: print(*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
n = int(input()) for i in range(n): lena = int(input()) arr = list(map(int, input().split())) rem = [j for j in list(range(1, 2*lena+1)) if j not in arr] counter = 0 final = [] for j in range(lena): counter2 = 0 for k in range(arr[j], 2*lena+1): if k in rem: final.append(arr[j]) final.append(k) rem.remove(k) counter2 = 1 break if counter2 == 0: print(-1) counter = 1 break if counter == 0: print(*final)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) for i in range(n): b[i] -= 1 # print("b: ", b) used = [False for _ in range(2*n)] for i in range(n): used[b[i]] = True ans = [0 for i in range(2*n)] for i in range(n): ans[2*i] = b[i] result = True for i in range(n): found_cand = False for cand in range(ans[2*i]+1, 2*n): if not used[cand]: used[cand] = True ans[2*i+1] = cand found_cand = True break if not found_cand: result = False if not result: print(-1) else: print(" ".join([str(item+1) for item in ans])) # print("ans: ", ans) # print("used: ", used) # print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.lang.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void readArray(FastReader scan, int[] a, int N) { for(int i = 0; i < N; i++) { a[i] = scan.nextInt(); } } public static void main(String[] args) { D(); } public static void A() { FastReader scan = new FastReader(); int T = scan.nextInt(); while(T-- > 0) { int a = scan.nextInt(); int b = scan.nextInt(); int x = scan.nextInt(); int y = scan.nextInt(); int ans1 = Math.max((a - x - 1) * b, x * b); int ans2 = Math.max((b - y - 1) * a, y * a); System.out.println(Math.max(ans1, ans2)); } } public static void D() { FastReader scan = new FastReader(); int T = scan.nextInt(); while(T-- > 0) { int N = scan.nextInt(); int[] b = new int[N]; readArray(scan, b, N); int[] a = new int[2 * N]; int[] n = new int[2 * N]; for(int i = 0; i < 2 * N; i += 2) { a[i] = b[i/2]; n[a[i] - 1]++; } int k = -1; for(int i = 1; i < 2 * N; i += 2) { k = a[i - 1]; if(k == 2 * N) { System.out.println(-1); break; } while(n[k] != 0) { k++; if(k == 2 * N) { break; } } if(k == 2 * N) { System.out.println(-1); break; } n[k] = 1; a[i] = k + 1; } if(k == 2 * N) { continue; } StringBuilder output = new StringBuilder(); for(int i = 0; i < 2 * N; i++) { output.append(a[i]).append(' '); } System.out.println(output); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int,input().split())) b = set(range(1,n*2+1)) b = b - set(a) b = list(b) f = 0 ans = [] for i in a: found = 0 j = 0 while j < len(b): if b[j] > i: found = 1 break j+=1 if not found: f = 1 break else: ans.append(i) ans.append(b[j]) del b[j] if not f: print(*ans) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 100 + 10; int n; int b[maxn]; int ans[2 * maxn]; void solve() { cin >> n; map<int, int> m; m.clear(); for (int i = 1; i <= n; i++) { cin >> b[i]; m[b[i]] = 1; ans[2 * i - 1] = b[i]; } for (int i = 1; i <= n; i++) { for (int j = b[i] + 1;; j++) { if (j > 2 * n) { puts("-1"); return; } if (m[j] == 0) { m[j] = 1; ans[2 * i] = j; break; } } } cout << ans[1]; for (int i = 2; i <= 2 * n; i++) cout << ' ' << ans[i]; cout << endl; } int main() { long long T; scanf("%lld\n", &T); while (T--) solve(); return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) for _ in range(t): n=int(input()) a=[i for i in range(1,2*n+1)] m=[int(i) for i in input().split()] l=[] for e in m: k=e l.append(k) while(True): e=e+1 if e not in m and e not in l: l.append(e) break q=l.copy() q.sort() if(q==a): print(*l) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n = int(input()) b = list(map(int,input().split())) used = [False]*( (2*n)+1) a = [None]*(2*n) possible=True for bi in b: used[bi] = True for i in range(2*n): if i%2==0: a[i] = b[i//2] else: no = a[i-1]+1 while no<=2*n and used[ no ]: no = no+1 if no > 2*n: possible=False break else: a[i] = no used[no] = True if possible: print(*a) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { bool u = 0, n = 0, z = 0; int a, b, c[100000] = {}, p, d, q = 1, s = 1; cin >> a; for (int i = 0; i < a; i++) { cin >> b; p = b * 2; s = 1; for (int i = 0; i <= (b * 2 - 2); i += 2) cin >> c[i]; for (int i = 0; i <= (b * 2 - 2); i += 2) { u = 0; q = 1; while (1) { d = c[i] + q; if (d > p) { u = 1; break; } else if (p >= d) { n = 0; z = 0; for (int i = 0; i <= b * 2; i++) if (d == c[i]) { n = 1; break; } if (n == 0) { c[s] = d; s += 2; z = 1; break; } if (n == 1 && i == b * 2 - 1) { u = 1; break; } } q++; } if (u == 1) break; } if (u == 1) cout << -1 << endl; else { for (int i = 0; i < b * 2; i++) cout << c[i] << " "; } cout << endl; for (int i = 0; i < 10000; i++) c[i] = 0; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) for test in range(t): n = int(input()) b = [int(i) for i in input().split()] def solve(): def minimal(v): for i in perm: if i > v: return perm.pop(perm.index(i)) return v perm = list(range(1, n * 2 + 1)) for i in range(n * 2 - 1, -1, -1): if perm[i] in b: del perm[i] msg = "" for v in b: msg += str(v) + " " iplus = minimal(v) if iplus > v: msg += str(iplus) + " " else: return "-1" return msg print(solve())
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Comparator; import java.util.Scanner; import java.util.Arrays; import java.util.TreeSet; import java.util.StringTokenizer; public class vk18 { public static void main(String[] stp) throws Exception { Scanner scan = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int t=Integer.parseInt(st.nextToken()),i,j=0; while(t-->0) { st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()),min=Integer.MAX_VALUE; Pair pair[]=new Pair[n]; boolean flag[]=new boolean[2*n + 1]; Arrays.fill(flag,false); st = new StringTokenizer(br.readLine()); for(i=0;i<n;i++) { int p=Integer.parseInt(st.nextToken()); pair[i]=new Pair(p,i+1,0); flag[p]=true; min=Math.min(min,p); } TreeSet<Integer> ts=new TreeSet<>(); for(i=1;i<=2*n;i++) { if(flag[i]==false) { ts.add(i); } } //Arrays.sort(pair,new Pair.First()); boolean right=true,left=true; for(i=0;i<n;i++) { right=false; int find=pair[i].d; for(j=pair[i].d+1;j<=2*n;j++) { if(ts.contains(j)) { right=true; pair[i].e=j; ts.remove(j); break; } } if(right==false){ pw.print(-1); left=false; break; } } if(left) { //Arrays.sort(pair, new Pair.Second()); for (i = 0; i < n; i++) pw.print(pair[i].d + " " + pair[i].e+" "); } pw.println(); } pw.flush(); } static class Pair implements Comparable<Pair> { int d, g,e; public Pair(int d, int g,int e) { this.d = d; this.g = g; this.e = e; } @Override public int compareTo(Pair p) { return 1; } static class First implements Comparator<Pair> { public int compare(Pair q, Pair s) { return -Integer.compare(s.d, q.d); } } static class Second implements Comparator<Pair> { public int compare(Pair q, Pair s) { return -Integer.compare(s.g, q.g); } } static class Third implements Comparator<Pair> { public int compare(Pair q, Pair s) { return -Integer.compare(s.e, q.e); } } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.io.FilterInputStream; import java.io.BufferedInputStream; 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; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CRestoringPermutation solver = new CRestoringPermutation(); solver.solve(1, in, out); out.close(); } static class CRestoringPermutation { public void solve(int testNumber, ScanReader in, PrintWriter out) { int t = in.scanInt(); while (t-- > 0) { int n = in.scanInt(); int arr[][] = new int[n][]; int ans[] = new int[2 * n]; HashSet<Integer> hashSet = new HashSet<>(); for (int i = 0; i < n; i++) arr[i] = new int[]{in.scanInt(), i, -1}; for (int i = 0; i < n; i++) hashSet.add(arr[i][0]); AVLTree<Integer> remai = new AVLTree<>(); for (int i = 1; i <= 2 * n; i++) { if (!hashSet.contains(i)) remai.insert(i); } boolean flag = true; for (int i = 0; i < n; i++) { int ne = (int) remai.lower_bound(arr[i][0]); if (ne >= 0 && ne < remai.getSize()) { arr[i][2] = remai.getAtIndex(ne); remai.remove(arr[i][2]); } else { flag = false; break; } } if (flag) { for (int i = 0; i < n; i++) out.print(arr[i][0] + " " + arr[i][2] + " "); out.println(); } else { out.println(-1); } } } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } static class AVLTree<T extends Comparable<? super T>> { private Node<T> root; private boolean multi; private Node<T> tempNode; public AVLTree() { this.root = null; this.multi = false; } public AVLTree(boolean multi) { this.root = null; this.multi = multi; } public T getAtIndex(int i) { if (i < 0 || i >= getSize()) return null; tempNode = root; int current = 0; while (tempNode != null) { if (tempNode.left == null) { if (current + tempNode.count <= i) { current += tempNode.count; tempNode = tempNode.right; } else { return tempNode.data; } } else { if (tempNode.leftcount + current > i) { tempNode = tempNode.left; } else if (tempNode.leftcount + tempNode.count + current <= i) { current += tempNode.count + tempNode.leftcount; tempNode = tempNode.right; } else return tempNode.data; } } return null; } public long getSize() { if (root == null) return 0; else { return root.leftcount + root.rightcount + root.count; } } public void insert(T data, long... count) { if (count.length == 0) root = insert(data, root, 1); else { if (count[0] > 0) root = insert(data, root, count[0]); } } private Node<T> insert(T data, Node<T> tempRoot, long count) { if (tempRoot == null) { if (!multi) { return new Node<>(data); } else { return new Node<>(data, count); } } int compartor = data.compareTo(tempRoot.data); if (compartor < 0) { tempRoot.left = insert(data, tempRoot.left, count); } else if (compartor > 0) { tempRoot.right = insert(data, tempRoot.right, count); } else { if (multi) { tempRoot.count += count; } } tempRoot.leftcount = getLeftCount(tempRoot.left) + getRightCount(tempRoot.left) + getCount(tempRoot.left); tempRoot.rightcount = getLeftCount(tempRoot.right) + getRightCount(tempRoot.right) + getCount(tempRoot.right); tempRoot.height = Math.max(Hieght(tempRoot.left), Hieght(tempRoot.right)) + 1; long diff = getBalence(tempRoot); if (diff > 1) { if (data.compareTo(tempRoot.left.data) < 0) { return rotateRight(tempRoot); } else { tempRoot.left = rotateLeft(tempRoot.left); return rotateRight(tempRoot); } } else if (diff < -1) { if (data.compareTo(tempRoot.right.data) > 0) { return rotateLeft(tempRoot); } else { tempRoot.right = rotateRight(tempRoot.right); return rotateLeft(tempRoot); } } return tempRoot; } public Node<T> rotateRight(Node<T> node) { Node<T> x = node.left; Node<T> t2 = x.right; x.right = node; node.left = t2; node.height = Math.max(Hieght(node.right), Hieght(node.left)) + 1; x.height = Math.max(Hieght(x.left), Hieght(x.right)) + 1; node.leftcount = getLeftCount(node.left) + getRightCount(node.left) + getCount(node.left); node.rightcount = getLeftCount(node.right) + getRightCount(node.right) + getCount(node.right); x.leftcount = getLeftCount(x.left) + getRightCount(x.left) + getCount(x.left); x.rightcount = getLeftCount(x.right) + getRightCount(x.right) + getCount(x.right); return x; } public Node<T> rotateLeft(Node<T> node) { Node<T> x = node.right; Node<T> t2 = x.left; x.left = node; node.right = t2; node.height = Math.max(Hieght(node.right), Hieght(node.left)) + 1; x.height = Math.max(Hieght(x.left), Hieght(x.right)) + 1; node.leftcount = getLeftCount(node.left) + getRightCount(node.left) + getCount(node.left); node.rightcount = getLeftCount(node.right) + getRightCount(node.right) + getCount(node.right); x.leftcount = getLeftCount(x.left) + getRightCount(x.left) + getCount(x.left); x.rightcount = getLeftCount(x.right) + getRightCount(x.right) + getCount(x.right); return x; } public long getLeftCount(Node<T> node) { if (node == null) return 0; return node.leftcount; } public long getRightCount(Node<T> node) { if (node == null) return 0; return node.rightcount; } public long getCount(Node<T> node) { if (node == null) return 0; return node.count; } long getBalence(Node<T> node) { if (node == null) return 0; return Hieght(node.left) - Hieght(node.right); } public long Hieght(Node<T> Node) { if (Node == null) return 0; else return Node.height; } public void remove(T data) { root = remove(root, data, 1); } private Node<T> remove(Node<T> tempRoot, T data, long num) { if (tempRoot == null) return null; int compartor = data.compareTo(tempRoot.data); if (compartor < 0) { tempRoot.left = remove(tempRoot.left, data, num); } else if (compartor > 0) { tempRoot.right = remove(tempRoot.right, data, num); } else { if (multi && num < tempRoot.count && tempRoot.count > 1) { tempRoot.count -= num; } else { if (tempRoot.left == null && tempRoot.right == null) { return null; } else if (tempRoot.left == null) return tempRoot.right; else if (tempRoot.right == null) return tempRoot.left; else { Node<T> minValueNode = getMinValueNode(tempRoot.right); tempRoot.data = minValueNode.data; tempRoot.count = minValueNode.count; tempRoot.right = remove(tempRoot.right, tempRoot.data, tempRoot.count); } } } tempRoot.leftcount = getLeftCount(tempRoot.left) + getRightCount(tempRoot.left) + getCount(tempRoot.left); tempRoot.rightcount = getLeftCount(tempRoot.right) + getRightCount(tempRoot.right) + getCount(tempRoot.right); tempRoot.height = Math.max(Hieght(tempRoot.left), Hieght(tempRoot.right)) + 1; long diff = getBalence(tempRoot); if (diff > 1) { if (getBalence(tempRoot.left) >= 0) { return rotateRight(tempRoot); } else { tempRoot.left = rotateLeft(tempRoot.left); return rotateRight(tempRoot); } } else if (diff < -1) { if (getBalence(tempRoot.right) <= 0) { return rotateLeft(tempRoot); } else { tempRoot.right = rotateRight(tempRoot.right); return rotateLeft(tempRoot); } } return tempRoot; } public Node<T> getMinValueNode(Node<T> node) { if (node == null) return null; Node<T> currentNode = node; while (currentNode.left != null) { currentNode = currentNode.left; } return currentNode; } public long lower_bound(T data) { tempNode = root; long index = 0; while (tempNode != null) { int comparator = data.compareTo(tempNode.data); if (comparator < 0) { tempNode = tempNode.left; } else if (comparator > 0) { index += (tempNode.count + tempNode.leftcount); tempNode = tempNode.right; } else { index += tempNode.leftcount; break; } } return index; } public class Node<T> { T data; long height; Node<T> left; Node<T> right; long leftcount; long rightcount; long count; public Node(T data) { this.data = data; this.height = 1; this.left = null; this.right = null; this.leftcount = 0; this.rightcount = 0; this.count = 1; } public Node(T data, long count) { this.data = data; this.height = 1; this.left = null; this.right = null; this.leftcount = 0; this.rightcount = 0; this.count = count; } } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def STR(): return list(input()) def INT(): return int(input()) def MAP(): return map(int, input().split()) def MAP2():return map(float,input().split()) def LIST(): return list(map(int, input().split())) def STRING(): return input() import string import sys from heapq import heappop , heappush from bisect import * from collections import deque , Counter , defaultdict from math import * from itertools import permutations , accumulate dx = [-1 , 1 , 0 , 0 ] dy = [0 , 0 , 1 , - 1] #visited = [[False for i in range(m)] for j in range(n)] #sys.stdin = open(r'input.txt' , 'r') #sys.stdout = open(r'output.txt' , 'w') #for tt in range(INT()): #CODE for tt in range(INT()): n = INT() arr = LIST() freq = [0] * (2*n+1) for i in arr: freq[i] = 1 #print(freq) flag = True ans = [] for i in range(n): for j in range(arr[i] , 2*n+1): if j > arr[i] and freq[j] == 0: ans.append(arr[i]) ans.append(j) freq[j] = 1 break #print(ans) k = list(ans) if len(ans) < 2 * n : print('-1') else: ans.sort() for i in range(1 , 2 * n + 1): if (i != ans[i - 1]): flag = False break if flag: print(*k) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) solutions = [] for i in range(t) : n = int(input()) string = str(input()) st = string.split() lst = [] for j in range(n) : lst.append(int(st[j])) solution = [] for j in range(n) : solution.append(lst[j]) number = lst[j] ok = 0 while ok == 0 and number < 2*n : number= number +1 if number not in solution and number not in lst : ok =1 if ok == 1 : solution.append(number) if len(solution) == 2*n : solutions.append(solution) else : solutions.append([-1]) for i in range(t) : for number in solutions[i] : print(number, end=" ") print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [] d = 0 for i in range(1, 2*n+1): if i not in a: b.append(i) b.sort() c = [] i = 0 j = 0 while(len(b) > 0): if(b[-1] < a[i]): d = 1 break else: if(b[j] > a[i]): c.append(a[i]) c.append(b[j]) a.remove(a[i]) b.remove(b[j]) j = 0 else: j += 1 if(j == len(b)): break if(d == 1): print(-1) else: print(*c)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
I=input for _ in[0]*int(I()): n=2*int(I());a=n*[0];b=a[::2]=*map(int,I().split()),;c={*range(1,n+1)}-{*b};i=1 try: for x in b:z=a[i]=min(y for y in c if x<y);c-={z};i+=2 except:a=-1, print(*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from math import * # from sys import stdin,stdout def binarySearch(arr,x,i): l=i r=len(arr)-1 while l <= r: mid = (l + r)//2; if arr[mid] == x: return mid elif arr[mid] < x: l = mid + 1 else: r = mid - 1 return -1 def js(arr,x): l=0 r=len(arr)-1 ans=-1 while(l<=r): m=(l+r)//2 if(arr[m]<=x): ans=m l=m+1 else: r=m-1 return ans def jg(arr,x): l=0 r=len(arr)-1 ans=-1 while(l<=r): m=(l+r)//2 if(arr[m]>=x): ans=m r=m-1 else: l=m+1 return ans def ceil(a,b): if a%b == 0: return int(a/b) else: return (a//b + 1) def c(s,i,a,b): count=0 for j in range(i,len(s)): if(s[i]!=s[j]): if(s[i]=="A"): count+=a else: count+=b i=j if(i<len(s)-1): if(s[i]=="A"): count+=a else: count+=b return count for __ in range(int(input())): n=int(input()) ar=list(map(int,input().split())) h=[0]*(2*n + 1) ans=[0]*(2*n +1) for i in range(n): h[ar[i]]=1 for i in range(n): ans[2*(i+1)-1]=ar[i] f=-1 for j in range(ar[i],2*n+1): if(h[j]==0): f=j h[j]=1 break if(f==-1): break else: ans[2*(i+1)]=j if(f==-1): print(-1) # print(*ans[1:]) else: print(*ans[1:])
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.io.*; public class Asd { static Scanner s=new Scanner(System.in); static PrintWriter w=new PrintWriter(System.out); public static void main(String args[]) { int t=s.nextInt(); while(t-->0) { solve(); } w.close(); } public static void solve() { int n=s.nextInt(); HashSet<Integer> set=new HashSet<>(); ArrayList<Integer> list=new ArrayList<>(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); set.add(arr[i]); } if(set.contains(2*n)||!set.contains(1)) { w.println(-1);return;} for(int i=0;i<n;i++) { for(int j=1;true;j++) { if(!set.contains(arr[i]+j)) { if(arr[i]+j>2*n) { w.println(-1);return; } list.add(arr[i]);list.add(arr[i]+j); set.add(arr[i]+j); break; } } } for(int i=0;i<list.size();i++) w.print(list.get(i)+" "); w.println(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; public class Solution{ public static void main(String args[]){ Scanner in=new Scanner (System.in); int t=in.nextInt(); while(--t>=0){ int n=in.nextInt(); int a[]=new int[n]; int b[]=new int[2*n]; HashSet<Integer> hset=new HashSet<>(); int flag=0; for(int i=0;i<n;i++){ a[i]=in.nextInt(); hset.add(a[i]); b[2*i]=a[i]; } for(int i=0;i<n;i++){ int x=a[i]; while(true){ x++; if(x>2*n){ flag=1; break; } if(!hset.contains(x)){ hset.add(x); b[2*i+1]=x; break; } } } if(flag==1) System.out.println(-1); else{ for(int i=0;i<2*n;i++){ System.out.print(b[i]+" "); } System.out.println(); } } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.lang.*; import java.io.*; public class Main { /* HashMap<> map=new HashMap<>(); TreeMap<> map=new TreeMap<>(); map.put(p,map.getOrDefault(p,0)+1); for(Map.Entry<> mx:map.entrySet()){ int v=mx.getValue(),k=mx.getKey(); }for (int i = 1; i <= 1000; i++) fac[i] = fac[i - 1] * i % mod; ArrayList<Pair<Character,Integer>> l=new ArrayList<>(); ArrayList<> l=new ArrayList<>(); HashSet<> has=new HashSet<>(); PriorityQueue<Integer> pq=new PriorityQueue<>(new Comparator<Integer>(){ public int compare(Integer a,Integer b){ return Integer.compare(b,a); } });*/ PrintWriter out; FastReader sc; long mod=(long)(1e9+7); int maxint= Integer.MAX_VALUE; int minint= Integer.MIN_VALUE; long maxlong=Long.MAX_VALUE; long minlong=Long.MIN_VALUE; /****************************************************************************************** *****************************************************************************************/ public void sol(){ int n=ni(); int[] ar=new int[n+1],p=new int[2*n+1]; TreeSet<Integer> h=new TreeSet<>(); for(int i=1;i<=2*n;i++)h.add(i); for(int i=1;i<=n;i++){ int d=ni(); h.remove(d); ar[i]=d; }boolean f=true; for(int i=1;i<=n;i++){ p[2*i-1]=ar[i]; int x=-1; for(int j:h){ if(j>ar[i]){ x=j; break; } }if(x==-1){ f=false; break; } p[2*i]=x; h.remove(x); }if(!f)pl("-1"); else{ StringBuilder sb=new StringBuilder(); for(int i=1;i<=2*n;i++)sb.append(p[i]+" "); pl(sb); } } public static void main(String[] args) { Main g=new Main(); g.out=new PrintWriter(System.out); g.sc=new FastReader(); int t=g.ni(); // int t=1; while(t-->0) g.sol(); g.out.flush(); } /**************************************************************************************** *****************************************************************************************/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public int ni(){ return sc.nextInt(); }public long nl(){ return sc.nextLong(); }public double nd(){ return sc.nextDouble(); }public char[] rl(){ return sc.nextLine().toCharArray(); }public String rl1(){ return sc.nextLine(); } public void pl(Object s){ out.println(s); }public void ex(){ out.println(); } public void pr(Object s){ out.print(s); }public String next(){ return sc.next(); }public long abs(long x){ return Math.abs(x); } public int abs(int x){ return Math.abs(x); } public double abs(double x){ return Math.abs(x); } public long pow(long x,long y){ return (long)Math.pow(x,y); } public int pow(int x,int y){ return (int)Math.pow(x,y); } public double pow(double x,double y){ return Math.pow(x,y); }public long min(long x,long y){ return (long)Math.min(x,y); } public int min(int x,int y){ return (int)Math.min(x,y); } public double min(double x,double y){ return Math.min(x,y); }public long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); }public long lcm(long a, long b) { return (a / gcd(a, b)) * b; } void sort1(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort1(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } void sort(double[] a) { ArrayList<Double> l = new ArrayList<>(); for (double i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }int swap(int a,int b){ return a; }long swap(long a,long b){ return a; }double swap(double a,double b){ return a; } boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0); }boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); }public long max(long x,long y){ return (long)Math.max(x,y); } public int max(int x,int y){ return (int)Math.max(x,y); } public double max(double x,double y){ return Math.max(x,y); }long sqrt(long x){ return (long)Math.sqrt(x); }int sqrt(int x){ return (int)Math.sqrt(x); }void input(int[] ar,int n){ for(int i=0;i<n;i++)ar[i]=ni(); }void input(long[] ar,int n){ for(int i=0;i<n;i++)ar[i]=nl(); }void fill(int[] ar,int k){ Arrays.fill(ar,k); }void yes(){ pl("YES"); }void no(){ pl("NO"); } int[] sieve(int n) { boolean prime[] = new boolean[n+1]; int[] k=new int[n+1]; for(int i=0;i<=n;i++) { prime[i] = true; k[i]=i; } for(int p = 2; p <=n; p++) { if(prime[p] == true) { // sieve[p]=p; for(int i = p*2; i <= n; i += p) { prime[i] = false; // sieve[i]=p; while(k[i]%(p*p)==0){ k[i]/=(p*p); } } } }return k; } int strSmall(int[] arr, int target) { int start = 0, end = arr.length-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } int strSmall(ArrayList<Integer> arr, int target) { int start = 0, end = arr.size()-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid) > target) { start = mid + 1; ans=start; } else { end = mid - 1; } } return ans; }long mMultiplication(long a,long b) { long res = 0; a %= mod; while (b > 0) { if ((b & 1) > 0) { res = (res + a) % mod; } a = (2 * a) % mod; b >>= 1; } return res; }long nCr(int n, int r, long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; }long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }long modInverse(long n, long p) { return power(n, p - 2, p); } public static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + "," + y; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(Pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.HashSet; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Mufaddal Naya */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); CRestoringPermutation solver = new CRestoringPermutation(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) { solver.solve(i, in, out); } out.close(); } static class CRestoringPermutation { public void solve(int testNumber, InputReader c, OutputWriter w) { int n = c.readInt(); int a[] = c.readIntArray(n); HashSet<Integer> hh = new HashSet<>(); for (int i = 0; i < n; i++) { hh.add(a[i]); } int bb[] = new int[2 * n]; for (int i = 0; i < n; i++) { int ck = -1; for (int j = a[i] + 1; j <= 2 * n; j++) { if (!hh.contains(j)) { ck = j; hh.add(j); break; } } if (ck == -1) { w.printLine(-1); return; } bb[2 * i] = a[i]; bb[2 * i + 1] = ck; } w.printLine(bb); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } public void printLine(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); M: while (t-- > 0) { int n = scn.nextInt(), B_ARR[] = new int[n + 1]; HashSet<Integer> set = new HashSet<>(); for (int i = 1; i <= n; i++) { B_ARR[i] = scn.nextInt(); set.add(B_ARR[i]); } int A_arr[] = new int[2 * n + 1], val = 1; for (int i = 1; i <= n; i++) { int v = B_ARR[i]; A_arr[2 * i - 1] = v; val = v + 1; while (set.contains(val)) val++; if (val > 2 * n) { System.out.println(-1); continue M; } set.add(val); A_arr[2 * i] = val; } for (int i = 1; i < A_arr.length; i++) System.out.print(A_arr[i]+" "); System.out.println(); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.*; import java.math.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class RestoringPermutation { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(in.nextInt(), in, out); out.close(); } static class TaskA { long mod = (long)(1000000007); public void solve(int testNumber, InputReader in, PrintWriter out) { while(testNumber-->0){ int n = in.nextInt(); int a[] = new int[2*n+1]; int b[] = new int[2*n+1]; for(int i=1;i<=2*n;i+=2){ a[i] = in.nextInt(); b[a[i]] = 1; } boolean ans = true; for(int j=2;j<=2*n;j+=2){ int i = a[j-1]+1; while(i<=2*n && b[i]!=0) i++; if(i>2*n){ ans = false; break; } b[i] = 1; a[j] = i; } if(ans){ for(int i=1;i<=2*n;i++) out.print(a[i] + " "); out.println(); } else out.println(-1); } } public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) { if (start > end) { return; } int mid = (start + end) / 2; a.add(mid); sortedArrayToBST(a, start, mid - 1); sortedArrayToBST(a, mid + 1, end); } class Combine{ long value; long delete; Combine(long val , long delete){ this.value = val; this.delete = delete; } } class Sort2 implements Comparator<Combine>{ public int compare(Combine a , Combine b){ if(a.value > b.value) return 1; else if(a.value == b.value && a.delete>b.delete) return 1; else if(a.value == b.value && a.delete == b.delete) return 0; return -1; } } public int lowerLastBound(ArrayList<Integer> a , int x){ int l = 0; int r = a.size()-1; if(a.get(l)>=x) return -1; if(a.get(r)<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid-1)<x) return mid-1; else if(a.get(mid)>=x) r = mid-1; else if(a.get(mid)<x && a.get(mid+1)>=x) return mid; else if(a.get(mid)<x && a.get(mid+1)<x) l = mid+1; } return mid; } public int upperFirstBound(ArrayList<Integer> a , Integer x){ int l = 0; int r = a.size()-1; if(a.get(l)>x) return l; if(a.get(r)<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid+1)>x) return mid+1; else if(a.get(mid)<=x) l = mid+1; else if(a.get(mid)>x && a.get(mid-1)<=x) return mid; else if(a.get(mid)>x && a.get(mid-1)>x) r = mid-1; } return mid; } public int lowerLastBound(int a[] , int x){ int l = 0; int r = a.length-1; if(a[l]>=x) return -1; if(a[r]<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid-1]<x) return mid-1; else if(a[mid]>=x) r = mid-1; else if(a[mid]<x && a[mid+1]>=x) return mid; else if(a[mid]<x && a[mid+1]<x) l = mid+1; } return mid; } public int upperFirstBound(long a[] , long x){ int l = 0; int r = a.length-1; if(a[l]>x) return l; if(a[r]<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid+1]>x) return mid+1; else if(a[mid]<=x) l = mid+1; else if(a[mid]>x && a[mid-1]<=x) return mid; else if(a[mid]>x && a[mid-1]>x) r = mid-1; } return mid; } public long log(float number , int base){ return (long) Math.floor((Math.log(number) / Math.log(base))); } public long gcd(long a , long b){ if(a<b){ long c = b; b = a; a = c; } if(a%b==0) return b; return gcd(b , a%b); } public void print2d(int a[][] , PrintWriter out){ for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++) out.print(a[i][j] + " "); out.println(); } out.println(); } public void print1d(int a[] , PrintWriter out){ for(int i=0;i<a.length;i++) out.print(a[i] + " "); out.println(); 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()); } public long nextLong() { return Long.parseLong(next()); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) import heapq for _ in range(t): n = int(input()) B = list(map(int, input().split())) if 1 not in set(B): print(-1) continue d = {} A = [0]*(2*n) for i in range(n): A[2*i] = B[i] d[B[i]] = 2*i+1 #print(id) flag = True ids = [] heapq.heapify(ids) for i in range(1, 2*n+1): if i in d: heapq.heappush(ids, d[i]) else: if not ids: flag = False break id = heapq.heappop(ids) if A[id-1] > i: flag = False break else: A[id] = i if flag: print(*A) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import math from collections import Counter for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) ll=[] s=list(range(1,n+1)) for i in l: ll.append(i) if i+1 not in ll and i+1 not in l: ll.append(i+1) else: j=i+2 while j in ll or j in l: j+=1 ll.append(j) d=Counter(ll) c=0 for i in d: if i>2*n or d[i]>1: c=1 #print(i,d[i],n,d,ll) break if c==1: print(-1) else: print(*ll)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from bisect import * for t in range(int(input())): n=int(input()) b=list(map(int,input().split())) f=0 c=set(b) d=[] for i in range(1,2*n+1): if i not in c: d.append(i) a=[] for i in range(n): a.append(b[i]) x=bisect_left(d,b[i]) if x==len(d): f=1 break a.append(d[x]) d.remove(d[x]) if f: print(-1) else: print(*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t= in.nextInt(); while(t-- > 0) { boolean num[] = new boolean[12345]; num[0] = true; int n = in.nextInt(); int b[] = new int[n + 1]; int a[] = new int[2*n + 1]; for (int i = 0; i < n; i++) { b[i] = in.nextInt(); num[b[i]]= true; } List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(b[i]); for (int j = b[i]+1; j < 300; j++) { if(!num[j]) { num[j] = true; list.add(j); break; } } } List<Integer> answer = new ArrayList<>(list); Collections.sort(list); boolean flag = true; for (int i = 0; i < 2 * n; i++) { if(list.get(i) != i+1) { flag = false; break; } } StringBuilder sb = new StringBuilder(); if(flag) { for (int i = 0; i < 2 * n; i++) { sb.append(answer.get(i)).append(" "); } out.println(sb.toString()); } else out.println(-1); } out.close(); } 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()); } public Long nextLong() { return Long.parseLong(next()); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n = int(input()) dic = {} for i in range(1,2*n+1): dic[i] = 0 arr = list(map(int,input().split())) flag = 0 for i in range(n): if arr[i]>=2*n: flag = 1 dic[arr[i]] = 1 l = [0]*(2*n) count = 0 for i in range(0,2*n,2): j = arr[count] while(j<=2*n and dic[j]!=0): j+=1 if(j==2*n and dic[j]==1): flag =1 break elif(j>2*n): flag =1 break l[i] = arr[count] count += 1 l[i+1] = j dic[j] = 1 if(flag==0): print(*l) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys import copy input=sys.stdin.buffer.readline t=int(input()) while t: t-=1 n=int(input()) arr=list(map(int,input().split())) arr1=copy.deepcopy(arr) arr1.sort() if arr1[0]==1: if max(arr)+1<=2*n: ans=[] for i in range(n): ans.append(arr[i]) for j in range(arr[i]+1,2*n+1): if j not in arr1: ans.append(j) arr1.append(j) break if len(set(ans))!=2*n: print("-1") else: for x in ans: print(x,end=' ') print() else: print("-1") else: print("-1")
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int t, n; void solve() { cin >> n; int b[n]; bool foundMin = false; bool foundMax = false; for (int i = 0; i < n; i++) { cin >> b[i]; if (b[i] == 1) foundMin = true; if (b[i] == n * 2) foundMax = true; } if (!foundMin || foundMax) { cout << "-1\n"; return; } int tempB[n]; for (int i = 0; i < n; i++) { tempB[i] = b[i]; } sort(tempB, tempB + n); int lastNum = 1; int needed = 2; for (int i = 1; i < n; i++) { if (tempB[i] - lastNum > needed) { cout << "-1\n"; return; } else { needed += tempB[i] - lastNum; lastNum = tempB[i]; } } bool taken[n * 2]; for (int i = 0; i < n * 2; i++) { taken[i] = false; } for (int i = 0; i < n; i++) { taken[b[i] - 1] = true; } vector<int> ans; for (int i = 0; i < n; i++) { ans.push_back(b[i]); bool found = false; for (int j = b[i]; j < n * 2; j++) { if (!taken[j]) { ans.push_back(j + 1); taken[j] = true; found = true; break; } } if (!found) { cout << "-1\n"; return; } } for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; cout << "\n"; } int main(int argc, char **argv) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; for (int i = 0; i < t; i++) { solve(); } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; bool compare(const pair<string, string>& i, const pair<string, string>& j) { if (i.first != j.first) return i.first < j.first; else return i.second < j.second; } vector<long long> factorize(long long n) { vector<long long> v; for (long long i = 2; i * i <= n; i++) { while (n % i == 0) { v.push_back(i); n /= i; } } if (n != 1) v.push_back(n); return v; } bool isPallindrome(string s) { for (long long i = 0; i <= s.length() / 2; i++) { if (s[i] != s[s.length() - i - 1]) return false; } return true; } long long GCD(long long a, long long b) { if (b == 0) return a; return GCD(b, a % b); } vector<bool> sieve(long long n) { vector<bool> PRIME(n + 1, true); PRIME[0] = false; PRIME[1] = false; for (long long p = 2; p * p <= n; p++) { if (PRIME[p] == true) { for (long long i = p * 2; i <= n; i += p) { PRIME[i] = false; } } } return PRIME; } bool isPrime(long long n) { if (n == 1) return false; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { long long n, a; cin >> n; vector<long long> vec; vector<bool> v((2 * n) + 1, false); v[0] = true; for (long long i = 0; i < n; i++) { cin >> a; v[a] = true; vec.push_back(a); } for (long long i = 0; i < n; i++) { long long p = vec[i] + 1; while (true) { if (p > 2 * n) { cout << "-1" << "\n"; goto h; } if (v[p] == false) { vec.push_back(p); v[p] = true; break; } p++; } } if (count((v).begin(), (v).end(), false) != 0) cout << "-1" << "\n"; else { for (long long i = 0; i < n; i++) cout << vec[i] << " " << vec[i + n] << " "; cout << "\n"; } h:; } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int a[101]; bool c[201]; vector<int> b[101]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; memset(c, false, sizeof(c)); for (int i = 0; i < n; i++) { b[i].clear(); cin >> a[i]; c[a[i]] = true; b[i].push_back(a[i]); } bool ff = false; for (int i = 1; i <= 2 * n; i++) { if (!c[i]) { bool f = false; for (int j = 0; j < n; j++) { if (b[j].size() == 2) continue; if (b[j][0] < i) { b[j].push_back(i); sort(b[j].begin(), b[j].end()); f = true; break; } } if (!f) { ff = true; break; } } } if (ff) { cout << -1 << '\n'; } else { for (int i = 0; i < n; i++) { cout << b[i][0] << ' ' << b[i][1] << ' '; } cout << '\n'; } } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); static void solve() throws Exception { int n = sc.nextInt(), arr[] = new int[2 * n]; TreeSet<Integer> st = new TreeSet<>(); for (int i = 1; i <= 2 * n; i++) { st.add(i); } for (int i = 0; i < n; i++) { arr[2 * i] = sc.nextInt(); st.remove(arr[2 * i]); } for(int i = 1; i < 2*n; i+=2) { int val = arr[i - 1]; if(st.higher(val) == null) { out.println(-1); return; } arr[i] = st.higher(val); st.remove(arr[i]); } for(int x : arr) out.print(x + " "); out.println(); } public static void main(String[] args) throws Exception { int tc = sc.nextInt(); while (tc-- > 0) { solve(); } out.close(); } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public Long nextLong() throws IOException { return Long.parseLong(next()); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) for _ in range(t): n = int(input()) B = list(map(int, input().split())) cands = [True] * (2 * n) for b in B: cands[b - 1] = False ret = [] for b in B: ret.append(b) for i in range(2 * n): if b < i + 1 and cands[i]: ret.append(i + 1) cands[i] = False break else: ret = [-1] break print(*ret)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { long long int t, i, j, k, n, x, y, x1, y1, x2, y2, p, arr[1000], b[2000], l; cin >> t; while (t--) { cin >> n; j = 0; long long int flag = 0, flag1 = 1; for (i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] > n) { j++; } if (arr[i] == 2 * n) { flag = 1; } if (arr[i] == 1) { flag1 = 0; } } if (j > n / 2 || flag) { cout << "-1\n"; } else { set<long long int> se; for (i = 1; i <= 2 * n; i++) { se.insert(i); } for (i = 0; i < n; i++) { se.erase(arr[i]); } l = 0; long long int flag2 = 0; for (i = 0; i < n; i++) { b[l++] = arr[i]; j = arr[i] + 1; long long int flag3 = 0; for (j = j; j <= 2 * n; j++) { if (se.find(j) != se.end()) { b[l++] = j; se.erase(j); flag3 = 1; break; } } if (flag3 == 0) { flag2 = 1; break; } } if (flag2) { cout << "-1\n"; continue; } for (i = 0; i < 2 * n; i++) { cout << b[i] << " "; } cout << endl; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
T=int(input()) for _ in range(T): a=int(input()) A=list(map(int,input().split())) DP=[0 for i in range((2*a)+1)] flag=0 for i in A: if(i>(2*a)): flag=1 else: DP[i]=1 if(flag==1): print(-1) continue else: mv=a B=[] for i in range(a): flag=0 b=A[i] B.append(b) for j in range(b,(2*a)+1): if(DP[j]==0): flag=1 B.append(j) DP[j]=1 break if(flag==0): B=[-1] break for i in B[:len(B)-1]: print(i,end=" ") print(B[len(B)-1])
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int arr[105]; unordered_set<int> used; bool fl = 1; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> arr[i]; used.insert(arr[i]); } vector<int> v; for (int i = 0; i < n; ++i) { v.push_back(arr[i]); int j = arr[i] + 1; while (used.find(j) != used.end()) ++j; if (j > 2 * n) break; else { v.push_back(j); } used.insert(j); } if (v.size() == 2 * n) for (int i = 0; i < v.size(); ++i) cout << v[i] << ' '; else cout << -1; cout << '\n'; v.clear(); used.clear(); } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int b[n]; int ans[2 * n]; for (long long i = 0; i < n; i++) cin >> b[i]; set<int> a; for (long long i = 0; i < 2 * n; i++) a.insert(i + 1); for (long long i = 0; i < n; i++) { ans[2 * i] = b[i]; a.erase(b[i]); } bool poss = true; for (long long i = 0; i < n; i++) { set<int>::iterator up = upper_bound(a.begin(), a.end(), b[i]); if (up != a.end()) { ans[2 * i + 1] = *up; a.erase(up); } else { cout << -1 << '\n'; poss = false; break; } } if (poss) { for (long long i = 0; i < 2 * n - 1; i++) cout << ans[i] << ' '; cout << ans[2 * n - 1] << '\n'; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class CodeForce { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0){ int n=Integer.parseInt(br.readLine()); int flag=0; HashSet<Integer> set1=new HashSet<>(); HashSet<Integer> set2=new HashSet<>(); String[] s=br.readLine().split(" "); int[] arr=new int[2*n]; for(int i=0;i<n;i++){ arr[i*2]=Integer.parseInt(s[i]); set1.add(arr[i*2]); if(arr[i*2]>=2*n){ flag=1; break; } } if(flag==1){ System.out.println("-1"); } else{ int te; for(int i=1;i<2*n;i=i+2){ te=arr[i-1]; while(true){ if(!set1.contains(te) && !set2.contains(te)) { break; } te++; } if(te>2*n) { flag=1; break; } arr[i]=te; set2.add(arr[i]); } if(flag==0){ for(int i=0;i<2*n;i++) System.out.print(arr[i]+" "); System.out.println(" "); } else System.out.println(-1); } } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int arr[n]; vector<bool> brr(2 * n + 1, 0); brr[0] = 1; for (int i = 0; i < n; i++) { cin >> arr[i]; brr[arr[i]] = 1; } vector<int> v(n); bool flag = 1, flag2 = 0; for (int i = 0; i < n; i++) { flag2 = 0; for (int j = arr[i]; j < 2 * n + 1; j++) { if (brr[j] == 0) { v[i] = j; brr[j] = 1; flag2 = 1; break; } } if (flag2 == 0) { flag = 0; break; } } if (flag) { for (int i = 0; i < n; i++) cout << arr[i] << " " << v[i] << " "; cout << "\n"; } else cout << "-1\n"; return; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; for (int i = 0; i < t; i++) { solve(); } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; set<int> s; for (int i = 1; i <= 2 * n; i++) s.insert(i); vector<int> b(n + 1); vector<int> a(2 * n + 1, 0); for (int i = 1; i <= n; i++) cin >> b[i]; int flag = 0; for (int i = 1; i <= n; i++) { if (s.find(b[i]) != s.end()) { s.erase(b[i]); a[2 * i - 1] = b[i]; } else { flag = 1; break; } } if (flag == 0) { set<int>::iterator it; for (it = s.begin(); it != s.end() && flag == 0; it++) { int flag1 = 0; for (int i = 2; i <= 2 * n; i = i + 2) { if (a[i] == 0) { if (a[i - 1] < *it) { a[i] = *it, flag1 = 1; break; } } } if (flag1 == 0) flag = 1; } } if (flag == 1) cout << -1; else { for (int i = 1; i <= 2 * n; i++) cout << a[i] << " "; } cout << endl; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n), b(2 * n); multiset<int> m; for (size_t i = 0; i < 2 * n; ++i) m.insert(i + 1); for (auto& i : a) { cin >> i; m.erase(i); } bool flg = true; for (size_t i = 0; i < n; ++i) { b[2 * i] = a[i]; auto it = upper_bound(m.begin(), m.end(), a[i]); if (it == m.end()) { flg = false; cout << -1 << endl; break; } b[2 * i + 1] = *it; m.erase(*it); } if (!flg) continue; for (auto& i : b) cout << i << " "; cout << endl; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def bubbleSort(arr): n = len(arr) # Traverse through all array elements for i in range(n): # Last i elements are already in place for j in range(0, n-i-1): # traverse the array from 0 to n-i-1 # Swap if the element found is greater # than the next element if arr[j] < arr[j+1] : arr[j], arr[j+1] = arr[j+1], arr[j] number_of_testcase=int(input()) for i in range(number_of_testcase): n=int(input()) numbers_list=list(map(int,input().split(' '))) initial_list=numbers_list.copy() #available_numbers=list(set(range(1,2*n+1))-set(initial_list)) available_numbers=[x for x in list(range(1,2*n+1)) if x not in initial_list] #print(available_numbers) flag2=1 final_dict={} for i in initial_list: flag=0 for j in available_numbers: if(i<j): flag=1 final_dict[i]=j available_numbers.remove(j) break if(flag==0): print('-1') flag2=0 break if(flag2==1): for j in initial_list: print(str(j)+' '+str(final_dict[j]),end=' ') print('')
PYTHON3
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; using LL = long long; namespace _buff { const size_t BUFF = 1 << 19; char ibuf[BUFF], *ib = ibuf, *ie = ibuf; char getc() { if (ib == ie) { ib = ibuf; ie = ibuf + fread(ibuf, 1, BUFF, stdin); } return ib == ie ? -1 : *ib++; } } // namespace _buff LL read() { using namespace _buff; LL ret = 0; bool pos = true; char c = getc(); for (; (c < '0' || c > '9') && c != '-'; c = getc()) { assert(~c); } if (c == '-') { pos = false; c = getc(); } for (; c >= '0' && c <= '9'; c = getc()) { ret = (ret << 3) + (ret << 1) + (c ^ 48); } return pos ? ret : -ret; } const int MOD = 998244353; using uint = unsigned; struct Z { uint v; Z(uint v = 0) : v(v) {} Z &operator+=(const Z &z) { v += z.v; if (v >= MOD) v -= MOD; return *this; } Z &operator-=(const Z &z) { if (v < z.v) v += MOD; v -= z.v; return *this; } Z &operator*=(const Z &z) { v = static_cast<uint64_t>(v) * z.v % MOD; return *this; } }; ostream &operator<<(ostream &os, const Z &z) { return os << z.v; } Z operator+(const Z &x, const Z &y) { return Z(x.v + y.v >= MOD ? x.v + y.v - MOD : x.v + y.v); } Z operator-(const Z &x, const Z &y) { return Z(x.v < y.v ? x.v + MOD - y.v : x.v - y.v); } Z operator*(const Z &x, const Z &y) { return Z(static_cast<uint64_t>(x.v) * y.v % MOD); } Z qpow(Z base, uint e) { Z ret(1); for (; e; e >>= 1) { if (e & 1) { ret *= base; } base *= base; } return ret; } const size_t L = 60; using ull = uint64_t; uint m; struct LB { ull b[L]; LB() { memset(b, 0, sizeof b); } void add(ull x) { for (uint i = m; i--;) { if (x >> i & 1) { if (b[i]) { x ^= b[i]; } else { b[i] = x; for (uint j = i; j--;) { if (b[i] >> j & 1) b[i] ^= b[j]; } for (uint j = i + 1; j < m; ++j) { if (b[j] >> i & 1) b[j] ^= b[i]; } return; } } } } }; ull b[L]; uint cnt; int f[L]; void get_b(const LB &lb) { cnt = 0; for (uint i = 0; i < m; ++i) { if (lb.b[i]) { b[cnt++] = lb.b[i]; } } } void dfs(ull cur = 0, uint i = 0) { if (i == cnt) { ++f[__builtin_popcountll(cur)]; return; } dfs(cur, i + 1); dfs(cur ^ b[i], i + 1); } Z comb[L][L]; void prep_comb() { for (uint i = 0; i < L; ++i) { comb[i][0] = 1; for (uint j = 1; j <= i; ++j) { comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1]; } } } int main() { int n = read(); m = read(); LB b; for (int i = 0; i < n; ++i) { b.add(read()); } get_b(b); assert(cnt <= n); Z mul = qpow(2, n - cnt); if ((cnt << 1) <= m) { dfs(); for (uint i = 0; i <= m; ++i) { cout << f[i] * mul << ' '; } } else { cnt = 0; for (uint i = 0; i < m; ++i) { ull cur = 1ull << i; for (uint j = 0; j < m; ++j) { cur ^= (b.b[j] >> i & 1) << j; } if (cur) { ::b[cnt++] = cur; } } dfs(); mul *= qpow(2, MOD - 1 - cnt); prep_comb(); for (uint i = 0; i <= m; ++i) { Z ans = 0; for (uint j = 0; j <= m; ++j) { for (uint k = 0; k <= i && k <= j; ++k) { Z cur = f[j] * comb[j][k] * comb[m - j][i - k]; if (k & 1) ans -= cur; else ans += cur; } } cout << ans * mul << ' '; } } return 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> namespace IO { static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23; inline char myGetchar() { static char buf[IN_BUF], *ps = buf, *pt = buf; if (ps == pt) { ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin); } return ps == pt ? EOF : *ps++; } template <typename T> inline bool read(T &x) { bool op = 0; char ch = IO::myGetchar(); x = 0; for (; !isdigit(ch) && ch != EOF; ch = IO::myGetchar()) { op ^= (ch == '-'); } if (ch == EOF) { return false; } for (; isdigit(ch); ch = IO::myGetchar()) { x = x * 10 + (ch ^ '0'); } if (op) { x = -x; } return true; } inline int readStr(char *s) { int n = 0; char ch = IO::myGetchar(); for (; isspace(ch) && ch != EOF; ch = IO::myGetchar()) ; for (; !isspace(ch) && ch != EOF; ch = IO::myGetchar()) { s[n++] = ch; } s[n] = '\0'; return n; } inline void myPutchar(char x) { static char pbuf[OUT_BUF], *pp = pbuf; struct _flusher { ~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); } }; static _flusher outputFlusher; if (pp == pbuf + OUT_BUF) { fwrite(pbuf, 1, OUT_BUF, stdout); pp = pbuf; } *pp++ = x; } template <typename T> inline void print_(T x) { if (x == 0) { IO::myPutchar('0'); return; } std::vector<int> num; if (x < 0) { IO::myPutchar('-'); x = -x; } for (; x; x /= 10) { num.push_back(x % 10); } while (!num.empty()) { IO::myPutchar(num.back() ^ '0'); num.pop_back(); } } template <typename T> inline void print(T x, char ch = '\n') { print_(x); IO::myPutchar(ch); } inline void printStr_(const char *s, int n = -1) { if (n == -1) { n = strlen(s); } for (int i = 0; i < n; ++i) { IO::myPutchar(s[i]); } } inline void printStr(const char *s, int n = -1, char ch = '\n') { printStr_(s, n); IO::myPutchar(ch); } } // namespace IO using namespace IO; const int P = 998244353, I2 = (P + 1) >> 1; int n, m, fac[55], inv[55]; std::vector<long long> a, b, c; std::vector<int> id; std::vector<int> f, g; int qpow(int a, int b) { int s = 1; for (; b; b >>= 1) { if (b & 1) { s = 1ll * s * a % P; } a = 1ll * a * a % P; } return s; } bool insert(long long x) { for (int i = m - 1; ~i; --i) { if (x >> i & 1) { if (!a[i]) { a[i] = x; return true; } else { x ^= a[i]; } } } return false; } namespace BF { void dfs(int k, long long now) { if (k == (int)b.size()) { ++f[__builtin_popcountll(now)]; return; } dfs(k + 1, now); dfs(k + 1, now ^ b[k]); } } // namespace BF void dfs(int k, long long now) { if (k == (int)id.size()) { ++g[__builtin_popcountll(now)]; return; } dfs(k + 1, now); dfs(k + 1, now ^ (1ll << id[k]) ^ c[id[k]]); } int C(int n, int m) { if (m < 0 || m > n) { return 0; } return 1ll * fac[n] * inv[m] % P * inv[n - m] % P; } int main() { read(n), read(m); a.resize(m); for (int i = 0; i < n; ++i) { long long x; read(x); insert(x); } for (int i = m - 1; ~i; --i) { if (a[i]) { for (int j = m - 1; j > i; --j) { if (a[j] >> i & 1) { a[j] ^= a[i]; } } } } for (int i = 0; i < m; ++i) { if (a[i]) { b.push_back(a[i]); } else { id.push_back(i); } } if ((int)b.size() < m / 2) { f.resize(m + 1); BF::dfs(0, 0); for (int i = 0; i <= m; ++i) { f[i] = 1ll * f[i] * qpow(2, n - (int)b.size()) % P; print(f[i], ' '); } return 0; } c.resize(m); for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { c[j] |= (long long)(a[i] >> j & 1) << i; } } g.resize(m + 1); dfs(0, 0); fac[0] = 1; for (int i = 1; i <= m; ++i) { fac[i] = 1ll * fac[i - 1] * i % P; } inv[m] = qpow(fac[m], P - 2); for (int i = m; i; --i) { inv[i - 1] = 1ll * inv[i] * i % P; } std::vector<std::vector<int>> F(m + 1, std::vector<int>(m + 1)); for (int i = 0; i <= m; ++i) { for (int j = 0; j <= m; ++j) { for (int k = 0; k <= m; ++k) { int t = 1ll * C(i, k) * C(m - i, j - k) % P; if (k & 1) { F[j][i] = (F[j][i] + P - t) % P; } else { F[j][i] = (F[j][i] + t) % P; } } } } f.resize(m + 1); for (int i = 0; i <= m; ++i) { for (int j = 0; j <= m; ++j) { f[i] = (f[i] + 1ll * F[i][j] * g[j]) % P; } f[i] = 1ll * f[i] * qpow(I2, m - (int)b.size()) % P; f[i] = 1ll * f[i] * qpow(2, n - (int)b.size()) % P; print(f[i], ' '); } }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; using i64 = long long; const int maxN = 223456; const int P = 998244353; int n, m, rnk; i64 base[maxN], p[maxN], dp[60][60][2]; int cnt[maxN], ans[maxN]; void dfs1(int d, i64 x) { if (d == rnk) { cnt[__builtin_popcountll(x)]++; } else { dfs1(d + 1, x); dfs1(d + 1, x ^ p[d]); } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { i64 x; scanf("%lld", &x); for (int j = m - 1; j >= 0; j--) { if (base[j]) { x = min(x, base[j] ^ x); } else if (x & (1ll << j)) { base[j] = x; p[rnk++] = x; break; } } } if (rnk <= m / 2) { dfs1(0, 0); i64 multi = 1; for (int i = 0; i < n - rnk; i++) multi = multi * 2 % P; for (int i = 0; i <= m; i++) { printf("%lld ", cnt[i] * multi % P); } } else { for (int i = m - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) base[i] = min(base[i], base[i] ^ base[j]); } rnk = 0; for (int i = 0; i < m; i++) if (base[i] == 0) { i64 x = (1ll << i); for (int j = i + 1; j < m; j++) if (base[j] & (1ll << i)) { x ^= (1ll << j); } p[rnk++] = x; } dfs1(0, 0); for (int x = 0; x <= m; x++) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int j = 0; j < m; j++) for (int k = 0; k <= m; k++) for (int par = 0; par <= 1; par++) { dp[j + 1][k + 1][par ^ (j < x)] += dp[j][k][par]; dp[j + 1][k][par] += dp[j][k][par]; } for (int k = 0; k <= m; k++) { i64 w = dp[m][k][0] - dp[m][k][1]; w %= P; if (w < 0) w += P; ans[k] = (ans[k] + cnt[x] * w) % P; } } i64 multi = 1; for (int i = 0; i < n; i++) multi = multi * 2 % P; for (int i = 0; i < m; i++) multi = multi * (P + 1) / 2 % P; for (int i = 0; i <= m; i++) { printf("%lld ", ans[i] * multi % P); } } }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 200010; const int M = 60; inline int add(int u, int v) { return (u += v) >= MOD ? u - MOD : u; } inline int sub(int u, int v) { return (u -= v) < 0 ? u + MOD : u; } inline int mul(int u, int v) { return (long long)u * v % MOD; } inline int power(int u, int v) { int res = 1; while (v) { if (v & 1) res = mul(res, u); u = mul(u, u); v >>= 1; } return res; } inline int inv(int u) { return power(u, MOD - 2); } int n, m; long long basis[M]; int k = 0; int p[M]; int c[M][M]; int num[M][M]; int add(long long u) { for (int i = m - 1; i >= 0; i--) { if (u >> i & 1) { if (basis[i]) u ^= basis[i]; else { basis[i] = u; k++; return 1; } } } return 0; } void go(int u, long long mask, int m) { if (u == m) { int numBit = __builtin_popcountll(mask); p[numBit] = add(p[numBit], 1); return; } go(u + 1, mask, m); if (basis[u]) go(u + 1, mask ^ basis[u], m); } int getComb(int n, int k) { if (k < 0 || k > n) return 0; return c[n][k]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; int ways = 1; for (int i = 1; i <= n; i++) { long long u; cin >> u; if (!add(u)) { ways = mul(ways, 2); } } if (k <= 27) { go(0, 0, m); for (int i = 0; i <= m; i++) { cout << mul(p[i], ways) << ' '; } cout << endl; } else { for (int i = m - 1; i >= 0; i--) { if (!basis[i]) continue; for (int j = m - 1; j > i; j--) { if (!basis[j]) continue; if (basis[j] >> i & 1) basis[j] ^= basis[i]; } } int cur = m - 1; for (int i = m - 1; i >= 0; i--) { if (basis[i]) { for (int j = m - 1; j >= 0; j--) { long long u = (basis[j] >> i & 1); long long v = (basis[j] >> cur & 1); basis[j] ^= (u << i) ^ (v << i) ^ (u << cur) ^ (v << cur); } swap(basis[i], basis[cur]); cur--; } } for (int i = 0; i < m - k; i++) { basis[i] ^= (1ll << i); for (int j = m - k; j < m; j++) { basis[i] ^= ((basis[j] >> i & 1ll) << j); } } go(0, 0, m - k); for (int i = 0; i < M; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) c[i][j] = 1; else c[i][j] = add(c[i - 1][j], c[i - 1][j - 1]); } } for (int c = 0; c <= m; c++) { for (int j = 0; j <= m; j++) { for (int x = 0; x <= j; x++) { int now = mul(getComb(j, x), getComb(m - j, c - x)); if (x & 1) num[c][j] = sub(num[c][j], now); else num[c][j] = add(num[c][j], now); } } } int inv2 = inv(2); for (int i = 1; i <= m - k; i++) { ways = mul(ways, inv2); } for (int c = 0; c <= m; c++) { int res = 0; for (int i = 0; i <= m; i++) { res = add(res, mul(p[i], num[c][i])); } cout << mul(ways, res) << ' '; } cout << endl; } return 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> template <class T> inline void read(T &x) { x = 0; register char c = getchar(); register bool f = 0; while (!isdigit(c)) f ^= c == '-', c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); if (f) x = -x; } const int N = 60, mod = 998244353; int n, m, k, t, c[N]; long long f[N], g[N]; struct z { int x; z(int x = 0) : x(x) {} friend inline z operator*(z a, z b) { return (long long)a.x * b.x % mod; } friend inline z operator-(z a, z b) { return (a.x -= b.x) < 0 ? a.x + mod : a.x; } friend inline z operator+(z a, z b) { return (a.x += b.x) >= mod ? a.x - mod : a.x; } } p[N], q[N], fac[N], ifac[N]; inline z C(int n, int m) { return n < m ? 0 : fac[n] * ifac[m] * ifac[n - m]; } inline z fpow(z a, int b) { z s = 1; for (; b; b >>= 1, a = a * a) if (b & 1) s = s * a; return s; } inline void insert(long long x) { for (int i = m - 1; i >= 0; i--) if ((x >> i) & 1) { if (f[i]) x ^= f[i]; else { f[i] = x; return; } } } int main() { read(n), read(m); for (int i = 1; i <= n; i++) { long long x; read(x), insert(x); } for (int i = 0; i < m; i++) for (int j = i + 1; j < m; j++) if ((f[j] >> i) & 1) f[j] ^= f[i]; for (int i = 0; i < m; i++) if (f[i]) c[i] = k++; for (int i = 0; i < m; i++) if (!f[i]) c[i] = k + (t++); for (int i = 0; i < m; i++) if (f[i]) for (int j = 0; j < m; j++) if ((f[i] >> j) & 1) g[c[i]] |= 1ll << c[j]; for (int i = 0; i < k; i++) for (int j = k; j < m; j++) if ((g[i] >> j) & 1) g[j] |= 1ll << i; for (int i = k; i < m; i++) g[i] |= 1ll << i; if (k <= ((m + 1) >> 1)) { std::function<void(int, long long)> dfs = [&](int i, long long s) { if (i >= k) { p[__builtin_popcountll(s)].x++; return; } dfs(i + 1, s), dfs(i + 1, s ^ g[i]); }; dfs(0, 0ll); } else { std::function<void(int, long long)> dfs = [&](int i, long long s) { if (i >= m) { q[__builtin_popcountll(s)].x++; return; } dfs(i + 1, s), dfs(i + 1, s ^ g[i]); }; dfs(k, 0ll); fac[0] = ifac[0] = ifac[1] = 1; for (int i = 1; i <= m; i++) fac[i] = fac[i - 1] * i; for (int i = 2; i <= m; i++) ifac[i] = (mod - mod / i) * ifac[mod % i]; for (int i = 1; i <= m; i++) ifac[i] = ifac[i - 1] * ifac[i]; for (int c = 0; c <= m; c++) for (int d = 0; d <= m; d++) for (int i = 0; i <= c; i++) { p[c] = p[c] + fpow(2, mod - 1 + k - m) * q[d] * (i & 1 ? mod - 1 : 1) * C(d, i) * C(m - d, c - i); } } for (int i = 0; i <= m; i++) printf("%d%c", (p[i] * fpow(2, n - k)).x, " \n"[i == m]); }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> namespace { using u64 = uint64_t; static const int MOD = 998244353; struct LargeSolver { LargeSolver(int r_, int m_, std::vector<u64> &&b_) : r{r_}, m{m_}, b{std::move(b_)}, count(m + 1), result(m + 1) { dfs_large(0, 0, 0); int power_of_two = 1; for (int i = 0; i < m - r; ++i) { power_of_two = ((MOD + 1LL) / 2) * power_of_two % MOD; } for (int d = 0; d <= m; ++d) { std::vector<int> coef(m + 1); coef[0] = power_of_two; for (int i = 0; i < m; ++i) { for (int j = i + 1; j > 0; --j) { coef[j] += (i < d ? MOD - 1LL : 1LL) * coef[j - 1] % MOD; if (coef[j] >= MOD) { coef[j] -= MOD; } } } for (int c = 0; c <= m; ++c) { result[c] += 1LL * coef[c] * count[d] % MOD; if (result[c] >= MOD) { result[c] -= MOD; } } } } void dfs_large(int i, int c, u64 x) { if (i < m - r) { dfs_large(i + 1, c, x); dfs_large(i + 1, c + 1, x ^ b[i]); } else { count[c + __builtin_popcountll(x)]++; } } int r, m; std::vector<u64> b; std::vector<int> count, result; }; struct Solver { Solver(int n_, int m_, std::vector<u64> &&a_) : n{n_}, m{m_}, rank{0}, a{std::move(a_)}, result(m + 1) { std::vector<int> notpivot_bits; for (int j = 0; j < m; ++j) { int pivot = rank; while (pivot < n && (~a[pivot] >> j & 1)) { pivot++; } if (pivot < n) { std::swap(a[rank], a[pivot]); for (int i = 0; i < n; ++i) { if (i != rank && a[i] >> j & 1) { a[i] ^= a[rank]; } } rank++; } else { notpivot_bits.push_back(j); } } if (rank <= m - rank) { dfs_small(0, 0); } else { std::vector<u64> basis; basis.reserve(m - rank); for (int j : notpivot_bits) { u64 b = 0; for (int i = 0; i < rank; ++i) { b |= (a[i] >> j & 1) << i; } basis.push_back(b); } LargeSolver solver{rank, m, std::move(basis)}; std::copy(solver.result.begin(), solver.result.end(), result.begin()); } int power_of_two = 1; for (int i = rank; i < n; ++i) { power_of_two += power_of_two; if (power_of_two >= MOD) { power_of_two -= MOD; } } for (int i = 0; i <= m; ++i) { result[i] = 1LL * power_of_two * result[i] % MOD; } } void dfs_small(int i, u64 x) { if (i < rank) { dfs_small(i + 1, x); dfs_small(i + 1, x ^ a[i]); } else { result[__builtin_popcountll(x)]++; } } int n, m, rank; std::vector<u64> a; std::vector<int> result; }; } // namespace int main() { std::ios::sync_with_stdio(false); int n, m; std::cin >> n >> m; std::vector<u64> a(n); for (int i = 0; i < n; ++i) { std::cin >> a[i]; } Solver solver{n, m, std::move(a)}; for (int i = 0; i <= m; ++i) { printf("%d%c", solver.result[i], " \n"[i == m]); } }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> #pragma GCC target("popcnt,sse4.2") using namespace std; const int mod = 998244353; inline int add(int a, int b) { if ((a += b) >= mod) a -= mod; return a; } inline int dec(int a, int b) { if ((a -= b) < 0) a += mod; return a; } inline int mult(int a, int b) { long long t = 1ll * a * b; if (t >= mod) t %= mod; return t; } inline int power(int a, int b) { int out = 1; while (b) { if (b & 1) out = mult(out, a); a = mult(a, a); b >>= 1; } return out; } int n, m; long long a[200010]; class linear_base { public: long long dat[60]; int id[60]; vector<int> pos; int insert(long long x) { for (int i = m - 1; i >= 0; i--) { if ((x >> i) & 1) { if (!dat[i]) { dat[i] = x; pos.push_back(i); return 1; } x ^= dat[i]; } } return 0; } long long ts[60], S[60]; void regulate() { for (int i = m - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) { if (dat[j] && ((dat[i] >> j) & 1)) dat[i] ^= dat[j]; } } int idcnt = 0; for (int i = 0; i < m; i++) if (!dat[i]) id[i] = idcnt++; for (int i = m - 1; i >= 0; i--) { if (dat[i]) for (int j = i - 1; j >= 0; j--) if (!dat[j] && ((dat[i] >> j) & 1)) S[i] |= (1ll << id[j]); } for (int i = 0; i < m; i++) { if (!dat[i]) { for (int j = 0; j < pos.size(); j++) if ((S[pos[j]] >> id[i]) & 1) ts[id[i]] |= (1ll << j); } } } } L; int K, b[200010], cnt[60], C[60][60]; void dfs(int np, long long S) { if (np == K) { cnt[__builtin_popcountll(S)]++; return; } dfs(np + 1, S); dfs(np + 1, S ^ L.dat[L.pos[np]]); } int cc[60]; void dfs2(int np, long long S, int curcnt) { if (np == m - K) { cc[curcnt + __builtin_popcountll(S)]++; return; } dfs2(np + 1, S ^ L.ts[np], curcnt + 1); dfs2(np + 1, S, curcnt); } int main() { scanf("%d%d", &n, &m); K = 0; for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= n; i++) K += (b[i] = L.insert(a[i])); if (K <= 27) { dfs(0, 0); for (int i = 0; i <= m; i++) printf("%d ", mult(cnt[i], power(2, n - K))); } else { L.regulate(); C[0][0] = 1; for (int i = 1; i < 60; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]); } dfs2(0, 0, 0); for (int T = 0; T <= m; T++) { int cur = 0; for (int pcnt = 0; pcnt <= m; pcnt++) { for (int i = 0; i <= min(T, pcnt); i++) { int tmp = mult(mult(C[pcnt][i], C[m - pcnt][T - i]), cc[pcnt]); if (!(i & 1)) cur = add(cur, tmp); else cur = dec(cur, tmp); } } cur = mult(mult(cur, power(2, K)), power(power(2, m), mod - 2)); printf("%d ", mult(cur, power(2, n - K))); } } return 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; void exgcd(int a, int b, int& x, int& y) { if (!b) { x = 1, y = 0; } else { exgcd(b, a % b, y, x); y -= (a / b) * x; } } int inv(int a, int n) { int x, y; exgcd(a, n, x, y); return (x < 0) ? (x + n) : (x); } const int Mod = 998244353; template <const int Mod = ::Mod> class Z { public: int v; Z() : v(0) {} Z(int x) : v(x) {} Z(long long x) : v(x % Mod) {} friend Z operator+(const Z& a, const Z& b) { int x; return Z(((x = a.v + b.v) >= Mod) ? (x - Mod) : (x)); } friend Z operator-(const Z& a, const Z& b) { int x; return Z(((x = a.v - b.v) < 0) ? (x + Mod) : (x)); } friend Z operator*(const Z& a, const Z& b) { return Z(a.v * 1ll * b.v); } friend Z operator~(const Z& a) { return inv(a.v, Mod); } friend Z operator-(const Z& a) { return Z(0) - a; } Z& operator+=(Z b) { return *this = *this + b; } Z& operator-=(Z b) { return *this = *this - b; } Z& operator*=(Z b) { return *this = *this * b; } friend bool operator==(const Z& a, const Z& b) { return a.v == b.v; } }; Z<> qpow(Z<> a, int p) { Z<> rt = Z<>(1), pa = a; for (; p; p >>= 1, pa = pa * pa) { if (p & 1) { rt = rt * pa; } } return rt; } const int bzmax = 60; int n, m; typedef class LinearBasis { public: long long a[bzmax]; LinearBasis() { memset(a, 0, sizeof(a)); } bool insert(long long x) { for (int i = m; i-- && x;) { if ((x >> i) & 1) { x ^= a[i]; } if ((x >> i) & 1) { a[i] = x; for (int j = i - 1; ~j; j--) { if ((a[i] >> j) & 1) { a[i] ^= a[j]; } } for (int j = i + 1; j < m; j++) { if ((a[j] >> i) & 1) { a[j] ^= a[i]; } } return true; } } return false; } LinearBasis dual() { LinearBasis rt; for (int i = m; i--;) { if (!a[i]) { long long x = 1ll << i; for (int j = i + 1; j < m; j++) { if ((a[j] >> i) & 1) { x |= 1ll << j; } } rt.insert(x); } } return rt; } } LinearBasis; LinearBasis lb; vector<Z<> > calc(LinearBasis& lb) { vector<long long> a; for (int i = 0; i < m; i++) { if (lb.a[i]) { a.push_back(lb.a[i]); } } int sz = a.size(); int half = sz >> 1; vector<long long> vl(1 << half, 0ll), vr(1 << (sz - half), 0ll); for (int s = 0, _ = 1 << half; s < _; s++) { for (int i = 0; i < half; i++) { if ((s >> i) & 1) { vl[s] ^= a[i]; } } } for (int s = 0, _ = 1 << (sz - half); s < _; s++) { for (int i = 0; i < sz - half; i++) { if ((s >> i) & 1) { vr[s] ^= a[i + half]; } } } vector<int> rt(m + 1, 0); int msk = (1 << half) - 1; for (int s = 0, _ = 1 << sz; s < _; s++) { rt[__builtin_popcountll(vl[s & msk] ^ vr[s >> half])]++; } vector<Z<> > _rt(m + 1); for (int i = 0; i <= m; i++) { _rt[i] = rt[i]; } return _rt; } Z<> f[60][60]; int main() { scanf("%d%d", &n, &m); int rk = 0; for (int i = 0; i < n; i++) { long long x; scanf("%lld", &x); rk += lb.insert(x); } vector<Z<> > ans; if (rk * 2 <= m) { ans = calc(lb); } else { lb = lb.dual(); auto v = calc(lb); f[0][0] = 1; for (int i = 1; i <= m; i++) { for (int j = i; j; j--) { f[0][j] += f[0][j - 1]; } } for (int i = 1; i <= m; i++) { memcpy(f[i], f[i - 1], sizeof(Z<>) * (m + 1)); for (int j = 1; j <= m; j++) { f[i][j] -= f[i][j - 1]; } for (int j = m; j; j--) { f[i][j] -= f[i][j - 1]; } } ans.resize(m + 1, Z<>(0)); for (int k = 0; k <= m; k++) { for (int c = 0; c <= m; c++) { ans[k] += f[c][k] * v[c]; } } Z<> coef = ~qpow(2, m - rk); for (auto& x : ans) { x *= coef; } } Z<> coef = qpow(2, n - rk); for (auto x : ans) { x *= coef; printf("%d ", x.v); } return 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; const int mo = 998244353; int n, m, s1, cnt[65536]; long long a[55], x, ans[55]; void insert(long long x) { for (int i = (int)(m - 1); i >= (int)(0); i--) if (x & (1ll << i)) if (!a[i]) { a[i] = x; break; } else x ^= a[i]; } int Count(long long x) { return cnt[x & 65535] + cnt[(x >> 16) & 65535] + cnt[(x >> 32) & 65535] + cnt[(x >> 48) & 65535]; } void get_que(long long *q) { static long long q1[1 << 16]; static long long q2[1 << 16]; int m = (*q) / 2, l1 = m, l2 = (*q) - m; memset(q1, 0, sizeof(q1)); memset(q2, 0, sizeof(q2)); for (int i = (int)(0); i <= (int)((1 << l1) - 1); i++) for (int j = (int)(0); j <= (int)(l1 - 1); j++) if (i & (1 << j)) q1[i] ^= q[j + 1]; for (int i = (int)(0); i <= (int)((1 << l2) - 1); i++) for (int j = (int)(0); j <= (int)(l2 - 1); j++) if (i & (1 << j)) q2[i] ^= q[j + 1 + l1]; for (int i = (int)(0); i <= (int)((1 << l1) - 1); i++) for (int j = (int)(0); j <= (int)((1 << l2) - 1); j++) ++ans[Count(q1[i] ^ q2[j])]; } void axiba1() { static long long q[55]; for (int i = (int)(0); i <= (int)(m - 1); i++) if (a[i]) q[++*q] = a[i]; get_que(q); } void axiba2() { static long long q[55], f[55]; for (int i = (int)(m - 1); i >= (int)(0); i--) if (a[i]) for (int j = (int)(i + 1); j <= (int)(m - 1); j++) if (a[j] & (1ll << i)) a[j] ^= a[i]; for (int i = (int)(0); i <= (int)(m - 1); i++) if (!a[i]) { q[++*q] = 1ll << i; for (int j = (int)(0); j <= (int)(m - 1); j++) if (a[j] & (1ll << i)) q[*q] |= 1ll << j; } get_que(q); int top = *q; memcpy(q, ans, sizeof(q)); memset(ans, 0, sizeof(ans)); for (int i = (int)(0); i <= (int)(m); i++) { memset(f, 0, sizeof(f)); f[0] = 1; for (int j = (int)(1); j <= (int)(m); j++) for (int k = (int)(m - 1); k >= (int)(0); k--) f[k + 1] += ((j <= i) ? -f[k] : f[k]); for (int j = (int)(0); j <= (int)(m); j++) ans[j] += (q[i] % mo) * (f[j] % mo); } for (int i = (int)(0); i <= (int)(m); i++) ans[i] = (ans[i] % mo + mo) % mo; for (int i = (int)(0); i <= (int)(m); i++) for (int j = (int)(1); j <= (int)(top); j++) ans[i] = 1ll * ans[i] * (mo + 1) / 2 % mo; } int main() { scanf("%d%d", &n, &m); for (int i = (int)(0); i <= (int)((1 << 16) - 1); i++) cnt[i] = cnt[i / 2] + (i & 1); for (int i = (int)(1); i <= (int)(n); i++) scanf("%lld", &x), insert(x); for (int i = (int)(0); i <= (int)(m - 1); i++) if (a[i]) s1++; if (s1 <= 27) axiba1(); else axiba2(); int val = 1; for (int i = (int)(s1 + 1); i <= (int)(n); i++) val = 2ll * val % mo; for (int i = (int)(0); i <= (int)(m); i++) printf("%d ", 1ll * ans[i] % mo * val % mo); }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; inline void add(int &x, int y) { (x += y) >= mod ? x -= mod : 0; } inline int pl(int x, int y) { return (x += y) >= mod ? x - mod : x; } inline int kpow(int a, int b) { int s = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) s = 1ll * s * a % mod; return s; } int n, m, ans[60], C[60][60], coef[60][60], po[200020], cnt[60]; long long p[60], a[60], b[60]; int cnta, cntb, pa[60]; void ins(long long x) { for (int i = m - 1; ~i; --i) if (x >> i & 1) { if (!p[i]) p[i] = x; x ^= p[i]; } } void work() { for (int i = 0; i < m; ++i) for (int j = i - 1; ~j; --j) if ((p[i] >> j & 1) && p[j]) p[i] ^= p[j]; } void dfs(int pos, long long w) { if (pos == cnta + 1) { ++ans[__builtin_popcountll(w)]; return; } dfs(pos + 1, w); dfs(pos + 1, w ^ a[pos]); } void dfs1(int pos, long long w) { if (pos == cntb + 1) { ++cnt[__builtin_popcountll(w)]; return; } dfs1(pos + 1, w); dfs1(pos + 1, w ^ b[pos]); } int main() { scanf("%d %d", &n, &m); for (int i = 0; i <= m; ++i) { C[i][0] = 1; for (int j = 1; j <= i; ++j) C[i][j] = pl(C[i - 1][j - 1], C[i - 1][j]); } for (int i = 1; i <= n; ++i) { static long long x; scanf("%lld", &x); ins(x); } work(); for (int i = 0; i < m; ++i) if (p[i]) a[++cnta] = p[i], pa[cnta] = i; else b[++cntb] = 1ll << i; if (cnta <= 26) { dfs(1, 0); int po = kpow(2, n - cnta) % mod; for (int i = 0; i <= m; ++i) printf("%d ", 1ll * ans[i] * po % mod); } else { for (int i = 1; i <= cntb; ++i) for (int j = 1; j <= cnta; ++j) if (a[j] & b[i]) b[i] ^= 1ll << pa[j]; dfs1(1, 0); for (int i = 0; i <= m; ++i) for (int j = 0; j <= m; ++j) { for (int k = 0; k <= j; ++k) if (k & 1) coef[i][j] = (coef[i][j] + 1ll * (mod - C[i][k]) * C[m - i][j - k]) % mod; else coef[i][j] = (coef[i][j] + 1ll * C[i][k] * C[m - i][j - k]) % mod; ans[j] = (ans[j] + 1ll * coef[i][j] * cnt[i] % mod) % mod; } int po = kpow(2, n - m + mod - 1); for (int i = 0; i <= m; ++i) printf("%d ", 1ll * ans[i] * po % mod); } return 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; long long mul(long long a, long long b) { if (!b) return 1; long long k = mul(a, b / 2); if (b & 1) return k * k % 998244353 * a % 998244353; return k * k % 998244353; } int n, m, k; long long b[64], arr[64], C[64][64], w[64][64], ans[64]; long long val; bool mark[64]; void add(long long x) { for (int i = m - 1; i >= 0; i--) { if (!x) break; if (!(x >> i & 1)) continue; if (!b[i]) { b[i] = x; arr[k++] = x; } x ^= b[i]; } } void GaussJordan() { for (int i = m - 1; i >= 0; i--) if (b[i]) for (int j = i - 1; j >= 0; j--) if (b[j] && (b[i] >> j & 1)) b[i] ^= b[j]; } void dfs(int pos, long long sum) { if (pos >= k) { ans[__builtin_popcount(sum & (1ll << 32) - 1) + __builtin_popcount(sum >> 32)]++; return; } dfs(pos + 1, sum ^ arr[pos]); dfs(pos + 1, sum); } void doit() { dfs(0, 0); for (int i = 0; i <= m; i++) cout << (ans[i] * val) % 998244353 << ' '; cout << endl; } void calcB() { for (int i = 0; i < m; i++) if (!b[i]) { mark[i] = true; b[i] = (1ll << i); } for (int i = m - 1; i >= 0; i--) for (int j = i - 1; j >= 0; j--) if (b[i] >> j & 1) b[j] |= (1ll << i); k = 0; for (int i = 0; i < m; i++) if (mark[i]) { cerr << b[i] << endl; arr[k++] = b[i]; } } void calcw() { for (int i = 0; i <= m; i++) C[i][0] = 1; for (int i = 1; i <= m; i++) for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % 998244353; for (int c = 0; c <= m; c++) for (int d = 0; d <= m; d++) { int dir = 1; for (int i = 0; i <= c; i++) { (w[c][d] += dir * C[d][i] * C[m - d][c - i] % 998244353 + 998244353) %= 998244353; dir = -dir; } } } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++) { long long x; cin >> x; add(x); } GaussJordan(); val = mul(2, n - k); if (k <= 26) { doit(); return 0; } calcB(); dfs(0, 0); calcw(); for (int c = 0; c <= m; c++) { long long p = 0; for (int d = 0; d <= m; d++) (p += w[c][d] * ans[d] % 998244353) %= 998244353; (p *= mul(mul(2, k), 998244353 - 2)) %= 998244353; cout << p * val % 998244353 << ' '; } cout << endl; return 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; const int mn = 1e5 + 10; const int mm = 53; const long long mod = 998244353; long long basis[mm]; int m; bool add(long long x) { for (int i = m - 1; i >= 0; i--) if ((x >> i) & 1) { if (basis[i]) x ^= basis[i]; else { basis[i] = x; return 1; } } return 0; } long long ans[mm + 1], num[mn + 1]; long long ch[mm + 10][mm + 10]; long long ve[mm + 10][mm + 10]; int rep[mm]; long long rehail(long long x) { long long ans = 0; for (int i = 0; i < m; i++) ans |= ((x >> i) & 1) << rep[i]; return ans; } int main() { int n, k = 0; long long ex = 1; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { long long x; scanf("%lld", &x); if (add(x)) k++; else ex = ex * 2 % mod; } vector<long long> bas; memset(rep, -1, sizeof(rep)); int cur = 0; for (int i = m - 1; i >= 0; i--) if (basis[i]) { for (int j = i - 1; j >= 0; j--) if (basis[j] && ((basis[i] >> j) & 1)) basis[i] ^= basis[j]; } for (int i = 0; i < m; i++) if (basis[i]) rep[i] = cur++, bas.push_back(basis[i]); for (int i = 0; i < m; i++) if (!basis[i]) rep[i] = cur++; for (long long& x : bas) x = rehail(x); if (k <= 27) { long long xs = 0; ans[0]++; for (int i = 1; i < 1 << k; i++) { for (int j = __builtin_ctz(i); j >= 0; j--) xs ^= bas[j]; ans[__builtin_popcountll(xs)]++; } for (int i = 0; i <= m; i++) printf("%lld ", ans[i] * ex % mod); } else { vector<long long> rbas; for (int i = k; i < m; i++) { long long x = 0; for (int j = 0; j < k; j++) x |= ((long long)(bas[j] >> i) & 1) << j; x |= 1LL << i; rbas.push_back(x); } ch[0][0] = 1; for (int i = 1; i < m + 10; i++) { ch[i][0] = 1; for (int j = 1; j < m + 10; j++) { ch[i][j] = (ch[i - 1][j - 1] + ch[i - 1][j]) % mod; } } for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int l = 0; l <= min(i, j); l++) { if (l & 1) { ve[i][j] -= ch[j][l] * ch[m - j][i - l]; } else { ve[i][j] += ch[j][l] * ch[m - j][i - l]; } ve[i][j] %= mod; } } } num[0]++; long long xs = 0; for (int i = 1; i < 1 << m - k; i++) { for (int j = __builtin_ctz(i); j >= 0; j--) xs ^= rbas[j]; num[__builtin_popcountll(xs)]++; } for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { ans[i] += num[j] * ve[i][j], ans[i] %= mod; } } for (int i = 0; i < m - k; i++) ex *= (mod + 1) / 2, ex %= mod; for (int i = 0; i <= m; i++) { if (ans[i] < 0) ans[i] += mod; printf("%lld ", ans[i] * ex % mod); } } }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } namespace IO { const int BUFFER_SIZE = 1 << 15; char input_buffer[BUFFER_SIZE]; int input_pos = 0, input_len = 0; void _update_input_buffer() { input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin); input_pos = 0; if (input_len == 0) input_buffer[0] = EOF; } inline char next_char(bool advance = true) { if (input_pos >= input_len) _update_input_buffer(); return input_buffer[advance ? input_pos++ : input_pos]; } template <typename T> inline void read_int(T &number) { bool negative = false; number = 0; while (!isdigit(next_char(false))) if (next_char() == '-') negative = true; do { number = 10 * number + (next_char() - '0'); } while (isdigit(next_char(false))); if (negative) number = -number; } template <typename T, typename... Args> inline void read_int(T &number, Args &...args) { read_int(number); read_int(args...); } } // namespace IO template <const int &MOD> struct _m_int { int val; _m_int(int64_t v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static int inv_mod(int a, int m = MOD) { int g = m, r = a, x = 0, y = 1; while (r != 0) { int q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } explicit operator int() const { return val; } explicit operator int64_t() const { return val; } _m_int &operator+=(const _m_int &other) { val -= MOD - other.val; if (val < 0) val += MOD; return *this; } _m_int &operator-=(const _m_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; unsigned x_high = x >> 32, x_low = (unsigned)x; unsigned quot, rem; asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m)); return rem; } _m_int &operator*=(const _m_int &other) { val = fast_mod((uint64_t)val * other.val); return *this; } _m_int &operator/=(const _m_int &other) { return *this *= other.inv(); } friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; } friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; } friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; } friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; } _m_int &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } _m_int &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } _m_int operator++(int) { _m_int before = *this; ++*this; return before; } _m_int operator--(int) { _m_int before = *this; --*this; return before; } _m_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const _m_int &other) const { return val == other.val; } bool operator!=(const _m_int &other) const { return val != other.val; } _m_int inv() const { return inv_mod(val); } _m_int pow(int64_t p) const { if (p < 0) return inv().pow(-p); _m_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } friend ostream &operator<<(ostream &os, const _m_int &m) { return os << m.val; } }; extern const int MOD = 998244353; using mod_int = _m_int<MOD>; const int BITS = 53; template <typename T> struct xor_basis { T basis[BITS]; int n = 0; T min_value(T start) const { if (n == BITS) return 0; for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]); return start; } T max_value(T start = 0) const { if (n == BITS) return ((T)1 << BITS) - 1; for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]); return start; } bool add(T x) { x = min_value(x); if (x == 0) return false; basis[n++] = x; for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--) swap(basis[k], basis[k - 1]); return true; } void merge(const xor_basis<T> &other) { for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]); } void merge(const xor_basis<T> &a, const xor_basis<T> &b) { if (a.n > b.n) { *this = a; merge(b); } else { *this = b; merge(a); } } }; const int POPCOUNT_BITS = 14; const int POPCOUNT_MASK = (1 << POPCOUNT_BITS) - 1; vector<int8_t> _popcount; void build_popcount() { _popcount.resize(1 << POPCOUNT_BITS); for (int x = 0; x < 1 << POPCOUNT_BITS; x++) _popcount[x] = _popcount[x >> 1] + (x & 1); } int popcountll(int64_t x) { return _popcount[x & POPCOUNT_MASK] + _popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] + _popcount[x >> (2 * POPCOUNT_BITS) & POPCOUNT_MASK] + _popcount[x >> (3 * POPCOUNT_BITS)]; } int popcount(int x) { return _popcount[x & POPCOUNT_MASK] + _popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] + _popcount[x >> (2 * POPCOUNT_BITS)]; } void row_reduce(vector<int64_t> &basis) { int B = basis.size(); for (int i = 0; i < B; i++) for (int j = i + 1; j < B; j++) basis[i] = min(basis[i], basis[i] ^ basis[j]); } vector<int64_t> generate_all(vector<int64_t> basis, int M) { row_reduce(basis); int B = basis.size(); vector<int64_t> change(B + 1, 0); int64_t prefix = 0; for (int i = 0; i < B; i++) { prefix ^= basis[i]; change[i] = prefix; } change[B] = prefix; vector<int64_t> counts(M + 1, 0); int64_t value = 0; for (int64_t mask = 0; mask < 1LL << B; mask++) { counts[popcountll(value)]++; value ^= change[__builtin_ctzll(mask + 1)]; } return counts; } vector<int64_t> build_orthogonal(vector<int64_t> basis, int M) { row_reduce(basis); int B = basis.size(); vector<int> highest_bits(B, -1); vector<bool> bit_covered(M, false); for (int i = 0; i < B; i++) { highest_bits[i] = 63 - __builtin_clzll(basis[i]); bit_covered[highest_bits[i]] = true; } vector<int64_t> orthogonal; for (int bit = M - 1; bit >= 0; bit--) if (!bit_covered[bit]) { int64_t value = 1LL << bit; for (int i = 0; i < B; i++) if (basis[i] >> bit & 1) value |= 1LL << highest_bits[i]; orthogonal.push_back(value); } return orthogonal; } vector<int64_t> generate_from_orthogonal(vector<int64_t> basis, int M) { row_reduce(basis); int B = basis.size(); vector<int64_t> orthogonal = build_orthogonal(basis, M); vector<int64_t> orthogonal_counts = generate_all(orthogonal, M); vector<int64_t> counts(M + 1, 0); vector<vector<int64_t>> choose(M + 1); for (int n = 0; n <= M; n++) { choose[n].resize(n + 1); choose[n][0] = choose[n][n] = 1; for (int r = 1; r < n; r++) choose[n][r] = choose[n - 1][r - 1] + choose[n - 1][r]; } auto &&get_choose = [&](int n, int r) -> int64_t { if (r < 0 || r > n) return 0; return choose[n][r]; }; for (int c = 0; c <= M; c++) { vector<int64_t> w(M + 1, 0); for (int d = 0; d <= M; d++) for (int j = 0; j <= d; j++) w[d] += (j % 2 == 0 ? 1 : -1) * get_choose(d, j) * get_choose(M - d, c - j); for (int d = 0; d <= M; d++) counts[c] += orthogonal_counts[d] * w[d]; assert((counts[c] & ((1LL << (M - B)) - 1)) == 0); counts[c] >>= M - B; } return counts; } int main() { build_popcount(); int N, M; IO::read_int(N, M); xor_basis<int64_t> basis_obj; for (int i = 0; i < N; i++) { int64_t a; IO::read_int(a); basis_obj.add(a); } int B = basis_obj.n; vector<int64_t> basis(B); for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i]; vector<int64_t> answers; if (B <= M / 2) answers = generate_all(basis, M); else answers = generate_from_orthogonal(basis, M); for (int i = 0; i <= M; i++) { mod_int answer = answers[i]; answer *= mod_int(2).pow(N - B); cout << answer << (i < M ? ' ' : '\n'); } }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int n, m; vector<long long> bs; int cnt[(1 << 16) + 1]; long long inv2 = (998244353 + 1) / 2; int popcount(long long x) { return cnt[x & 65535] + cnt[x >> 16 & 65535] + cnt[x >> 32 & 65535] + cnt[x >> 48 & 65535]; } bool add(long long x) { for (auto& t : bs) x = min(x, t ^ x); if (!x) return false; for (int i = 0; i < ((int)(bs).size() + 1); ++i) if (i == (int)(bs).size() || x > bs[i]) { bs.insert(bs.begin() + i, x); return true; } } void build() { for (int i = 0; i < (int)(bs).size(); i++) { for (int j = i + 1; j < (int)(bs).size(); j++) bs[i] = min(bs[i], bs[i] ^ bs[j]); } for (int i = 0; i < (int)(bs).size(); i++) { int l = m - 1 - i; if (!(bs[i] >> l & 1)) { int r = -1; for (int j = l - 1; j >= 0; j--) { if (bs[i] >> j & 1) { r = j; break; } } for (int j = 0; j < (int)(bs).size(); j++) { long long a = (bs[j] >> l & 1), b = (bs[j] >> r & 1); if (a != b) bs[j] ^= (1LL << l) | (1LL << r); } } } while ((int)(bs).size() < m) bs.push_back((1LL << (m - 1 - (int)(bs).size()))); for (int i = 0; i < (int)(bs).size(); i++) { int l = m - 1 - i; for (int j = l + 1; j < (int)(bs).size(); j++) { bs[i] |= (bs[m - 1 - j] >> (m - 1 - i) & 1) << j; } } } long long cur = 0; long long pk = 1; long long res[55], fres[55]; long long C[55][55], f[55][55]; void dfs(int d) { if (d == (int)(bs).size()) { res[popcount(cur)]++; return; } dfs(d + 1); cur ^= bs[d]; dfs(d + 1); cur ^= bs[d]; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); for (int i = 1; i < (1 << 16); i++) { cnt[i] = cnt[i / 2] + i % 2; } cin >> n >> m; for (int i = 0; i < n; i++) { long long x; cin >> x; if (!add(x)) pk *= 2, pk %= mod; } if ((int)(bs).size() <= 27) { dfs(0); for (int i = 0; i <= m; i++) cout << (pk * res[i] % mod + mod) % mod << " "; cout << "\n"; } else { for (int i = 0; i < 55; i++) C[i][i] = C[i][0] = 1; for (int i = 0; i < 55; i++) { for (int j = 1; j < i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; } for (int b = 0; b <= m; b++) { for (int c = 0; c <= m; c++) { for (int a = 0; a <= b && a <= c; a++) { int s = (a % 2 == 0 ? 1 : -1); f[b][c] = (f[b][c] + s * C[b][a] * C[m - b][c - a]) % mod; } } } int k = (int)(bs).size(); build(); dfs(k); long long pmk = 1; for (int i = 0; i < m - k; i++) pk = (pk * inv2) % mod; for (int c = 0; c <= m; c++) { for (int b = 0; b <= m; b++) { fres[c] += (f[b][c] * res[b]); fres[c] %= mod; } } for (int i = 0; i <= m; i++) cout << (pk * fres[i] % mod + mod) % mod << " "; cout << "\n"; } }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> template <class T> inline void read(T &res) { res = 0; bool bo = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-') ; if (c == '-') bo = 1; else res = c - 48; while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + (c - 48); if (bo) res = ~res + 1; } const int N = 60, djq = 998244353, i2 = 499122177; int n, m, orz = 1, cnt1, p[N], cnt0, cnt[N], ans[N], C[N][N]; long long b[N], a[N]; void ins(long long x) { for (int i = m - 1; i >= 0; i--) { if (!((x >> i) & 1)) continue; if (b[i] == -1) return (void)(b[i] = x); else x ^= b[i]; } orz = (orz << 1) % djq; } void dfs(int dep, int tar, long long T) { if (dep == tar + 1) return (void)(ans[__builtin_popcountll(T)]++); dfs(dep + 1, tar, T); dfs(dep + 1, tar, T ^ a[dep]); } int main() { long long x; read(n); read(m); for (int i = 0; i < m; i++) b[i] = -1; for (int i = 1; i <= n; i++) read(x), ins(x); for (int i = 0; i < m; i++) if (b[i] != -1) for (int j = i + 1; j < m; j++) if (b[j] != -1 && ((b[j] >> i) & 1)) b[j] ^= b[i]; for (int i = 0; i < m; i++) if (b[i] != -1) a[++cnt1] = b[i]; if (cnt1 <= 26) dfs(1, cnt1, 0); else { for (int i = 0; i < m; i++) if (b[i] == -1) { a[++cnt0] = 1ll << i; for (int j = i + 1; j < m; j++) if (b[j] != -1 && ((b[j] >> i) & 1)) a[cnt0] |= 1ll << j; } dfs(1, cnt0, 0); for (int i = 0; i <= m; i++) cnt[i] = ans[i], ans[i] = 0, C[i][0] = 1; for (int i = 1; i <= m; i++) for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % djq; int I = 1; for (int i = 1; i <= cnt0; i++) I = 1ll * I * i2 % djq; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) { int pl = 0; for (int k = 0; k <= j && k <= i; k++) { int delta = 1ll * C[j][k] * C[m - j][i - k] % djq; if (k & 1) pl = (pl - delta + djq) % djq; else pl = (pl + delta) % djq; } ans[i] = (1ll * I * pl % djq * cnt[j] + ans[i]) % djq; } } for (int i = 0; i <= m; i++) printf("%d ", 1ll * ans[i] * orz % djq); return puts(""), 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; const int P = 998244353, N = 100; int n, m, g[N], k, C[N][N], w[N][N], ans[N], cnt; long long a[N], b[N], c[N], x; int ksm(int a, int b) { int ans = 1; for (; b; b >>= 1, a = 1ll * a * a % P) if (b & 1) ans = 1ll * ans * a % P; return ans; } void insert(long long x) { for (int i = (m - 1); i >= (0); --i) if (x & (1ll << i)) { if (!a[i]) { a[i] = x; ++cnt; return; } x ^= a[i]; } } void dfs(int x, long long z) { if (x > k) { g[__builtin_popcountll(z)]++; if (__builtin_popcountll(z) == 5) cerr << z << endl; return; } dfs(x + 1, z ^ b[x]); dfs(x + 1, z); } int main() { scanf("%d%d", &n, &m); for (int i = (1); i <= (n); ++i) scanf("%lld", &x), insert(x); if (cnt <= m / 2) { for (int i = (0); i <= (m - 1); ++i) if (a[i]) b[++k] = a[i]; dfs(1, 0); for (int i = (0); i <= (m); ++i) printf("%lld ", 1ll * ksm(2, n - k) * g[i] % P); } else { for (int i = (0); i <= (m - 1); ++i) for (int j = (i + 1); j <= (m - 1); ++j) if (a[j] & (1ll << i)) a[j] ^= a[i]; for (int i = (0); i <= (m - 1); ++i) for (int j = (0); j <= (m - 1); ++j) if (a[j] & (1ll << i)) c[i] |= 1ll << j; for (int i = (0); i <= (m - 1); ++i) if (!(c[i] & (1ll << i))) b[++k] = c[i] ^ (1ll << i); dfs(1, 0); for (int i = (0); i <= (m); ++i) { C[i][0] = 1; for (int j = (1); j <= (i); ++j) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % P; } for (int i = (0); i <= (m); ++i) for (int j = (0); j <= (m); ++j) for (int k = (0); k <= (i); ++k) k & 1 ? ((w[i][j] += P - 1ll * C[j][k] * C[m - j][i - k] % P) >= P) && (w[i][j] -= P) : ((w[i][j] += 1ll * C[j][k] * C[m - j][i - k] % P) >= P) && (w[i][j] -= P); for (int i = (0); i <= (m); ++i) for (int j = (0); j <= (m); ++j) ((ans[i] += 1ll * g[j] * w[i][j] % P) >= P) && (ans[i] -= P); long long t = ksm(2, P - 1 + n - m); for (int i = (0); i <= (m); ++i) printf("%lld ", 1ll * t * ans[i] % P); } return 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } template <const int &MOD> struct _m_int { int val; _m_int(int64_t v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static int inv_mod(int a, int m = MOD) { int g = m, r = a, x = 0, y = 1; while (r != 0) { int q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } explicit operator int() const { return val; } explicit operator int64_t() const { return val; } _m_int &operator+=(const _m_int &other) { val -= MOD - other.val; if (val < 0) val += MOD; return *this; } _m_int &operator-=(const _m_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; unsigned x_high = x >> 32, x_low = (unsigned)x; unsigned quot, rem; asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m)); return rem; } _m_int &operator*=(const _m_int &other) { val = fast_mod((uint64_t)val * other.val); return *this; } _m_int &operator/=(const _m_int &other) { return *this *= other.inv(); } friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; } friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; } friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; } friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; } _m_int &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } _m_int &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } _m_int operator++(int) { _m_int before = *this; ++*this; return before; } _m_int operator--(int) { _m_int before = *this; --*this; return before; } _m_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const _m_int &other) const { return val == other.val; } bool operator!=(const _m_int &other) const { return val != other.val; } _m_int inv() const { return inv_mod(val); } _m_int pow(int64_t p) const { if (p < 0) return inv().pow(-p); _m_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } friend ostream &operator<<(ostream &os, const _m_int &m) { return os << m.val; } }; extern const int MOD = 998244353; using mod_int = _m_int<MOD>; const int BITS = 53; template <typename T> struct xor_basis { T basis[BITS]; int n = 0; T min_value(T start) const { if (n == BITS) return 0; for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]); return start; } T max_value(T start = 0) const { if (n == BITS) return ((T)1 << BITS) - 1; for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]); return start; } bool add(T x) { x = min_value(x); if (x == 0) return false; basis[n++] = x; for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--) swap(basis[k], basis[k - 1]); return true; } void merge(const xor_basis<T> &other) { for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]); } void merge(const xor_basis<T> &a, const xor_basis<T> &b) { if (a.n > b.n) { *this = a; merge(b); } else { *this = b; merge(a); } } }; const int POPCOUNT_BITS = 14; const int POPCOUNT_MASK = (1 << POPCOUNT_BITS) - 1; vector<int8_t> _popcount; void build_popcount() { _popcount.resize(1 << POPCOUNT_BITS); for (int x = 0; x < 1 << POPCOUNT_BITS; x++) _popcount[x] = _popcount[x >> 1] + (x & 1); } int popcountll(int64_t x) { return _popcount[x & POPCOUNT_MASK] + _popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] + _popcount[x >> (2 * POPCOUNT_BITS) & POPCOUNT_MASK] + _popcount[x >> (3 * POPCOUNT_BITS)]; } int popcount(int x) { return _popcount[x & POPCOUNT_MASK] + _popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] + _popcount[x >> (2 * POPCOUNT_BITS)]; } void row_reduce(vector<int64_t> &basis) { int B = basis.size(); for (int i = 0; i < B; i++) for (int j = i + 1; j < B; j++) basis[i] = min(basis[i], basis[i] ^ basis[j]); } vector<int64_t> generate_all(vector<int64_t> basis, int M) { row_reduce(basis); int B = basis.size(); vector<int64_t> change(B + 1, 0); int64_t prefix = 0; for (int i = 0; i < B; i++) { prefix ^= basis[i]; change[i] = prefix; } change[B] = prefix; vector<int64_t> counts(M + 1, 0); int64_t value = 0; for (int64_t mask = 0; mask < 1LL << B; mask++) { counts[popcountll(value)]++; value ^= change[__builtin_ctzll(mask + 1)]; } return counts; } vector<int64_t> build_orthogonal(vector<int64_t> basis, int M) { row_reduce(basis); int B = basis.size(); vector<int> highest_bits(B, -1); vector<bool> bit_covered(M, false); for (int i = 0; i < B; i++) { highest_bits[i] = 63 - __builtin_clzll(basis[i]); bit_covered[highest_bits[i]] = true; } vector<int64_t> orthogonal; for (int bit = M - 1; bit >= 0; bit--) if (!bit_covered[bit]) { int64_t value = 1LL << bit; for (int i = 0; i < B; i++) if (basis[i] >> bit & 1) value |= 1LL << highest_bits[i]; orthogonal.push_back(value); } return orthogonal; } vector<int64_t> generate_from_orthogonal(vector<int64_t> basis, int M) { row_reduce(basis); int B = basis.size(); vector<int64_t> orthogonal = build_orthogonal(basis, M); vector<int64_t> orthogonal_counts = generate_all(orthogonal, M); vector<int64_t> counts(M + 1, 0); vector<vector<int64_t>> choose(M + 1); for (int n = 0; n <= M; n++) { choose[n].resize(n + 1); choose[n][0] = choose[n][n] = 1; for (int r = 1; r < n; r++) choose[n][r] = choose[n - 1][r - 1] + choose[n - 1][r]; } for (int c = 0; c <= M; c++) { vector<int64_t> w(M + 1, 0); for (int d = 0; d <= M; d++) for (int j = 0; j <= d; j++) if (c - j >= 0 && M - d >= c - j) w[d] += (j % 2 == 0 ? 1 : -1) * choose[d][j] * choose[M - d][c - j]; for (int d = 0; d <= M; d++) counts[c] += orthogonal_counts[d] * w[d]; assert((counts[c] & ((1LL << (M - B)) - 1)) == 0); counts[c] >>= M - B; } return counts; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); build_popcount(); int N, M; cin >> N >> M; xor_basis<int64_t> basis_obj; for (int i = 0; i < N; i++) { int64_t a; cin >> a; basis_obj.add(a); } int B = basis_obj.n; vector<int64_t> basis(B); for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i]; vector<int64_t> answers; if (B <= M / 2) answers = generate_all(basis, M); else answers = generate_from_orthogonal(basis, M); for (int i = 0; i <= M; i++) { mod_int answer = answers[i]; answer *= mod_int(2).pow(N - B); cout << answer << (i < M ? ' ' : '\n'); } }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } template <const int &MOD> struct _m_int { int val; _m_int(int64_t v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static int inv_mod(int a, int m = MOD) { int g = m, r = a, x = 0, y = 1; while (r != 0) { int q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } explicit operator int() const { return val; } explicit operator int64_t() const { return val; } _m_int &operator+=(const _m_int &other) { val -= MOD - other.val; if (val < 0) val += MOD; return *this; } _m_int &operator-=(const _m_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; unsigned x_high = x >> 32, x_low = (unsigned)x; unsigned quot, rem; asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m)); return rem; } _m_int &operator*=(const _m_int &other) { val = fast_mod((uint64_t)val * other.val); return *this; } _m_int &operator/=(const _m_int &other) { return *this *= other.inv(); } friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; } friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; } friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; } friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; } _m_int &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } _m_int &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } _m_int operator++(int) { _m_int before = *this; ++*this; return before; } _m_int operator--(int) { _m_int before = *this; --*this; return before; } _m_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const _m_int &other) const { return val == other.val; } bool operator!=(const _m_int &other) const { return val != other.val; } _m_int inv() const { return inv_mod(val); } _m_int pow(int64_t p) const { if (p < 0) return inv().pow(-p); _m_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } friend ostream &operator<<(ostream &os, const _m_int &m) { return os << m.val; } }; extern const int MOD = 998244353; using mod_int = _m_int<MOD>; const int BITS = 53; template <typename T> struct xor_basis { T basis[BITS]; int n = 0; T min_value(T start) const { if (n == BITS) return 0; for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]); return start; } T max_value(T start = 0) const { if (n == BITS) return ((T)1 << BITS) - 1; for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]); return start; } bool add(T x) { x = min_value(x); if (x == 0) return false; basis[n++] = x; for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--) swap(basis[k], basis[k - 1]); return true; } void merge(const xor_basis<T> &other) { for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]); } void merge(const xor_basis<T> &a, const xor_basis<T> &b) { if (a.n > b.n) { *this = a; merge(b); } else { *this = b; merge(a); } } }; const int POPCOUNT_BITS = 14; const int POPCOUNT_MASK = (1 << POPCOUNT_BITS) - 1; vector<int8_t> _popcount; void build_popcount() { _popcount.resize(1 << POPCOUNT_BITS); for (int x = 0; x < 1 << POPCOUNT_BITS; x++) _popcount[x] = _popcount[x >> 1] + (x & 1); } int popcountll(int64_t x) { return _popcount[x & POPCOUNT_MASK] + _popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] + _popcount[x >> (2 * POPCOUNT_BITS) & POPCOUNT_MASK] + _popcount[x >> (3 * POPCOUNT_BITS)]; } int popcount(int x) { return _popcount[x & POPCOUNT_MASK] + _popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] + _popcount[x >> (2 * POPCOUNT_BITS)]; } void row_reduce(vector<int64_t> &basis) { int B = basis.size(); for (int i = 0; i < B; i++) for (int j = i + 1; j < B; j++) basis[i] = min(basis[i], basis[i] ^ basis[j]); } vector<int64_t> generate_all(vector<int64_t> basis, int M) { row_reduce(basis); int B = basis.size(); vector<int64_t> change(B + 1, 0); int64_t prefix = 0; for (int i = 0; i < B; i++) { prefix ^= basis[i]; change[i] = prefix; } change[B] = prefix; vector<int64_t> counts(M + 1, 0); int64_t value = 0; for (int64_t mask = 0; mask < 1LL << B; mask++) { counts[popcountll(value)]++; value ^= change[__builtin_ctzll(mask + 1)]; } return counts; } vector<int64_t> build_orthogonal(vector<int64_t> basis, int M) { row_reduce(basis); int B = basis.size(); vector<int> highest_bit(B, -1); vector<bool> bit_covered(M, false); for (int i = 0; i < B; i++) { highest_bit[i] = 63 - __builtin_clzll(basis[i]); bit_covered[highest_bit[i]] = true; } vector<int64_t> orthogonal; for (int bit = M - 1; bit >= 0; bit--) if (!bit_covered[bit]) { int64_t value = 1LL << bit; for (int i = 0; i < B; i++) value |= (basis[i] >> bit & 1) << highest_bit[i]; orthogonal.push_back(value); } return orthogonal; } vector<int64_t> generate_from_orthogonal(vector<int64_t> basis, int M) { row_reduce(basis); int B = basis.size(); vector<int64_t> orthogonal = build_orthogonal(basis, M); vector<int64_t> orthogonal_counts = generate_all(orthogonal, M); vector<int64_t> counts(M + 1, 0); vector<vector<int64_t>> choose(M + 1); for (int n = 0; n <= M; n++) { choose[n].resize(n + 1); choose[n][0] = choose[n][n] = 1; for (int r = 1; r < n; r++) choose[n][r] = choose[n - 1][r - 1] + choose[n - 1][r]; } auto &&get_choose = [&](int n, int r) -> int64_t { if (r < 0 || r > n) return 0; return choose[n][r]; }; for (int c = 0; c <= M; c++) { vector<int64_t> w(M + 1, 0); for (int d = 0; d <= M; d++) for (int j = 0; j <= d; j++) w[d] += (j % 2 == 0 ? 1 : -1) * get_choose(d, j) * get_choose(M - d, c - j); for (int d = 0; d <= M; d++) counts[c] += orthogonal_counts[d] * w[d]; assert((counts[c] & ((1LL << (M - B)) - 1)) == 0); counts[c] >>= M - B; } return counts; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); build_popcount(); int N, M; cin >> N >> M; xor_basis<int64_t> basis_obj; for (int i = 0; i < N; i++) { int64_t a; cin >> a; basis_obj.add(a); } int B = basis_obj.n; vector<int64_t> basis(B); for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i]; vector<int64_t> answers; if (B <= M / 2) answers = generate_all(basis, M); else answers = generate_from_orthogonal(basis, M); for (int i = 0; i <= M; i++) { mod_int answer = answers[i]; answer *= mod_int(2).pow(N - B); cout << answer << (i < M ? ' ' : '\n'); } }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class t, class u> void chmax(t& first, u second) { if (first < second) first = second; } template <class t, class u> void chmin(t& first, u second) { if (second < first) first = second; } template <class t> using vc = vector<t>; template <class t> using vvc = vc<vc<t>>; using pi = pair<ll, ll>; using vi = vc<ll>; template <class t, class u> ostream& operator<<(ostream& os, const pair<t, u>& p) { return os << "{" << p.first << "," << p.second << "}"; } template <class t> ostream& operator<<(ostream& os, const vc<t>& v) { os << "{"; for (auto e : v) os << e << ","; return os << "}"; } using uint = unsigned; using ull = unsigned long long; template <class t, size_t n> ostream& operator<<(ostream& os, const array<t, n>& first) { return os << vc<t>(first.begin(), first.end()); } template <ll i, class T> void print_tuple(ostream&, const T&) {} template <ll i, class T, class H, class... Args> void print_tuple(ostream& os, const T& t) { if (i) os << ","; os << get<i>(t); print_tuple<i + 1, T, Args...>(os, t); } template <class... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) { os << "{"; print_tuple<0, tuple<Args...>, Args...>(os, t); return os << "}"; } template <class t> void print(t x, ll suc = 1) { cout << x; if (suc == 1) cout << "\n"; if (suc == 2) cout << " "; } ll read() { ll i; cin >> i; return i; } vi readvi(ll n, ll off = 0) { vi v(n); for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off; return v; } template <class T> void print(const vector<T>& v, ll suc = 1) { for (ll i = ll(0); i < ll(v.size()); i++) print(v[i], i == ll(v.size()) - 1 ? suc : 2); } string readString() { string s; cin >> s; return s; } template <class T> T sq(const T& t) { return t * t; } void yes(bool ex = true) { cout << "Yes" << "\n"; if (ex) exit(0); } void no(bool ex = true) { cout << "No" << "\n"; if (ex) exit(0); } void possible(bool ex = true) { cout << "Possible" << "\n"; if (ex) exit(0); } void impossible(bool ex = true) { cout << "Impossible" << "\n"; if (ex) exit(0); } constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; } const ll infLL = LLONG_MAX / 3; const ll inf = infLL; ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } ll popcount(signed t) { return __builtin_popcount(t); } ll popcount(ll t) { return __builtin_popcountll(t); } bool ispow2(ll i) { return i && (i & -i) == i; } ll mask(ll i) { return (ll(1) << i) - 1; } bool inc(ll first, ll second, ll c) { return first <= second && second <= c; } template <class t> void mkuni(vc<t>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } ll rand_int(ll l, ll r) { static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<ll>(l, r)(gen); } template <class t> void myshuffle(vc<t>& first) { for (ll i = ll(0); i < ll(ll(first.size())); i++) swap(first[i], first[rand_int(0, i)]); } template <class t> ll lwb(const vc<t>& v, const t& first) { return lower_bound(v.begin(), v.end(), first) - v.begin(); } struct modinfo { uint mod, root; }; template <modinfo const& ref> struct modular { static constexpr uint const& mod = ref.mod; static modular root() { return modular(ref.root); } uint v; modular(ll vv = 0) { s(vv % mod + mod); } modular& s(uint vv) { v = vv < mod ? vv : vv - mod; return *this; } modular operator-() const { return modular() - *this; } modular& operator+=(const modular& rhs) { return s(v + rhs.v); } modular& operator-=(const modular& rhs) { return s(v + mod - rhs.v); } modular& operator*=(const modular& rhs) { v = ull(v) * rhs.v % mod; return *this; } modular& operator/=(const modular& rhs) { return *this *= rhs.inv(); } modular operator+(const modular& rhs) const { return modular(*this) += rhs; } modular operator-(const modular& rhs) const { return modular(*this) -= rhs; } modular operator*(const modular& rhs) const { return modular(*this) *= rhs; } modular operator/(const modular& rhs) const { return modular(*this) /= rhs; } modular pow(ll n) const { modular res(1), x(*this); while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } modular inv() const { return pow(mod - 2); } friend modular operator+(ll x, const modular& y) { return modular(x) + y; } friend modular operator-(ll x, const modular& y) { return modular(x) - y; } friend modular operator*(ll x, const modular& y) { return modular(x) * y; } friend modular operator/(ll x, const modular& y) { return modular(x) / y; } friend ostream& operator<<(ostream& os, const modular& m) { return os << m.v; } friend istream& operator>>(istream& is, modular& m) { ll x; is >> x; m = modular(x); return is; } bool operator<(const modular& r) const { return v < r.v; } bool operator==(const modular& r) const { return v == r.v; } bool operator!=(const modular& r) const { return v != r.v; } explicit operator bool() const { return v; } }; template <class mint> vi sweep(vvc<mint>& first, ll c = -1) { if (first.empty()) return {}; if (c == -1) c = first[0].size(); ll h = first.size(), w = first[0].size(), r = 0; vi res; for (ll i = ll(0); i < ll(c); i++) { if (r == h) break; for (ll j = ll(r); j < ll(h); j++) if (first[j][i].v) { swap(first[r], first[j]); break; } if (first[r][i].v == 0) continue; for (ll j = ll(0); j < ll(h); j++) if (j != r) { mint z = -first[j][i] / first[r][i]; for (ll k = ll(i); k < ll(w); k++) first[j][k] += first[r][k] * z; } res.push_back(i); r++; } return res; } template <class mint> vvc<mint> kernel(const vvc<mint>& first, ll w) { ll h = first.size(); vvc<mint> second(w, vc<mint>(h + w)); for (ll i = ll(0); i < ll(h); i++) for (ll j = ll(0); j < ll(w); j++) second[j][i] = first[i][j]; for (ll i = ll(0); i < ll(w); i++) second[i][h + i] = 1; ll r = sweep(second, h).size(); vvc<mint> res; for (ll i = ll(r); i < ll(w); i++) res.emplace_back(second[i].begin() + h, second[i].end()); return res; } extern constexpr modinfo base{998244353, 3}; extern constexpr modinfo base2{2, 1}; using mint = modular<base>; const ll vmax = (1 << 21) + 10; mint fact[vmax], finv[vmax], invs[vmax]; void initfact() { fact[0] = 1; for (ll i = ll(1); i < ll(vmax); i++) { fact[i] = fact[i - 1] * i; } finv[vmax - 1] = fact[vmax - 1].inv(); for (ll i = vmax - 2; i >= 0; i--) { finv[i] = finv[i + 1] * (i + 1); } for (ll i = vmax - 1; i >= 1; i--) { invs[i] = finv[i] * fact[i - 1]; } } mint choose(ll n, ll k) { return fact[n] * finv[n - k] * finv[k]; } mint binom(ll first, ll second) { return fact[first + second] * finv[first] * finv[second]; } mint catalan(ll n) { return binom(n, n) - (n - 1 >= 0 ? binom(n - 1, n + 1) : 0); } vc<mint> getfreq(ll m, vi first) { ll n = ll(first.size()); vc<mint> res(m + 1); ll cur = 0; for (ll bit = ll(0); bit < ll(1 << n); bit++) { for (ll j = ll(0); j < ll(n); j++) if ((bit ^ (bit - 1)) & 1 << j) cur ^= first[j]; else break; res[popcount(cur)] += 1; } return res; } signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); ll n, m; cin >> n >> m; vi first; ll cnt = 0; for (ll _ = ll(0); _ < ll(n); _++) { ll x; cin >> x; for (auto v : first) chmin(x, x ^ v); if (x) first.push_back(x); else cnt++; } vc<mint> ans(m + 1); if (ll(first.size()) <= m / 2) { void(0); ans = getfreq(m, first); } else { void(0); initfact(); using mint2 = modular<base2>; vvc<mint2> vs(ll(first.size()), vc<mint2>(m)); for (ll i = ll(0); i < ll(ll(first.size())); i++) for (ll j = ll(0); j < ll(m); j++) vs[i][j] = (first[i] >> j) & 1; auto ker = kernel(vs, m); vi second(ll(ker.size())); for (ll i = ll(0); i < ll(ll(ker.size())); i++) for (ll j = ll(0); j < ll(m); j++) second[i] += ll(ker[i][j].v) << j; auto z = getfreq(m, second); for (ll i = ll(0); i < ll(m + 1); i++) { for (ll c = ll(0); c < ll(i + 1); c++) for (ll d = ll(0); d < ll(m - i + 1); d++) { mint w = z[i] * choose(i, c) * choose(m - i, d); if (c & 1) w = -w; ans[c + d] += w; } } for (auto& v : ans) v /= mint(2).pow(m - ll(first.size())); } for (auto& v : ans) v *= mint(2).pow(cnt); print(ans); }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) { if (ch == '-') f = -1; } for (; isdigit(ch); ch = getchar()) { x = x * 10 + ch - 48; } return x * f; } const int mxN = 1 << 19; const int mxM = 53; const int P = 998244353; long long comb[mxM + 3][mxM + 3]; int bitcnt[mxN + 3]; long long a[mxM + 3]; int b[mxM + 3]; long long ans[mxM + 3]; int n, m, sz; long long quickpow(long long x, long long y) { long long cur = x, ret = 1ll; for (int i = 0; y; i++) { if (y & (1ll << i)) { y -= (1ll << i); ret = ret * cur % P; } cur = cur * cur % P; } return ret; } void initfact(int n) { comb[0][0] = 1ll; for (int i = 1; i <= n; i++) { comb[i][0] = comb[i][i] = 1ll; for (int j = 1; j < i; j++) { comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % P; } } } int getbitcnt(long long x) { return bitcnt[x & 524287] + bitcnt[(x >> 19) & 524287] + bitcnt[(x >> 38) & 524287]; } namespace Solve1 { void dfs(int pos, long long x) { if (pos == sz) { ans[getbitcnt(x)]++; return; } dfs(pos + 1, x); dfs(pos + 1, x ^ a[b[pos]]); } } // namespace Solve1 namespace Solve2 { long long sta[mxM + 3]; long long f[mxM + 3]; void dfs(int pos, int cnt, long long x) { if (pos == m) { f[cnt + getbitcnt(x)]++; return; } dfs(pos + 1, cnt, x); dfs(pos + 1, cnt + 1, x ^ sta[pos]); } void solve() { for (int i = sz; i < m; i++) { for (int j = 0; j < sz; j++) { if (a[b[j]] & (1ll << b[i])) { sta[i] |= (1ll << j); } } } dfs(sz, 0, 0ll); for (int k = 0; k <= m; k++) { for (int i = 0; i <= m; i++) { long long coe = 0ll; for (int j = 0; j <= i && j <= k; j++) { long long tmp = comb[i][j] * comb[m - i][k - j] % P; if (j & 1) tmp = P - tmp; coe += tmp - P, coe += (coe >> 31) & P; } coe = coe * f[i] % P; ans[k] += coe - P, ans[k] += (ans[k] >> 31) & P; } ans[k] = ans[k] * quickpow((P + 1ll) / 2ll, m - sz) % P; } } } // namespace Solve2 int main() { initfact(mxM); for (int i = 1; i < mxN; i++) bitcnt[i] = bitcnt[i >> 1] + (i & 1); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { long long x; scanf("%I64d", &x); for (int k = m - 1; k >= 0; k--) if (x & (1ll << k)) { if (!a[k]) { a[k] = x; sz++; break; } else x ^= a[k]; } } for (int i = m - 1; i >= 0; i--) if (a[i]) { for (int j = i + 1; j < m; j++) if (a[j] & (1ll << i)) a[j] ^= a[i]; } for (int i = 0, j = 0; i < sz; i++) { while (!a[j]) j++; b[i] = j; j++; } for (int i = sz, j = 0; i < m; i++) { while (a[j]) j++; b[i] = j; j++; } if (sz <= 26) Solve1::dfs(0, 0ll); else Solve2::solve(); for (int i = 0; i <= m; i++) ans[i] = ans[i] * quickpow(2ll, n - sz) % P; for (int i = 0; i <= m; i++) printf("%I64d ", ans[i]); puts(""); return 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; template <class T> inline void chkmax(T &a, T b) { if (a < b) a = b; } template <class T> inline void chkmin(T &a, T b) { if (a > b) a = b; } inline int read() { int s = 0, f = 1; char ch = getchar(); while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') ch = getchar(), f = -1; while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar(); return ~f ? s : -s; } const int maxn = 120; const int mod = 998244353; int n, m, k; long long rk[maxn], s[maxn]; inline void insert(long long x) { for (int i = (m - 1), _end_ = (0); i >= _end_; i--) if (x >> i & 1) { if (!s[i]) { s[i] = x; k++; break; } else x ^= s[i]; } } inline void init() { n = read(); m = read(); for (int i = (1), _end_ = (n); i <= _end_; i++) { long long x; scanf("%lld", &x); insert(x); } } long long cnt[100], C[maxn][maxn]; long long ans[100], tp; void dfs(int u, long long s) { if (u == tp + 1) return cnt[__builtin_popcountll(s)]++, void(); dfs(u + 1, s); dfs(u + 1, s ^ rk[u]); } inline void doing() { for (int i = (0), _end_ = (m - 1); i <= _end_; i++) if (s[i]) for (int j = (i + 1), _end_ = (m - 1); j <= _end_; j++) if (s[j] >> i & 1) s[j] ^= s[i]; for (int i = (0), _end_ = (m - 1); i <= _end_; i++) if (s[i]) rk[++tp] = s[i]; if (k <= m / 2) { dfs(1, 0); long long mult = 1; for (int i = (1), _end_ = (n - k); i <= _end_; i++) mult = (long long)mult * 2 % mod; for (int i = (0), _end_ = (m); i <= _end_; i++) printf("%lld ", (long long)cnt[i] * mult % mod); } else { tp = 0; for (int i = (0), _end_ = (m - 1); i <= _end_; i++) if (!s[i]) { long long x = 1ll << i; for (int j = (0), _end_ = (m - 1); j <= _end_; j++) if (s[j] & (1ll << i)) x |= 1ll << j; rk[++tp] = x; } dfs(1, 0); for (int i = (0), _end_ = (m); i <= _end_; i++) C[i][0] = 1; for (int i = (1), _end_ = (m); i <= _end_; i++) for (int j = (1), _end_ = (i); j <= _end_; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; for (int x = (0), _end_ = (m); x <= _end_; x++) { for (int y = (0), _end_ = (m); y <= _end_; y++) for (int p = (0), _end_ = (min(y, x)); p <= _end_; p++) { ans[x] = (ans[x] + (long long)cnt[y] * C[y][p] % mod * C[m - y][x - p] * (p & 1 ? -1 : 1)) % mod; } ans[x] = (ans[x] + mod) % mod; } long long mult = 1; for (int i = (1), _end_ = (n); i <= _end_; i++) mult = (long long)mult * 2 % mod; for (int i = (1), _end_ = (m); i <= _end_; i++) mult = (long long)mult * (mod + 1 >> 1) % mod; for (int i = (0), _end_ = (m); i <= _end_; i++) printf("%lld ", (long long)ans[i] * mult % mod); } } int main() { init(); doing(); return 0; }
CPP
1336_E2. Chiori and Doll Picking (hard version)
This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways. Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 ≀ i < m, such that \left⌊ (x)/(2^i) \rightβŒ‹ is odd. Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353. Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ m ≀ 53) β€” the number of dolls and the maximum value of the picking way. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^m) β€” the values of dolls. Output Print m+1 integers p_0, p_1, …, p_m β€” p_i is equal to the number of picking ways with value i by modulo 998 244 353. Examples Input 4 4 3 5 8 14 Output 2 2 6 6 0 Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2
11
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int n, m; vector<long long> bs, todo; int cnt[(1 << 16) + 1]; long long inv2 = (mod + 1) / 2; int popcount(long long x) { return __builtin_popcountll(x); } long long getp(long long x) { return 63 - __builtin_clzll(x); } bool add(long long x) { for (int i = m - 1; i >= 0; i--) { if (!bs[i]) continue; if (x >> i & 1) x ^= bs[i]; } if (!x) return false; bs[getp(x)] = x; return true; } void build() { for (int i = 0; i < (m); ++i) { for (int j = i - 1; j >= 0; j--) { if (bs[i] >> j & 1) bs[i] ^= bs[j]; } } vector<long long> new_bs; for (int i = 0; i < m; i++) { if (bs[i]) continue; long long val = 0; val |= (1LL << i); for (int j = 0; j < m; j++) val |= (bs[j] >> i & 1) << j; new_bs.push_back(val); } todo = new_bs; } long long pk = 1; long long res[55], fres[55]; long long C[55][55], f[55][55]; void dfs(int d, long long cur) { if (d == (int)(todo).size()) { res[popcount(cur)]++; return; } dfs(d + 1, cur); if (todo[d]) dfs(d + 1, cur ^ todo[d]); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); for (int i = 1; i < (1 << 16); i++) { cnt[i] = cnt[i / 2] + i % 2; } cin >> n >> m; bs.resize(m, 0); for (int i = 0; i < n; i++) { long long x; cin >> x; if (!add(x)) pk *= 2, pk %= mod; } for (auto& x : bs) if (x) todo.push_back(x); if ((int)(todo).size() <= 26) { dfs(0, 0); for (int i = 0; i <= m; i++) cout << (pk * res[i] % mod + mod) % mod << " "; cout << "\n"; } else { for (int i = 0; i < 55; i++) C[i][i] = C[i][0] = 1; for (int i = 0; i < 55; i++) { for (int j = 1; j < i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; } build(); dfs(0, 0); for (int b = 0; b <= m; b++) { for (int c = 0; c <= m; c++) { for (int a = 0; a <= b && a <= c; a++) { int s = (a % 2 == 0 ? 1 : -1); f[b][c] = (f[b][c] + s * C[b][a] * C[m - b][c - a]) % mod; } fres[c] += (f[b][c] * res[b]); fres[c] %= mod; } } for (int i = 0; i < (int)(todo).size(); i++) pk = (pk * inv2) % mod; for (int i = 0; i <= m; i++) cout << (pk * fres[i] % mod + mod) % mod << " "; cout << "\n"; } }
CPP