CF584B Kolya and Tanya(数学计数,快速幂)

来源:互联网 发布:apache maven是什么 编辑:程序博客网 时间:2024/06/15 11:52
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 ≤ 105) — 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.
Sample test(s)
Input

1
Output
20
Input
2
Output

680

题意:3*n个在圆桌上,每个人可以分得1-3金币,但是要满足A[i]+a[n+i]+a[n*2+i]!=6,即正三角的三个人金币和不能是6,问所有的组合数。

思路:先不考虑条件限制的话,那么对于没个正三角形来说,有3*3*3种可能,一共有n个正三角形,那么就是(3^3)^n,现在考虑条件限制,

即不能有(1,2,3)和(2,2,2),(1,2,3)共6种,加上(2,2,2) 共7种,也就是说,一个三角不满足的有7个,那么所有的就是7^n,所以答案是

27^n-7^n,用快速幂取模即可

#include<cstdio>#include<algorithm>using namespace std;const int maxn=10003;const int inf=1<<28;#define LL long longconst LL mod=1e9+7;LL powmod(LL a,LL b){    LL ans=1;    while(b){        if(b&1)ans=ans*a%mod;        a=a*a%mod;        b>>=1;    }    return ans%mod;}int main(){    LL n;    while(~scanf("%lld",&n)){        printf("%lld\n",(powmod(27,n)-powmod(7,n)+mod)%mod);    }    return 0;}


0 0
原创粉丝点击