Friday, 12 June 2015

ESTIMATION AND IMPLIMENTATION

One Dimensional Game of Life 
Solved




In Conway's Game of Life, cells in a grid are used to simulate biological cells.
Each cell is considered to be either alive or dead.
At each step of the simulation
each cell's current status and number of living neighbors is used to determine the status
of the cell during the following step of the simulation.
In this one-dimensional version, there are N cells numbered 0 through N-1.
The number of cells does not change at any point in the simulation.
Each cell i is adjacent to cells i-1 and i+1.
Here, the indices are taken modulo N meaning cells 0 and N-1 are also adjacent to eachother.
At each step of the simulation, cells with exactly one living neighbor change their status
(alive cells become dead, dead cells become alive).
For example, if we represent dead cells with a '0' and living cells with a '1', consider
the state with 8 cells:
01100101
  • Cells 0 and 6 have two living neighbors.
  • Cells 1, 2, 3, and 4 have one living neighbor.
  • Cells 5 and 7 have no living neighbors.
Thus, at the next step of the simulation, the state would be:
00011101
Given some state of the game, your task is to determine the state immediately preceding it.
In some cases there may be more than one answer or no possible answer.

Input

Input will begin with an integer T<100, the number of test cases.
Each test case consists of a single line, with between 3 and 50 characters, inclusive.
Each character will be either '0' or '1'.
Each '0' represents a dead cell, and each '1' represents an alive cell.

Output

For each test case, output the state of the game that precedes the given state.
If there is no possible solution, print "No solution" (quotes for clarity only).
If there are multiple possible solutions, print "Multiple solutions" (quotes for clarity only).

Sample Input

4
00011101
000
000001
11110

Sample Output

01100101
Multiple solutions
No solution
10010



-------------------------MY EDITORIAL ---------------------------------------------------------------------------------------------
THIS PROBLEN CAN BE SOLVE BY CONSIDERING FIRSTTWO BIT AND COMPUTING REAT ALL 
NOTE TAH THER ARE ONLY 4 POSSIBLE COMBINATIONS OF FIRST TWO BITS  ie. 00, O1,10,11
FOR ALL THESE COMBINATIONS WE COMPUTE ARRAY 
IF ONLY 1 SUCESS FULL ARRAY COMPUTED THAN OK ELSE IF MORE THAN 1 THAN MULTI ANS 
ELSE NO ANS..
FOR  GENERATING ANY SEQUENCE WE USE TWO CURRENT BIT AND 1 BIT OF GIVEN SEQUENCE   (CORESPOINDING BIT FROM GIVEN SEQUENCE )
NOW FOR A VALID SEQUENCE  LAST BIT MUST BE VALID WHICH IS CHECKED BY 1 ST AND 2 ND LAST BIT .. ALSO 1ST BIT MUST BE VALID WHICH IS CHECKED BY LAST BIT AND 2ND BIT ..


*********************************************CHEF EDITORIAL*********************************************

EXPLANATION

