实习摸鱼、刷一刷题和八股。
1. 小红的好数组
小红定义一个数组为“好数组”,当且仅当该数组满足以下条件
1. 数组仅由0、1、2三种元素组成
2. 数组相邻的元素不想等
例如:1,2,1,2,0,1是好数组。小红定义一个数组的“陡峭值”为该数组相邻元素的差的绝对值之和。例如,[2,1,2,0,1]的陡峭值为|2-1|+|1-2|+|2-0|+|0-1|=5,小红想知道,长度为n的所有好数组的陡峭值之和是多少?由于答案过大请对10^9+7取模,数据范围2<=n<=10^9
示例1:
输入
2
输出
8
思路:dp思路比较好想,但是n的数据范围太大了,只能过很少一部分用例。原因出现求幂的过程中,应该使用快速幂的方式,逐步进行取模
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
const int MOD = 10e9 + 7;
long long fastMi(long long x, int n, int m) {
int res = 1;
while (n > 0) {
if (n % 2 == 1) res = (res * x) % m;
x = (x * x) % m;
n /