Codeforces Round #324 (Div. 2) B. Kolya and Tanya 快速幂
B. Kolya and Tanya
Time Limit: 1 Sec ?
Memory Limit: 256 MB
題目連接
http://codeforces.com/contest/584/problem/BDescription
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 Input
1Sample Output
20HINT
?
題意
給你一個(gè)環(huán),環(huán)上有3n個(gè)點(diǎn),每個(gè)點(diǎn)的權(quán)值可以是1-3,然后問你滿足a[i]+a[i+1]+a[i+2]!=6的方案有多少種
題解:
反面,a[i]+a[i+1]+a[i+2]=6的情況這三個(gè)數(shù)的取值一共有7種
那么答案就是 3^(3n) - 7^n就好了
代碼:
#include<stdio.h> #include<iostream> #include<math.h>using namespace std;#define mod 1000000007 long long quickpow(long long m,long long n,long long k) {long long b = 1;while (n > 0){if (n & 1)b = (b*m)%k;n = n >> 1 ;m = (m*m)%k;}return b; }int main() {long long n;cin>>n;cout<<(quickpow(3,3*n,mod) - quickpow(7,n,mod) + mod) % mod <<endl; }?
總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #324 (Div. 2) B. Kolya and Tanya 快速幂的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 晚上梦到门牙掉了是什么意思
- 下一篇: 梦到丈夫出车祸是什么预兆