vok8's blog

By vok8, history, 4 years ago, In English

Laxman and Maths

Logic (Explanation)
Problem Setter's Code

-/-/-

Raavan and Subarray

Logic (Explanation)
Problem Setter's Code

-/-/-

Bharat and Possibilities

Logic (Explanation)
Problem Setter's Code

-/-/-

Hanuman and Tree

Logic (Explanation)
Problem Setter's Code

-/-/-

Lord Ram and XorOrXorOr Power

  • Problem Setter: vok8
Logic (Explanation)
Problem Setter's Code

-/-/-

Luv Kush and FX Sequence

  • Problem Setter: vok8
Logic (Explanation)
Problem Setter's Code

-/-/-

Sita Mata and Time Machine

  • Problem Setter: vok8
Logic (Explanation)
Problem Setter's Code
  • Vote: I like it
  • +25
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it -13 Vote: I do not like it

@admin please someone provide the Java code for the Lord Ram and XorOrXorOr Power problem as the editotial.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +16 Vote: I do not like it

    Editorial is not for direct solution. It is to give you some hint, or some guidance for the problem. Try in java yourself, and if you have some problem then ask it.

»
4 years ago, # |
  Vote: I like it +12 Vote: I do not like it

We can also solve Bharat and Possibilities by the concept of non negative integral solution.

For example if k = 10(length of array) and n = 3([1,3] — values can be used in array). Let c1 = count of 1 in the array, c2 = count of 2 in the array, c3 = count of 3 in the array, then c1 + c2 + c3 = 10

Irrespective of count of 1,2,3, whatever 10 elements we will select, needs no arrangement as only the reversed sorted one will be non-increasing.

So for the solution we need to find non negative integral solutions for c1 + c2 + c3 = 10 => (10+3-1)C(3-1) or (10+3-1)C(10)

In general we will get, c1 + c2 + ... + cn = k => (k+n-1)C(k) or (k+n-1)C(n-1)

Overall enjoyed the contest, kudos to setters :)

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Woah!

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    "needs no arrangement as only the reversed sorted one will be non-increasing." Can someone explain this with a example?Please.

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it +1 Vote: I do not like it

      Let's say k=3 and n=5, and let's say:

      count(1)=2 count(2)=2 count(3)=1

      n = count(1) + count(2) + count(3) = 5

      For these values of count(1), count(2), and count(3), there is only one arrangement, which satisfies the condition of non-increasing order, and that arrangement is:

      3 2 2 1 1

      So, that statement means, there is only one possible arrangement for each unique permutation of count(1),...,count(k).

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        Thank you

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        I wrote this code using your approach,it is giving wrong answer. Can you point out what I did wrong?

        ...
        #include<bits/stdc++.h>
        #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
        #define ll long long
        #define ull unsigned long long
        #define M 1000000007
        using namespace std;
        ll C2(int n, int k){
            ll res = 1;
            if ( k > n - k )
                k = n - k;
        
            for (ll i = 0; i < k; ++i)
            {
                res *= (n - i);
                res %= M;
                res /= (i + 1);
                res %=M;
            }
            return res;
        }
        
        int main(){
            int n,k;
            cin>>n>>k;
            ll ans;
            ans= C2(k+n-1 ,k);
            cout<< ans << "\n";
            return 0;
        }
        
        • »
          »
          »
          »
          »
          4 years ago, # ^ |
          Rev. 3   Vote: I like it +1 Vote: I do not like it
          Snippet

          The above part of your code, will cause overflow in the value of res. Suppose the value of res is less than (i+1), before this statement res /= (i + 1);. Hence, your code will lead you to a wrong value of res.

          Instead, of doing what you have done in your code, maintain two res variables, and at the end, use the concept of Modular Inverse to calculate and return the value. You can check the below snippet, for more clarification.

          Snippet
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone suggest an alternative solution for the last problem?

  • »
    »
    4 years ago, # ^ |
    Rev. 4   Vote: I like it +7 Vote: I do not like it

    I did some preprocessing. I used a dp like dp[n][n][K] (K is 1e5). Here dp[i][j][k] means the probability of going from ith state to jth state in exactly k steps.

    We can do it like dp[I][j][k]= sum of (dp[i][u][k-1]*dp[u][j][1]) for all u from 1 to n because in (k-1)the step we can be in any state.

    I am wondering can we decrease the 3rd dimension of dp here from K to log(K) as we do in binary lifting. We will just store the answer for only those steps which are the power of 2. Can somebody confirm it?

    In case you want to check out my submission https://www.codechef.com/viewsolution/33157897.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    I haven't fully read the editorial's approach. But here is what I did,

    Consider dp[start_index][ending_index][number_of_jumps] as the probability of reaching from starting index to ending index with number_of_jumps. Here we can precompute this dp table and answer queries in O(1).

    Now for jump = 1, it's simple dp[ i ][ j ][ 1 ] = grid[ i ][ j ]/sum[ i ] .

    Now let's say for jump = x, we can calculate as following,

    Consider we start from index 'i', want to reach 'j' in 'K' steps. Then try every possible ending index lets say 'x' we reach from 'i' in 'K-1' steps.

    Then the transition is easy to see, Loop x from 1 to n.(i.e. every possible ending index we can reach in K — 1 steps)

    dp[ i ][ j ][ K ] += dp[ i ][ x ][ K — 1 ] * dp[ x ][ j ][ 1 ].

    I hope you get it, Here is my submission if you want to check: Submission

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hi bro, can i know how all of you came to know about this competition?

»
4 years ago, # |
Rev. 5   Vote: I like it +1 Vote: I do not like it

I used top down approach for Bharat and Possibilities.But I got TLE . dp[val][ind]- tells number of ways for placing val as first element with array of size n-ind

Can any1 optimize my code

#include<bits/stdc++.h>
using namespace std;
#define boost ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long int ll;
#define mod 1000000007

 ll n,k;
ll dp[2002][2002];
 ll solve(ll val,ll ind)
 {
 	if(ind==n-1)
 		return 1;
 	if(ind>=n)
 		return 0;
 	if(dp[val][ind]!=-1)
 		return dp[val][ind];
 	ll res=0;
 	for(int j=val;j>=1;j--)
 		res=(res%mod+((solve(j,ind+1))%mod))%mod;
 	return dp[val][ind]=res;
 }


int main()
{
    boost
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);    
    freopen("output.txt", "w", stdout);
#endif  
cin>>k>>n;
memset(dp,-1,sizeof(dp));
ll res=0;
for(int i=k;i>=1;i--)
res=(res%mod+(solve(i,0)%mod))%mod;
cout<<res<<"\n";
}
  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    You did not declare mod

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Hey, your code seems to be O(N*N*K), hence it is giving TLE.

    Use Suffix Sums, to reduce your code's complexity to O(N*K). (Also, mentioned in the Editorial)

    PS: You can check that your solution is O(N*N*K), and not O(N*K), by maintaining a global count variable, and just increment that variable in the 'for' loop of your recursive function, and then check the value of count for any values of N and K.

    I hope, it helps!

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      How to use suffix sums in this top-down approach case? I get it my solution is O(N*N*K)

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        Code

        Run the above code for N=2000 and K=2000, you will get the value of countt as:

        countt = constant * N * N * K

        Hence, showing up that your code runs in O(N*N*K) Time, which is bound to give TLE.

        I put up countt variable's idea, just to verify the complexity of your code.

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it +2 Vote: I do not like it

          I get that . Actually my question was how to use suffix sums in this top down approach?

          • »
            »
            »
            »
            »
            »
            4 years ago, # ^ |
              Vote: I like it +3 Vote: I do not like it

            Yes, I actually did same but couldn't optimize it so I had to write bottom up dp. Can anyone help?

            • »
              »
              »
              »
              »
              »
              »
              4 years ago, # ^ |
                Vote: I like it +9 Vote: I do not like it

              You can let $$$dp_{ij}$$$ denote the number of ways to get a sequence of length $$$i$$$ such that the last element is at least $$$j$$$. Then you get an $$$O(1)$$$ recurrence relation. https://www.codechef.com/viewsolution/33181662 I'm using $$$n$$$ as the length and $$$[1,k]$$$ as the range.

              • »
                »
                »
                »
                »
                »
                »
                »
                4 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                Nice solution!!

                One doubt, why didn't we take the recurrence relation as

                dp[n][k]=(solve(n-1, k+1)+solve(n-1, k))%p ?

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  The numbers of sequences of length $$$n$$$ ending with $$$k$$$ is $$$dp_{(n-1)(k)}$$$, because after any number greater than $$$k$$$, I can write a $$$k$$$. So the first term is the number of ways to get a sequence of length $$$n$$$ that ends with $$$k$$$, and the second term is the number of ways to get a sequence that ends with something larger than $$$k$$$, by the definition of the dp.

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

can anyone explain this recurrence in bharat and possibilities dp[i][j]=dp[i][j-1]+dp[i+1][j] for this i don't understand the second term this: dp[i+1][j]
and also can't we also iterate from 1 to n for(int i=n;i>=1;--i) { for(int j=2;j<=k;++j) { if(i==n) dp[i][j]=dp[i][j-1]; else dp[i][j]=dp[i][j-1]+dp[i+1][j];/* this recurrence */ dp[i][j]%=1000000007; } }

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    If we see at dp formula it says dp[a][j] = sum(dp[i][j-1]) where i from a to n.

    Now for i=n

    dp[n][j] = dp[n][j-1]

    So the value of dp[n][j] = dp[n][j-1]_______(eq-1)

    And now we have i=n-1

    So dp[n-1][j] = dp[n-1][j-1] + dp[n][j-1]

    But dp[n][j] = dp[n][j-1] from eq1.

    dp[n-1][j] = dp[n-1][j-1] + dp[n][j]

    Or dp[n-1][j] = dp[n-1][j-1] +dp[n][j-1]________(eq-2)

    And now if we have i = n-2

    So dp[n-2][j] = dp[n-2][j-1] + dp[n-1][j-1] + dp[n][j-1]

    But dp[n-1][j-1] + dp[n][j-1] = dp[n-1][j] (from eq-2)

    So now dp[n-2][j] = dp[n-2][j-1]+dp[n-1][j]

    So in general dp[i][j] = dp[i][j-1] + dp[i+1][j]

»
4 years ago, # |
Rev. 2   Vote: I like it +7 Vote: I do not like it

In Laxman and maths, one can solve by printing n for n times too, which will satisfy the given equation as sum of array = n*(n^3) = n^4 which is square of n^2.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone point out problem in this, It is giving wrong answer. question:- Luv Kush and FX Sequence

#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ull unsigned long long
#define MOD 998244353
using namespace std;
ll I[2][2];
ll A[2][2];
ll res[2][2];
void mul(ll A[2][2],ll B[2][2]){
    for(int i= 0 ;i < 2; i++){
        for(int j =0 ;j <2; j ++ ) {
            res[i][j] =0;
            for(int k=0;k<2; k++){
                res[i][j]=(res[i][j]+((A[i][k]* B[k][j])%MOD))%MOD;
            }
        }
    }
    for(int i = 0;i<2;i++) for(int j = 0 ;j < 2; j++) A[i][j] = res[i][j];
}
void powerExponentiation(ll A[2][2],ll n ) {
    for(int i = 0 ; i< 2; i++ ) {
        for(int j = 0 ;j < 2; j ++ ){
            if( i== j)
                I[i][j] = 1;
            else I[i][j] = 0;
        }
    }
    while(n){
        if(n%2 ==0) {
            mul(A,A);
            n/=2;
        }
        else{
            mul(I,A);
            n = n-1;
        }
    }
    for(int i =0;i<2;i++)for(int j=0;j<2;j++) A[i][j] = I[i][j];
}
int main(){
    int t;cin>>t;
    ll n,a,b,c,d,e,f;
    ll ans;
    while(t--){
        cin>>n>>a>>b>>c>>d>>e>>f;
        A[0][0] = 0;
        A[0][1] = f;
        A[1][0] = 1;
        A[1][1] = e;
        if(n ==0){
            ans = ((a * c ) % MOD +(a - c))%MOD;
            cout<<ans<<"\n";
        }
        else if( n == 1) {
            ans = ((b*d)%MOD + (b - d))%MOD ;
            cout<<ans<<"\n";
        }
        else{
            powerExponentiation(A,n-1);
            ll fnthterm1 = ((a*A[0][1])%MOD + (b* A[1][1])%MOD)%MOD;
            ll fnthterm2;
            if( n % 3 ==0)
                fnthterm2 = c% MOD;
            else if((n+ 1)%3 ==0)
                fnthterm2 = d% MOD;
            else
                fnthterm2 = (c ^ d) %MOD;
            ans = ((fnthterm1*fnthterm2)%MOD +(fnthterm1- fnthterm2)) %MOD;
            cout<< ans<<"\n" ;
        }
    }
    return 0;
}

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    a, b, c, d, e, f are already of the order of 10^18, better take modulo before multiplying.