Thread: program taking too long to complete

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    program taking too long to complete

    i'am trying to solve a problem:

    Given a string of N 0's and M 1's,how many unique permutations of this string start with 1?

    answers for each test case is printed modulo(10^9 +7).

    however my program takes too long to complete.
    please suggest a much faster method.

    Code:
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int main()
    {
        int T;
        cin>>T;
        string s;
        int z,o;
        long long ans;
        while(T--)
        {
            s="";
            ans=1;
            cin>>z>>o;
            for(auto i=0;i<o;++i)
                s+='1';
            for(auto i=0;i<z;++i)
                s+='0';
            while(prev_permutation(s.begin(),s.end()))
            {
                if(s[0]=='0')
                    break;
                ++ans;
            }
            cout<<ans%(1000000007)<<'\n';
        }
    }

  2. #2
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    here is the range:
    1<=N,M<=1000

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Why don't you research the relevant maths which tells you the answer directly (using some function), rather than trying to brute force the answer by counting every possible combination.

    Work smarter, not harder.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <Winsock> connect() taking too long/never working.
    By darkAlly in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-26-2014, 10:09 AM
  2. please help e to complete the program
    By trojan32 in forum C Programming
    Replies: 3
    Last Post: 01-27-2013, 01:40 AM
  3. Little help to complete my program..pls..
    By scorpio76 in forum C Programming
    Replies: 14
    Last Post: 02-24-2011, 05:58 AM
  4. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  5. Replies: 3
    Last Post: 03-21-2004, 06:02 PM