zoukankan      html  css  js  c++  java
  • Codeforces Round #324 (Div. 2) B

    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.

    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):

    题意: 输入一个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
    ",(ans1-ans2)%mod);
    32     else
    33     printf("%I64d
    ",(ans1+mod-ans2)%mod);
    34     return 0;
    35 }
  • 相关阅读:
    基本的CRUD操作
    java.lang.IllegalStateException: Cannot forward after response has been committed的一个情况解决方法
    一个解决过程:Servlet [某路径xxx] in web application [/项目xxx] threw load() exception和java.lang.ClassNotFoundException XXX
    卸载时候出现: windows installer 程序有问题。此安装需要的dll不能运行 的一个解决方法
    jdk各版本特性
    抽象类与接口
    Integert 与 int例子详解
    Spring(mvc)思维导图
    关于存储数组有序无序
    遍历回顾(手稿)-先序中序求后序----和----中序后序求先序
  • 原文地址:https://www.cnblogs.com/hsd-/p/5550841.html
Copyright © 2011-2022 走看看