1.题目描述:
Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle.
More formally, there are 3n gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the circle by numbers from 0 to 3n - 1, let the gnome sitting on the i-th place have ai coins. If there is an integer i (0 ≤ i < n) such that ai + ai + n + ai + 2n ≠ 6, then Tanya is satisfied.
Count the number of ways to choose ai so that Tanya is satisfied. As there can be many ways of distributing coins, print the remainder of this number modulo 109 + 7. Two ways, a and b, are considered distinct if there is index i (0 ≤ i < 3n), such that ai ≠ bi (that is, some gnome got different number of coins in these two ways).
A single line contains number n (1 ≤ n ≤ 105) — the number of the gnomes divided by three.
Print a single number — the remainder of the number of variants of distributing coins that satisfy Tanya modulo 109 + 7.
1
20
2
680
20 ways for n = 1 (gnome with index 0 sits on the top of the triangle, gnome 1 on the right vertex, gnome 2 on the left vertex):
给一个n,表示一张桌子坐了3*n个人
你要给每一个人发钱,可以发1,2,3块,第i个人的钱是ai (i从0开始)
有一个要求:. ai + ai + n + ai + 2n ≠ 6,
也就是只要有 一组ai + ai + n + ai + 2n ≠ 6.就可以了
3.解题思路:当n=1时,_______ _______ ______ 三个数每位上可以填1,2,3三个数,所以,所有可能的结果就是3*3*3 不满足条件的有2 2 2这一种情况,还有1 2 3这三个数字,总共有6种情况,所以答案是27-1-6=20;当n=2时,所有可能的结果是3*3*3*3*3*3-?=680 ,可以推测出减去49即7*7
所以找到的规律就是:
当n=1时, 27-7
当n=2时 27*27-7*7
注意点:因为最后要模上一个数,中间去模的过程中要注意
4.AC代码:#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 1111
#define eps 1e-5
#define pi acos(-1.0)
const ll mod = 1e9 + 7;
ll mod_pow(ll x, ll n)
{
ll ans = 1;//返回的值,x的n次方
while (n)
{
if (n & 1)
ans = (ans * x) % mod; // 奇数位
x = x*x%mod; // 每位都平方
n >>= 1; // 右移
}
return ans;
}
int main()
{
/*
#ifdef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
*/
int n;
while (scanf("%d", &n) != EOF)
{
ll ans = (mod_pow(27, n)%mod - mod_pow(7, n)%mod + mod)%mod;
printf("%lld\n", ans);
}
return 0;
}