Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Further to the question question I solved it in following way.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <stdio.h>
int main ( int argc, char **argv) {
 int T; // number of test cases
 std::cin >> T;
 for ( int i = 0 ; i < T; i++) {
 int N; // number of childrens
 std::cin >> N;
 int M; // number of rounds
 std::cin >> M;
 long long childrens[50];
 for ( int j = 0; j < N; j++ ) 
 std::cin >> childrens[j];
 int pos = 0;
 for ( int i = 0; i < N ; i++) {
 long long childs[50];
 for ( int j = 0; j < N; j++ ) 
 childs[j] = childrens[j];
 for ( int j = 0; j < M; j++) {
 int left = ( pos == 0) ? N - 1 : pos - 1;
 int right = ( pos + 1 >= N ) ? 0 : pos + 1;
 childs[pos] = (childs[pos] + childs[left] + childs[right])% 1000000007;
 pos++;
 pos = ( pos >= N ) ? 0 : pos;
 }
 std::copy(childs ,childs + N ,std::ostream_iterator<long long>(std::cout," "));
 std::cout << std::endl;
 }
 std::cout << std::endl;
 }
 return 0;
}

but the M can be 10^9, My code is going to take lot of time to finish, What are the various ways I can optize this.

Further to the question I solved it in following way.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <stdio.h>
int main ( int argc, char **argv) {
 int T; // number of test cases
 std::cin >> T;
 for ( int i = 0 ; i < T; i++) {
 int N; // number of childrens
 std::cin >> N;
 int M; // number of rounds
 std::cin >> M;
 long long childrens[50];
 for ( int j = 0; j < N; j++ ) 
 std::cin >> childrens[j];
 int pos = 0;
 for ( int i = 0; i < N ; i++) {
 long long childs[50];
 for ( int j = 0; j < N; j++ ) 
 childs[j] = childrens[j];
 for ( int j = 0; j < M; j++) {
 int left = ( pos == 0) ? N - 1 : pos - 1;
 int right = ( pos + 1 >= N ) ? 0 : pos + 1;
 childs[pos] = (childs[pos] + childs[left] + childs[right])% 1000000007;
 pos++;
 pos = ( pos >= N ) ? 0 : pos;
 }
 std::copy(childs ,childs + N ,std::ostream_iterator<long long>(std::cout," "));
 std::cout << std::endl;
 }
 std::cout << std::endl;
 }
 return 0;
}

but the M can be 10^9, My code is going to take lot of time to finish, What are the various ways I can optize this.

Further to the question I solved it in following way.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <stdio.h>
int main ( int argc, char **argv) {
 int T; // number of test cases
 std::cin >> T;
 for ( int i = 0 ; i < T; i++) {
 int N; // number of childrens
 std::cin >> N;
 int M; // number of rounds
 std::cin >> M;
 long long childrens[50];
 for ( int j = 0; j < N; j++ ) 
 std::cin >> childrens[j];
 int pos = 0;
 for ( int i = 0; i < N ; i++) {
 long long childs[50];
 for ( int j = 0; j < N; j++ ) 
 childs[j] = childrens[j];
 for ( int j = 0; j < M; j++) {
 int left = ( pos == 0) ? N - 1 : pos - 1;
 int right = ( pos + 1 >= N ) ? 0 : pos + 1;
 childs[pos] = (childs[pos] + childs[left] + childs[right])% 1000000007;
 pos++;
 pos = ( pos >= N ) ? 0 : pos;
 }
 std::copy(childs ,childs + N ,std::ostream_iterator<long long>(std::cout," "));
 std::cout << std::endl;
 }
 std::cout << std::endl;
 }
 return 0;
}

but the M can be 10^9, My code is going to take lot of time to finish, What are the various ways I can optize this.

Source Link
Avinash
  • 121
  • 2

Circle Summation (30 Points) InterviewStree Puzzle cont

Further to the question I solved it in following way.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <stdio.h>
int main ( int argc, char **argv) {
 int T; // number of test cases
 std::cin >> T;
 for ( int i = 0 ; i < T; i++) {
 int N; // number of childrens
 std::cin >> N;
 int M; // number of rounds
 std::cin >> M;
 long long childrens[50];
 for ( int j = 0; j < N; j++ ) 
 std::cin >> childrens[j];
 int pos = 0;
 for ( int i = 0; i < N ; i++) {
 long long childs[50];
 for ( int j = 0; j < N; j++ ) 
 childs[j] = childrens[j];
 for ( int j = 0; j < M; j++) {
 int left = ( pos == 0) ? N - 1 : pos - 1;
 int right = ( pos + 1 >= N ) ? 0 : pos + 1;
 childs[pos] = (childs[pos] + childs[left] + childs[right])% 1000000007;
 pos++;
 pos = ( pos >= N ) ? 0 : pos;
 }
 std::copy(childs ,childs + N ,std::ostream_iterator<long long>(std::cout," "));
 std::cout << std::endl;
 }
 std::cout << std::endl;
 }
 return 0;
}

but the M can be 10^9, My code is going to take lot of time to finish, What are the various ways I can optize this.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /