Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.*;
- import java.text.*;
- class Main {
- static double result;
- static double[] prob;
- static double[] flies;
- static int leaves;
- public static void main(String[] args) {
- // Uncomment this line if you want to read from a file
- In.open("public/sample.in");
- int t = In.readInt();
- for (int i = 0; i < t; i++) {
- testCase();
- }
- // Uncomment this line if you want to read from a file
- //In.close();
- }
- public static void testCase() {
- // Input using In.java class
- int leaves = In.readInt();
- //Out.print(leaves + " ");
- int startleaf = In.readInt();
- //Out.print(startleaf+ " ");
- int jumps = In.readInt();
- //Out.println(jumps);
- flies = new double[leaves+1];
- prob = new double[leaves+1];
- //save values of probs for each leaf :
- for(int i = 0; i<leaves; i++){
- flies[i] = In.readInt();
- //Out.print(flies[i] + " ");
- }
- // Out.println();
- for(int i = 0; i<leaves; i++){
- prob[i] = In.readDouble();
- //Out.print(prob[i] + " ");
- }
- //Out.println();
- DecimalFormat df = new DecimalFormat("0.0####");
- df.setRoundingMode(RoundingMode.HALF_DOWN);
- System.out.println(df.format(calcul(startleaf, jumps)));
- }
- static double calcul(int startleaf, int jumps){
- if (jumps == 0){
- if(startleaf>=0 && startleaf<=leaves){
- return flies[startleaf];
- }
- }
- double sum = 1.0;
- double left = 1.0;
- double right = 1.0;
- for(int i = 0; i<jumps; i++){//nombre d'additions
- if(startleaf==0){
- if(1-prob[startleaf] == 1.0){//si la prob d'aller à droite ==1
- sum *= (1-prob[startleaf])*calcul(startleaf+1, jumps-1);
- return flies[startleaf] + sum;
- }
- else{
- if(prob[startleaf] == 1.0){
- return flies[startleaf];
- }
- }
- }
- else if(startleaf==leaves){
- if(prob[startleaf] == 1.0){
- sum *= prob[startleaf]*calcul(startleaf-1, jumps-1);
- return flies[startleaf] + sum;
- }
- else if(prob[startleaf] == 0.0){
- return flies[startleaf];
- }
- }
- else{
- left *= prob[startleaf]*calcul(startleaf-1, jumps-1);
- right *= (1-prob[startleaf])*calcul(startleaf+1, jumps-1);
- return flies[startleaf] + left + right;
- }
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement