Showing posts with label Combinatorics. Show all posts
Showing posts with label Combinatorics. Show all posts

সমাবেশ (Combination) Calculation nCr

Standard
/**Bismillahir Rahmanir Rahim.**/

#include <bits/stdc++.h>

using namespace std;

long long ara[52][52];
long long nCr(int n, int r)
{
    if(n == r) return 1;
    if(r == 1) return n;
    if(ara[n][r]!=0) return ara[n][r];
    return ara[n][r] = nCr(n-1, r) + nCr(n-1, r-1);
}
int main()
{
    int n = 50, r = 25;
    long long ans = nCr(n, r);
    cout << ans << endl;
    return 0;
}

ফেক্টরিয়ালের পেছনের ০ (Trailing Zeros on Factorial)

Standard
/**Bismillahir Rahmanir Rahim.**/

#include <bits/stdc++.h>

using namespace std;

int zero(int x)
{
    int ans = 0;
    for(int i=5; x/i>0;i*=5)
        ans += x/i;
    return ans;
}
int main()
{
    int n;
    cin >> n ;
    cout << zero(n) << endl;
    return 0;
}

int main()
{
    int ans = bigMod(4, 100001, 1234);
    cout << ans << endl;
    return 0;
}

বিগমড (Bigmod)

Standard
/**Bismillahir Rahmanir Rahim.**/

#include <bits/stdc++.h>

using namespace std;

int bigMod(int a, int n, int m)
{
    //here, a, n and m refer to (a^n)%m

    if(n==0) return 1 % m ;
    int x = bigMod(a, n/2, m);
    x = (x*x) % m;
    if(n%2) x = (x*a)%m;
    return x%m;
}
int main()
{
    int ans = bigMod(4, 100001, 1234);
    cout << ans << endl;
    return 0;
}