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个位置 0~3n-1 每个位置的k等于(1,2,3)
若ai + ai + n + ai + 2n ≠ 6 则算做一种情况 问共有多少情况 % 1000000000+9
题解: 计算式子
ans1=3^3n%mod
ans2=7^n%mod
( ans1-ans2)%mod
若 ans1<ans2
则输出(ans1+mod-ans2)%mod
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<queue> 5 #include<stack> 6 #include<cmath> 7 #define ll __int64 8 #define pi acos(-1.0) 9 #define mod 1000000007 10 using namespace std; 11 ll n; 12 ll ans1,ans2; 13 ll quickmod(ll a,ll b) 14 { 15 ll sum=1; 16 while(b) 17 { 18 if(b&1) 19 sum=(sum*a)%mod; 20 b>>=1; 21 a=(a*a)%mod; 22 } 23 return sum; 24 } 25 int main() 26 { 27 scanf("%I64d",&n); 28 ans1=quickmod(3,3*n)%mod; 29 ans2=quickmod(7,n)%mod; 30 if(ans1>=ans2) 31 printf("%I64d\n",(ans1-ans2)%mod); 32 else 33 printf("%I64d\n",(ans1+mod-ans2)%mod); 34 return 0; 35 }