染色问题 - LintCode
#ifndef C1444_H
#define C1444_H
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
class Solution {
public:
/**
* @param n: the number of sectors
* @param m: the number of colors
* @return: The total number of plans.
*/
int getCount(int n, int m) {
// Write your code here
/*
//动态规划求解
long long dp[100001];
long long num = 1e9 + 7;
dp[1] = m;
dp[2] = (long long)m*(m - 1) % num;
dp[3] = (long long)m*(m - 1)*(m - 2) % num;
for (int i = 4; i <= n; ++i)
dp[i] = (dp[i - 1] * (m - 2) + dp[i - 2] * (m - 1))%num;
return dp[n];*/
//直接求解得到结果
long long num = 1e9 + 7;
long long res = myPow(m - 1, n) % num + pow(-1, n - 2)*(m - 1);
return res;
}
//二分法求指数
long long myPow(int m, int n)
{
if (n == 0)
return 1;
if (n == 1)
return m;
long long num = 1e9 + 7;
long long res = myPow(m, n >> 1);
res = res * res % num;
if (n & 1 == 1)
res = res * m % num;
return res;
}
};
#endif