zoukankan      html  css  js  c++  java
  • 徐州网络赛A-Hard To Prepare【dp】【位运算】【快速幂】

    After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked Kokoro to deliver a Nogaku show during the feast. To enjoy the show, every audience has to wear a Nogaku mask, and seat around as a circle.

    There are N guests Reimu serves. Kokoro has 2^k2k masks numbered from 0,1,cdots,0,1,2^k - 12k1, and every guest wears one of the masks. The masks have dark power of Dark Nogaku, and to prevent guests from being hurt by the power, two guests seating aside must ensure that if their masks are numbered ii and jj , then ii XNOR jj must be positive. (two guests can wear the same mask). XNOR means ~(ii^jj) and every number has kk bits. (11 XNOR 1 = 11=1, 00 XNOR 0 = 10=1, 11 XNOR 0 = 00=0)

    You may have seen 《A Summer Day's dream》, a doujin Animation of Touhou Project. Things go like the anime, Suika activated her ability, and the feast will loop for infinite times. This really troubles Reimu: to not make her customers feel bored, she must prepare enough numbers of different Nogaku scenes. Reimu find that each time the same guest will seat on the same seat, and She just have to prepare a new scene for a specific mask distribution. Two distribution plans are considered different, if any guest wears different masks.

    In order to save faiths for Shrine, Reimu have to calculate that to make guests not bored, how many different Nogaku scenes does Reimu and Kokoro have to prepare. Due to the number may be too large, Reimu only want to get the answer modules 1e9+71e9+7 . Reimu did never attend Terakoya, so she doesn't know how to calculate in module. So Reimu wishes you to help her figure out the answer, and she promises that after you succeed she will give you a balloon as a gift.

    Input

    First line one number TT , the number of testcases; (T le 20)(T20) .

    Next TT lines each contains two numbers, NN and k(0<N, k le 1e6)k(0<N,k1e6) .

    Output

    For each testcase output one line with a single number of scenes Reimu and Kokoro have to prepare, the answer modules 1e9+71e9+7 .

    样例输入

    2
    3 1
    4 2

    样例输出

    2
    84

    题目来源

    ACM-ICPC 2018 徐州赛区网络预赛

    题意:

    给定n,k

    有n个人围城圆,2^k个面具【从0开始编号】  要求相邻的人拿到的面具编号的同或值不为0 

    问方案数

    思路:

    只要任意一位相同同或值就不为0 即对于一个确定的i j如果是i的反码同或就会是0 只有这一种情况不能取

    所以第一个人有m种情况 之后的n-2个人就有m-1种情况

    到了第n-1个人的时候因为是一个圈 所以第n个人和第1个以及第n-1个都是相邻的

    要分情况讨论

    如果第1个和第n-1个是不同的 那么第n个人就m-2种可能 总的可能是 m*(m-1)^(n - 2) * (m - 2)

    如果第1个和第n-1个是相同的 那么第n个人有m-1种可能 总的可能是 (m - 1) * (前n-2个人的可能)

    两者相加即为答案 

    线性dp即可

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <stdio.h>
     4 #include <vector>
     5 #include <cmath>
     6 #include <cstring>
     7 #include <set>
     8 #include <map>
     9 
    10 using namespace std;
    11 
    12 typedef long long LL;
    13 int t;
    14 LL n, k;
    15 const int maxn = 1e6 + 5;
    16 const int mod = 1e9 + 7;
    17 LL pow2[maxn], dp[maxn];
    18 
    19 LL quick_pow(LL a, LL b)
    20 {
    21     LL res = 1;
    22     while (b) {
    23         if (b & 1) {
    24             res = res * a % mod;
    25         }
    26         a = a * a % mod;
    27         b >>= 1;
    28     }
    29     return res % mod;
    30 }
    31 
    32 void table()
    33 {
    34     for (int i = 0; i < maxn; i++) {
    35         pow2[i] = quick_pow(2, i);
    36     }
    37 }
    38 
    39 int main()
    40 {
    41     //table();
    42     cin >> t;
    43     while (t--) {
    44         memset(dp, 0, sizeof(dp));
    45         scanf("%lld%lld", &n, &k);
    46         LL m = quick_pow(2ll, k);
    47         dp[1] = m % mod;
    48         dp[2] = m * (m - 1) % mod;
    49         for (int i = 3; i <= n; i++) {
    50             dp[i] = (dp[i] + dp[i - 2]) % mod;
    51             dp[i] = (dp[i] + quick_pow(m - 1, i - 2) % mod * m % mod * (m - 2) % mod) % mod;
    52         }
    53         printf("%lld
    ", dp[n]);
    54     }
    55 }
  • 相关阅读:
    JEECG与帆软报表集成
    各种数据库的锁表和解锁操作
    sql server数据库查询超时报错
    java项目部署后的文件路径获取
    js解决跨站点脚本编制问题
    java递归算法实现拼装树形JSON数据
    FreeMarker中的list集合前后台代码
    去除list集合中重复项的几种方法
    Java中Properties类的操作
    mysql 容灾 灾备 备份
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9681363.html
Copyright © 2011-2022 走看看