目录
题目(来源:Cdeforces 584B):
B. Kolya and Tanya
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
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).
Input
A single line contains number n (1 ≤ n ≤ ) — the number of the gnomes divided by three.
Output
Print a single number — the remainder of the number of variants of distributing coins that satisfy Tanya modulo 109 + 7.
Examples
input
1
output
20
input
2
output
680
Note
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):
题目大意:
在一个圆上,3n个点依次标号,然后标号为i,i+n,i+2n的点连成三角形。然后每个点分配一个值(在1-3范围内)。然后只要有一个三角形的点的值的和不为6就成功。问有多少种分配方案。
算法思想:
排列组合。总共个可能,减去个不可能的结果。这个7怎么来的呢?->> 6=2+2+2(1种),1+2+3(6种)所以总排列数减去不符合的排列数即为符合数,但结果那么大,就得用到快速幂,不熟悉的小伙伴看这里(是看一次爱一次的大佬文章呀~):传送门_快速幂算法
本题代码:
#include<stdio.h>
const int mod=1e9+7;
typedef long long ll;
ll fast_pow(ll base,ll n){
ll ans=1;
while(n){
if(n&1)ans=(ans*base)%mod;
base=(base*base)%mod;
n>>=1;
}
return ans;
}
int main(){
int n;
scanf("%d",&n);
printf("%lld",(fast_pow(27ll,n)-fast_pow(7ll,n)+mod)%mod);
return 0;
}