HTML & JS
#include<bits/stdc++.h>
#define M 1000000007
using namespace std;
long long int done [100008][2];
long long int maxmanhelp(int n,int k,int state)
{
	
	done[1][0]=1;done[1][1]=1;
	for(int i=2;i<=n;i++)
	{
		for(int j=0;j<2;j++)
		{
			if(j==0)
			done[i][j]=(((k-1)%M*done[i-1][0]%M)%M+(done[i-1][1]%M))%M;
			else if(j==1)
			done[i][j]=((k-1)%M*done[i-1][0]%M)%M;
		}
	 }
		return done[n][state];


}
void maxways(int n,int k)
{
	memset(done,-1,sizeof(done));
	if(k==1)
	{
		if(n=1)
		cout<<"1"<<endl;
		if(n>2)
		cout<<"0"<<endl;
	}
	else
	{
	 cout<<(k%M*maxmanhelp(n,k,0)%M)%M<<endl;
    }
}
int main()
{
	ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
    int n,k;
    cin>>n>>k;
    maxways(n,k);
    }
    return 0;
}

Generated URL:

Output:

No Output
Report Bug