fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc=new Scanner(System.in);
  14. int n=sc.nextInt();
  15. int a[]=new int[n];
  16. for(int i=0;i<n;i++) a[i]=sc.nextInt();
  17. int b[]=new int[n];
  18. for(int i=0;i<n;i++) b[i]=sc.nextInt();
  19. int dpa[][]=new int[n+1][2];
  20. int dpb[][]=new int[n+1][2];
  21. if(a[0]%2==0) dpa[1][0]=1;
  22. else dpa[1][1]=1;
  23.  
  24. if(b[0]%2==0) dpb[1][0]=1;
  25. else dpb[1][1]=1;
  26.  
  27. for(int i=2;i<=n;i++){
  28. if(a[i-1]%2==0){
  29. dpa[i][0]=dpa[i-1][0]+dpb[i-1][0];
  30. dpa[i][1]=dpa[i-1][1]+dpb[i-1][1];
  31. }else{
  32. dpa[i][0]=dpa[i-1][1]+dpb[i-1][1];
  33. dpa[i][1]=dpa[i-1][0]+dpb[i-1][0];
  34. }
  35.  
  36. if(b[i-1]%2==0){
  37. dpb[i][0]=dpb[i-1][0]+dpa[i-1][0];
  38. dpb[i][1]=dpb[i-1][1]+dpa[i-1][1];
  39. }else{
  40. dpb[i][0]=dpa[i-1][1]+dpb[i-1][1];
  41. dpb[i][1]=dpa[i-1][0]+dpb[i-1][0];
  42. }
  43. }
  44. System.out.println("Even number of ways: "+(dpa[n][0]+dpb[n][0]));
  45. System.out.println("Odd number of ways: "+(dpa[n][1]+dpb[n][1]));
  46. }
  47. }
Success #stdin #stdout 0.13s 58916KB
stdin
2
2 2
2 2
stdout
Even number of ways: 4
Odd number of ways: 0