150B - Quantity of Strings - CodeForces Solution


combinatorics dfs and similar graphs math *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int n,m,k,p,a[2005],num;
long long ans=1,mod=1e9+7;
int A(int x){
	if(x==a[x])return x;
	a[x]=A(a[x]);
	return a[x];
}
int main(){
	cin>>n>>m>>k;
	num=n;
	for(int i=1;i<=n;i++)a[i]=i;
	for(int i=1;i<=n-k+1;i++){
		for(int I=i,J=i+k-1;I<J;I++,J--){
			if(A(I)!=A(J))num--;
			a[a[I]]=A(J);
		}
	}
	for(int i=1;i<=num;i++)ans*=m,ans%=mod;
	cout<<ans;
  	return 0;
}
 		 		  		   	  	  				  	  			


Comments

Submit
0 Comments
More Questions

988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem
1177B - Digits Sequence (Hard Edition)
1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking
1395B - Boboniu Plays Chess
1475D - Cleaning the Phone
617B - Chocolate
1051B - Relatively Prime Pairs
95B - Lucky Numbers
1692D - The Clock
1553D - Backspace
1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest
385A - Bear and Raspberry
1311B - WeirdSort
1713F - Lost Array
236B - Easy Number Challenge
275A - Lights Out