We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #207: Integer partition equations
Project Euler #207: Integer partition equations
+ 0 comments i am getting timeout:
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
public static void main(String[] args) { Scanner in = new Scanner(System.in); int q = in.nextInt(); int a; int b; double abratio; int m; int perfectpartition; int totalpartition; double pmratio; double discriminant; double t; double t4; double t2; for(int i=0;i<q;i++){ a=in.nextInt(); b=in.nextInt(); abratio = (double)a/b; m=2; totalpartition=0; perfectpartition=0; for(int k=1;k<=m;k++){ discriminant = 1+4*k; t=Math.log((1+Math.sqrt(discriminant))/2)/Math.log(2); t4=Math.round(Math.pow(4,t)*1000000d)/1000000d; t2=Math.round(Math.pow(2,t)*1000000d)/1000000d;; if(t4==(int)t4 && t2==(int)t2){ totalpartition++; if(Math.round(t*1000000d)/1000000d==(int)t) perfectpartition++; } pmratio=(double)perfectpartition/totalpartition; if(pmratio<abratio) break; else m++; } System.out.println(m-1); } }
}
+ 0 comments Something wrong with my solution. Can't understand what. It does not passed a couple of tests within timeout error:
import math
q = int(input().rstrip())
for _ in range(q): a, b = map(int, input().rstrip().split())
pn = 1 if b/a > 10000: pn = int(math.log(int(b/a),2)) - 1 if pn < 1: pn = 1 start_p2 = 2**pn if pn > 1: start_p2 -=1 start_num_1 = pn
num_1 = start_num_1 cur_p2 = start_p2 k = 2 prev_curp2 = cur_p2 while 1: num_1 += 1 cur_p2 = 2*cur_p2 if (num_1-1)*b < a*(cur_p2 - 2): k = math.floor(((num_1-1)*b)/a + 2 - cur_p2) + cur_p2 while (num_1-1)*b < a*(cur_p2 - 2 - k - 1) and (cur_p2 - k - 1 >= prev_curp2): k -= 1 break prev_curp2 = cur_p2 k_sum = k**2 - k print("{}".format(k_sum)) prev_curp2 = cur_p2 k_sum = k**2 - k print("{}".format(k_sum))
+ 0 comments can anyone explain me why P(15)=2/3 and P(20)=1/2
+ 1 comment can anyone explain me how P(15)=2/3 and why P(20)=1/2
+ 0 comments Need help to understand the problem. I mean how we are getting P(6) = 1/2?
Load more conversations
Sort 39 Discussions, By:
Please Login in order to post a comment