First solution: If we choose an arbitrary assignment for any two adjacent cells of the original state, then the entire rest of the state can be determined from the current state. For example, if we choose values for original[0] and original[1], then current[1] is defined as original[0]^original[1]^original[2] (where '^' is exclusive or), thus original[2]=original[0]^original[1]^current[1]. Repeating the process allows us to determine all cells of the original state. Depending on the initial assignment, this may or may not yield a valid solution. There are only 4 initial assignments to try, and we simply count the number that yield valid solutions.
Second solution: Consider the state of the game as theBINARY representation of an N-bit integer. Let A represent the original state of the game, and B the current state. Then the simulation step simply implies that B=F(A), where F(A)=(A<<<1)^A^(A>>>1), '<<<' is left rotation (not left shift), and '>>>' is right rotation. Now consider these cases:
  • If N is divisible by 3, then F(011011...011011)=000...000, hence the solution cannot be unique (becase F(X^Y)=F(X)^F(Y), there will either be 0 solutions, or 4 solutions). Denote G(B)=(B<<<1)^(B<<<2)=A^(A<<<3). Now G(B)^(G(B)<<<3)^(G(B)<<<6)^...^(G(B)<<<(N-3)) should equal 0. If this is the case, then there are multiple solutions. Otherwise there is no solution.
  • If N has a remainder of 1 when divided by 3, then F(110110...1101101)=1000...000, hence there is always a unique solution, given by B^(B<<<1)^(B<<<3)^(B<<<4)^(B<<<5)^...^(B<<<(N-3))^(B<<<(N-1)).
  • If N has a remainder of 2 when divided by 3, then F(101101...10110110)=1000...000, again implying a unique solution, given by B^(B<<<2)^(B<<<3)^(B<<<5)^(B<<<6)^...^(B<<<(N-2))^(B<<<(N-1)).
  • ***********************************************CODE*************************************************************
  1. #include<iostream>
  2. using namespace std;
  3. #include<bits/stdc++.h>
  4. int ans1[1000000],ans2[1000000],ans3[100000],ans4[1000000];// EACH ARRAY CONTAI //N 1 GENERATED SEQ
  5. int main()
  6. {
  7. int t;
  8. cin>>t;
  9. while(t--)
  10. {
  11. int f1=0,f2=0,f3=0,f4=0;
  12. char arr[1000000];
  13. cin>>arr;
  14. int len=strlen(arr);
  15. int count =0;
  16. /* case 0 0*/// IF FIRST 2 BITS ARE 0 0
  17. int a=0;
  18. int b=0;
  19. ans1[0]=0;
  20. ans1[1]=0;
  21. for(int i=2;i<len;i++)
  22. {
  23. if(arr[i-1]-'0'==b)// USING CORRESPONDING GIVEN SEQUENCE TO DECIDE //NEXT BIT
  24. {
  25. int temp=b ; // IF SAME (CHECKED AVOVE) MEANS BOTH SIDE MUST //BE SAME BIT SO JUST COPY SAME BIT AS NEXT BIT
  26. b=a;
  27. a=temp;
  28. ans1[i]=b;
  29. }
  30. else
  31. {
  32. int temp=a;// NOT SAME THAN BOTH SIDE MUST BE NOT SAME BIT
  33. a=b; // SO GIVE TOGGELED VALUE
  34. b=1-temp;
  35. ans1[i]=b;
  36. }
  37. }
  38. // CHECKING FOR VALID SEQUENCE GENERATED
  39. if((arr[0]=='0' && b==0) ||(arr[0]=='1' && b==1) )// CHECKIG FOR 1ST BIT
  40. {
  41. // CHECKING FOR LAST BIT
  42. if(((arr[len-1]-'0'==b && a==0) || (a==1 && arr[len-1]-'0'!=b)))
  43. {
  44. // cout<<"test1 sucessfull "<<endl;
  45. count++; // BOTH 1 ST AND LAST BIT ARE VALID SO INCREAST COUNTER
  46. f1=1;
  47. }
  48. }
  49. // case 0 1
  50. a=0;
  51. b=1;
  52. ans2[0]=0;
  53. ans2[1]=1;
  54. for(int i=2;i<len;i++)
  55. {
  56. if(arr[i-1]-'0'==b)
  57. {
  58. int temp=b;
  59. b=a;
  60. a=temp;
  61. ans2[i]=b;
  62. }
  63. else
  64. {
  65. int temp=a;
  66. a=b;
  67. b=1-temp;
  68. ans2[i]=b;
  69. }
  70. }
  71. if((arr[0]=='0' && b==1) ||(arr[0]=='1' && b==0))
  72. {
  73. if(((arr[len-1]-'0'==b && a==0) || a==1 && arr[len-1]-'0'!=b))
  74. {
  75. // cout<<"test2 sucessfull "<<endl;
  76. count++;
  77. f2=1;
  78. }
  79. }
  80. /* for(int i=0;i<len;i++) cout<<ans2[i]<<" ";
  81. cout<<endl;*/
  82. // case 1 0
  83. a=1;
  84. b=0;
  85. ans3[0]=1;
  86. ans3[1]=0;
  87. for(int i=2;i<len;i++)
  88. {
  89. if(arr[i-1]-'0'==b)
  90. {
  91. int temp=b;
  92. b=a;
  93. a=temp;
  94. ans3[i]=b;
  95. }
  96. else
  97. {
  98. int temp=a;
  99. a=b;
  100. b=1-temp;
  101. ans3[i]=b;
  102. }
  103. }
  104. if((arr[0]=='0' && b==1) ||(arr[0]=='1' && b==0))
  105. {
  106. if ((arr[len-1]-'0'==b && a==1) || (a==0 && arr[len-1]-'0'!=b))
  107. {
  108. //cout<<"test3 sucessfull "<<endl;
  109. f3=1;
  110. count++;
  111. }
  112. }
  113. /* for(int i=0;i<len;i++) cout<<ans3[i]<<" ";
  114. cout<<endl;*/
  115. // case 1 1
  116. a=1;
  117. b=1;
  118. ans4[0]=1;
  119. ans4[1]=1;
  120. for(int i=2;i<len;i++)
  121. {
  122. if(arr[i-1]-'0'==b)
  123. {
  124. int temp=b;
  125. b=a;
  126. a=temp;
  127. ans4[i]=b;
  128. }
  129. else
  130. {
  131. int temp=a;
  132. a=b;
  133. b=1-temp;
  134. ans4[i]=b;
  135. }
  136. }
  137. if((arr[0]=='0' && b==0) ||(arr[0]=='1' && b==1) )
  138. {
  139. if(((arr[len-1]-'0'==b && a==1) || a==0 && arr[len-1]-'0'!=b))
  140. {
  141. count++;
  142. f4=1;
  143. }
  144. }
  145. /* for(int i=0;i<len;i++) cout<<ans4[i]<<" ";
  146. cout<<endl;*/
  147. if(count ==0 ) cout<<"No solution"<<endl;
  148. else if(count>1) cout<<"Multiple solutions"<<endl;
  149. else
  150. {
  151. // FIND WHICH SEQUENCE WAS VALID AND PRINT
  152. if(f1==1)
  153. {
  154. for(int i=0;i<len;i++)cout<<ans1[i];
  155. }
  156. else if(f2==1)
  157. {
  158. for(int i=0;i<len;i++)cout<<ans2[i];
  159. }
  160. else if(f3==1)
  161. {
  162. for(int i=0;i<len;i++)cout<<ans3[i];
  163. }
  164. else if(f4==1)
  165. {
  166. for(int i=0;i<len;i++)cout<<ans4[i];
  167. }
  168. cout<<endl;
  169. }
  170. }
  171. }

