combinatorics *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

//you can use greater and lee_equal on the place of less
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define fastio()       ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define M              1000000007
#define int            long long int
#define F              first
#define S              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define max_heap       priority_queue <int>
#define min_heap       priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl

void print2d(vector<vector<int>> v)
{
    int n = v.size();
    int m = v[0].size();
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }
}  

//------------------------------------------------------------------------------->get hash function
int get_hash(string s)
{
    int h = 0;
    for(char c : s)
    {
        h = (h * 31 + (c - 'a' + 1)) % M;
    }
    return h;
}
int get_hash2(string s)
{
    int h = 0;
    int p_power = 1;
    int p = 31;
    for (char c : s)
    {
        h = (h + (c - 'a' + 1) * p_power) % M;
        p_power = (p_power * p) % M;
    }
    return h;
}
//*****END

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
	const char* comma = strchr (names + 1, ',');
	cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}
int ans;
int arr[9];
int n , k;
bool check(int n ,int cnt)
{
	if(cnt > k) return false;
	if(n == 1) return true;

	return check(arr[n] ,cnt+1);
}
void dfs(int x)
{
	if(x > k)
	{
		bool flag = 0;
		for(int i=1; i<=k; i++)
		{
			if(check(arr[i] , 0) == false)
			{
				flag =1;
				break;
			}
		}
		if(flag == 0)
		{
			// print(arr);
		 	ans++;
		}
		return;
	}

	for(int i=1; i<=k; i++)
	{
		arr[x] = i;
		dfs(x+1);
	}

}

void solveit() 
{
	cin>>n>>k;

	dfs(1);
	// cout<<ans<<endl;

	for(int i=k+1; i<=n; i++)
	{
		ans = (ans * (n-k))%M;
	}
	cout<<ans<<endl;
	
	
}

int32_t main()
{
	fastio(); 

#ifndef ONLINE_JUDGE
	freopen("input.txt",  "r",  stdin);
	freopen("output.txt", "w", stdout);
#endif

	clock_t z = clock();
	//createsieve();
	int t = 1;
	// cin >> t;
	while (t--) solveit();

	cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
	return 0;
}


Comments

Submit
0 Comments
More Questions

42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility