A small balloon with the little sister to the painted subject description A small and little sister idle, bored, they pass by a store and see there are a lot of balloons colorless, so they suddenly have a

A small and little sister to balloon coloring
Time limit: 1 sec Memory Limit: 128 MB
submit: correct 2: 1

Title Description
A small and little sister idle, bored, they pass by a store and see there are a lot of balloons colorless, so they suddenly have an idea, to buy their own balloon, the balloon is painted a different color, and then sent to the store next to the primary school in children. They bought balloons n, m in the paint, they balloon in a row, they are sent to the order of the children of the balloon are arranged. Initially, the balloon is colorless, and now they want to use the m kinds of paint balloons coloring, they do not want to give the children a balloon adjacent same color, same balloon that is arranged adjacent to the color is considered adjacent children's balloon same color, the same as if the color of the adjacent balloon, the balloon has these two children will not be happy. A small little sister hope to find the children's coloring scheme happy how many (ie two adjacent balloons color is not the same coloring scheme how many).

The answer to fetch more than 1,000,000,007 output.

Input
input two integers n (1 <= n <= 1012), m (1 <= m <= 108).

Output
Output line represents the answer.

Sample input

2 2

Sample Output

2

Thinking: Suppose there are n balloons, m colors, then only in the n-1 balloons, more to one of its ball of m colors fills, then the last color on the balloon surface can not be in front of it one and the same, so it has the C (1, m -1); seed color case, because it is independent of each other, so it is recursion formulas

dp[i][j] = dp[i - 1][j] * dp[1][m - 1];

So it can be solved

answer:

#include <iostream>

using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
int f2;
long long ksm(ll m, ll n)
{
	long long res = 1;
	while (n)
	{
		if (n&1) res = res * m % mod;
		m = m * m % mod;
		n >>= 1; 
	
	} 
	return res;
}

int main()
{
	ll n, m;
	while(cin >> n >> m){
	
	n %= mod;
	m %= mod;
	if (m == 1)
	{
		if (n == 1)
			cout << 1 << endl;
		else cout << 0 << endl;
	}
	else
	{
		cout << m * ksm(m - 1, n - 1) % mod << endl;
	}
}
	return 0;
}
Published 18 original articles · won praise 2 · Views 555

Guess you like

Origin blog.csdn.net/xiaoxiao66668/article/details/104232246