package yukicoder317; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.Scanner; public class Main { static class UnionFind{ static int[] data; UnionFind(int size){ data=new int[size]; Arrays.fill(data, -1); } boolean unionSet(int x,int y){ x=root(x); y=root(y); if(x!=y){ if(data[y] memList=new ArrayList<>(); int[] memNum=new int[n+1]; for(int i=1;i<=n;i++){ if(!memList.contains(uf.root(i))){ memList.add(uf.root(i)); memNum[uf.size(i)]++; } } ArrayDeque set=new ArrayDeque<>(); for(int i=1;i<=n;i++){ if(memNum[i]!=0){ set.add(new Set(i,1,memNum[i])); //size,cost,quant } } int[] d=new int[n+1]; Arrays.fill(d, -1); while(!set.isEmpty()){ Set tmpset=set.poll(); int count=1; int tmpsum=tmpset.quant(); while(tmpset.quant!=1&&count<10){ if(Math.pow(2, count+1)-1<=tmpsum){ set.add(new Set(tmpset.size()*(int)Math.pow(2,count),(int)Math.pow(2, count),1)); tmpset.quant-=Math.pow(2, count); }else if(Math.pow(2, count+1)-1>tmpsum){ set.add(new Set(tmpset.size()*(tmpsum-((int)Math.pow(2, count)-1)),tmpsum-((int)Math.pow(2, count)-1),1)); tmpset.quant-=(tmpsum-((int)Math.pow(2, count)-1)); } count++; //1+2+4+8+16+32+,size,cost,quant } for(int i=n;i-tmpset.size()>=0;i--){ if(i-tmpset.size()==0){ d[i]=0; }else if(d[i-tmpset.size()]==-1){ continue; }else if(d[i-tmpset.size()]>=0){ if(d[i]==-1){ d[i]=d[i-tmpset.size()]+tmpset.cost(); }else{ d[i]=Math.min(d[i-tmpset.size()]+tmpset.cost(), d[i]); } } } } for(int i=1;i<=n;i++){ System.out.println(d[i]); } System.err.println(new Date().getTime()-t+"ms"); } }