Birthday Candles IMPLIMENTATION

Birthday Candles 
Solved

Problem code: CANDLE

All submissions for this problem are available.

The chef is preparing a birthday cake for one of his guests,
and his decided to write the age of the guest in candles on the cake.
There are 10 types of candles, one for each of the digits '0' through '9'.
The chef has forgotten the age of the guest, however, so doesn't know whether he has enough candles of the right types.
For example, if the guest were 101 years old, the chef would need two '1' candles and one '0' candle.
Given the candles the chef has, your task is to determine the smallest positive integer that cannot be represented with those candles.

Input:

Input will begin with an integer T≤100, the number of test cases.
Each test case consists of a single line with exactly 10 integers, each between 0 and 8, inclusive.
The first integer of each test case represents the number of '0' candles the chef has,
the second integer represents the number of '1' candles the chef has, and so on.

Output:

For each test case, output on a single line the smallest positive integer that cannot be expressed with the given candles.

Sample input:

3
2 1 1 4 0 6 3 2 2 2
0 1 1 1 1 1 1 1 1 1
2 2 1 2 1 1 3 1 1 1
 

Sample output:

4
10
22
 

-------------------------------------------------editorial-------------------------------------------------------------------------------
CHECK WHETHER 1 TO 10 CAN BE MADE OR NOT IF NOT THAN GIVE ANSWER AS IN CASE OF TEST CASE 1 
ELSE
CHANCE OF 1 ST NUMBER NO TO FORM IS 10 ,THAN 11 , 22 , 33 , 44 , 55, 66 , 77 ...
100 111,222,333,444......
1000 1111,2222,3333.....
10000 11111 ,22222,33333.....
HENCE COUNT NO OF 0 1,..9 WHOSE VALUE IS LESS THAN WILL GIVE THE ANSWER ..
ALSO NOTE THAT NO OF '  0 ' CAN BE 1 LESS THAN ANY OTHER NO ..
******************************************************CODE*****************************************
  1. #include<iostream>
  2. using namespace std;
  3. #include<bits/stdc++.h>
  4. int main()
  5. {
  6. int t;
  7. cin>>t;
  8. while(t--)
  9. {
  10. int arr[11];
  11. for(int i=0;i<10;i++)
  12. {
  13. cin>>arr[i];
  14. }
  15. int ans=0;
  16. int cc=1;
  17. int i=0;
  18. int f=0;
  19. while(1)
  20. {
  21. if(i==0 )
  22. {
  23. if(arr[i]<cc-1)
  24. {
  25. f=1;
  26. cout<<"1";
  27. for(int j=1;j<cc;j++) cout<<"0";
  28. }
  29. }
  30. else
  31. {
  32. if(arr[i]<cc)
  33. {
  34. f=1;
  35. for(int j=0;j<cc;j++) cout<<i;
  36. }
  37. }
  38. i++;
  39. i%=10;
  40. if(f==1) break;
  41. if(i==0)
  42. {
  43. cc++;
  44. }
  45. // cout<<"i "<<i<<" cc "<<cc<<endl;
  46. }
  47. cout<<endl;
  48. }
  49. }