1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import java.util.ArrayList; import java.io.*; class Lexicography { static ArrayList<Long> perms = new ArrayList<Long>(); public static void main(String[] args) throws IOException{ long time = System.currentTimeMillis(); getPermutations(Integer.valueOf(new BufferedReader(new InputStreamReader(System.in)).readLine())); System.out.println("\nSize:-"+perms.size()+"\nTime:-"+(System.currentTimeMillis()-time)); //This println is never printed... java aborts before that } public static ArrayList<Long> getPermutations(int num){ int[] n = new int[num+1]; for(int i=0; i<=num; i++) n[i]=i; perms.add(getLong(n)); for(int i=num; i>=0; i--) permutate(n[i],n); return perms; } private static void permutate(int k, int[] n){ if(k>n.length) return; int p=0; for(int i:n){ if(i==k) break; p++;} for(int i=0; i<n.length; i++){ if(i==p || n[i]<k) continue; n=swap(p,i,n); perms.add(getLong(n)); //System.out.println("i:"+(i+1)+" k:"+k+" n:"+getLong(n));//this prints all permutations till the last one and then poof! for(int j=k-1; j>=0; j--) permutate(j,n); n=swap(p,i,n); } } private static int[] swap(int i, int f, int[] a){ int t=a[f]; a[f]=a[i]; a[i]=t; return a; } private static long getLong(int[] n){ long ten=1, num=0; for(int i=n.length-1; i>=0; i--){ num+=n[i]*ten; ten*=10; } return num; } } |
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBMZXhpY29ncmFwaHkgewoKICAgIHN0YXRpYyBBcnJheUxpc3Q8TG9uZz4gcGVybXMgPSBuZXcgQXJyYXlMaXN0PExvbmc+KCk7CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIElPRXhjZXB0aW9uewogICAgICAgIGxvbmcgdGltZSA9IFN5c3RlbS5jdXJyZW50VGltZU1pbGxpcygpOwogICAgICAgIGdldFBlcm11dGF0aW9ucy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iICAgIG46IitnZXRMb25nKG4pKTsvL3RoaXMgcHJpbnRzIGFsbCBwZXJtdXRhdGlvbnMgdGlsbCB0aGUgbGFzdCBvbmUgYW5kIHRoZW4gcG9vZiEKCiAgICAgICAgICAgIGZvcihpbnQgaj1rLTE7IGo+PTA7IGotLSkgcGVybXV0YXRlKGosbik7CiAgICAgICAgICAgIG49c3dhcChwLGksbik7CiAgICAgICAgfQogICAgfQoKICAgIHByaXZhdGUgc3RhdGljIGludFtdIHN3YXAoaW50IGksIGludCBmLCBpbnRbXSBhKXsKICAgICAgICBpbnQgdD1hW2ZdOwogICAgICAgIGFbZl09YVtpXTsgYVtpXT10OwogICAgICAgIHJldHVybiBhOwogICAgfQoKICAgIHByaXZhdGUgc3RhdGljIGxvbmcgZ2V0TG9uZyhpbnRbXSBuKXsKICAgICAgICBsb25nIHRlbj0xLCBudW09MDsKICAgICAgICBmb3IoaW50IGk9bi5sZW5ndGgtMTsgaT49MDsgaS0tKXsKICAgICAgICAgICAgbnVtKz1uW2ldKnRlbjsgdGVuKj0xMDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIG51bTsKICAgIH0KCn0=
-
upload with new input
-
result: Runtime error time: 0.27s memory: 245760 kB signal: -1
10
# # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0xb6fcf2ed, pid=30804, tid=3030907760 # # JRE version: 6.0_31-b04 # Java VM: Java HotSpot(TM) Client VM (20.6-b01 mixed mode, sharing linux-x86 ) # Problematic frame: # V [libjvm.so+0x3422ed] MarkSweep::AdjustPointerClosure::do_oop(oopDesc**)+0xd # # An error report file with more information is saved as: # /tmp/hs_err_pid30804.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp #
-
result: Success time: 0.13s memory: 245632 kB returned value: 0
8
Size:-362880 Time:-99
-
result: Runtime error time: 0.27s memory: 245760 kB signal: -1
9
# # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0xb70cb2ed, pid=27876, tid=3031939952 # # JRE version: 6.0_31-b04 # Java VM: Java HotSpot(TM) Client VM (20.6-b01 mixed mode, sharing linux-x86 ) # Problematic frame: # V [libjvm.so+0x3422ed] MarkSweep::AdjustPointerClosure::do_oop(oopDesc**)+0xd # # An error report file with more information is saved as: # /tmp/hs_err_pid27876.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp #


