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

  • 题目列表
  • 排名:-
  • 问答
  • A. Hard to prepare

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 2k2^k2k masks numbered from 0,1,⋯,0,1,\cdots,0,1,⋯, 2k−12^k - 12k−1, 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 iii and jjj , then iii XNOR jjj must be positive. (two guests can wear the same mask). XNOR means ~(iii^jjj) and every number has kkk bits. (111 XNOR 1=11 = 11=1, 000 XNOR 0=10 = 10=1, 111 XNOR 0=00 = 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+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 TTT , the number of testcases; (T≤20)(T \le 20)(T≤20) .

Next TTT lines each contains two numbers, NNN and k(0<N,k≤1e6)k(0<N, k \le 1e6)k(0<N,k≤1e6) .

Output

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

样例输入

2
3 1
4 2

样例输出

2
84

题目来源

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

题目的意思是,这里有2的k次方个数值,每一个数字的长度是k,有n个人围成一个圆圈,并且相邻的两个人的数字编号不能同或的0(同或与亦或相反)

101

010

异或值    111

同或值    000

我们如果想要相邻的两个数字同或值不是零的话,只要有一位是相同的就不是零,所以如果有k位的话,那么它的相邻处有2的k次方-1种方案,如果是长度为n的一个圈的话我们很容易想到一共有2^k * (2^k-1)^(n-2) * (2^k - 2) ;我们看到这里分成了三部分第一部分是第一个位置放有2^k种方案,从第二个位置开始到第n-1个位置都有2^k-1种方案,就剩下最后一个位置它的两边都有人所以会有(2^k - 2)种方案这就是上面的式子由来,但是这里我们会发现我们漏掉很多种方案,因为第n-1个位置的人的数字是可能和第一个人的数字是相同的,那么我们公式的第三部分有时候应该不是这样的,我们应该讨论一下就是第1个和第n-1个位置的数字相同和不同时的情况,但是我们又会发现我们要求算1和第n-1个不同又会出现第n-2个位置情况会影响第n-1的数目;

#include <bits/stdc++.h>
using namespace std;

typedef long long ll ;
const int inf = 0x3f3f3f3f;
const int mod =  1e9+7;
const int Max = 1e6+10;
ll n,m,w[Max];

#define rep(i,s,n) for(ll i=s;i<=n;i++)
#define per(i,n,s) for(ll i=n;i>=s;i--)
ll pow(ll base , ll k){
   ll res=(ll)1;
   while(k){
    if(k&1)  res=res*base;
    res=(res%mod+mod)%mod;
    base=base*base;
    base=(base%mod+mod)%mod;
    k>>=1;
   }
   return res;
}
ll de(ll n, ll m){
     ll sum;
     if(n==1) return w[m];

if(n==2) return (w[m]%mod*(w[m]-1)%mod+mod)%mod;

ll sum1=(w[m]%mod*pow((w[m]-1),(ll)(n-2))%mod*(ll)max((w[m]-2),(ll)0))%mod;
     ll sum2=de(n-2,m)%mod;
     sum=(sum1+sum2)%mod;
     return (sum%mod+mod)%mod;
}
int main(){
   int t;
   w[0]=1;
   rep(i,1,Max-10){
     w[i]=w[i-1]*2;
     w[i]=(w[i]%mod+mod)%mod;
   }
   scanf("%d",&t);
   while(t--){
    scanf("%lld %lld",&n,&m);
    ll ans=de(n,m);
    printf("%lld\n",ans);
   }
   return 0;
}

28 问题分解(递归)相关推荐

  1. 循环赛日程表非递归Java_王晓东《算法设计与分析》课件.ppt

    <王晓东<算法设计与分析>课件.ppt>由会员分享,可在线阅读,更多相关<王晓东<算法设计与分析>课件.ppt(356页珍藏版)>请在人人文库网上搜索. ...

  2. 二叉树的前中后序递归和非递归遍历操作【代码】

    "遍历"是二叉树各种操作的基础,可以在遍历过程中对节点进行各种操作,如:求节点的双亲,求节点的孩子,判断节点的层次.当然,还有一些更重要的操作,例如,依据遍历序列建立二叉树,对建立 ...

  3. java 20 -3 递归之删除特定目录下的特定文件

    1 /* 2 需求:删除H:\demo目录下的带内容的文件 3 分析: 4 A:封装该目录 5 B:获取该目录下所有的文件或文件夹的File数组 6 C:遍历该File数组,获取每一个File对象 7 ...

  4. 递归Java_递归的Java实现

    递归是一种应用非常广泛的算法(或者编程技巧).递归求解问题的分解过程,去的过程叫"递",回来的过程叫"归". 递归需要满足的三个条件:1. 一个问题的解可以分解 ...

  5. 学习笔记9-C语言-传参、类型限定符、递归

    函数传参: 1.形参变量属于它所在的函数,出了该函数就不能使用 2.实参与形参之间都是以赋值的形式进行数据传递(值传递) 3.return 其实是把返回值数据放置到一个公共的区域(函数和函数调用者), ...

  6. ++递归 字符串全排列_超全递归技巧整理,这次一起拿下递归

    0. 前言 大家好,我是多选参数的程序锅,一个正在 neng 操作系统.学数据结构和算法以及 Java 的硬核菜鸡.本篇将主要介绍递归相关的内容,下面是本篇的内容提纲. 1. 递归基础 ★ 争哥:从我 ...

  7. 信息学奥赛一本通 2032:【例4.18】分解质因数

    [题目链接] ybt 2032:[例4.18]分解质因数 [题目考点] 1. 质数 [解题思路] 解法1:使用循环 每次循环中,遍历2到n,找到一个n的质因数,输出,而后n除以该因数,继续循环. 解法 ...

  8. 递归总结 By greenhand

    View Code  1 namespace  递归的理解  2 {  3     class Program  4     {  5         /// <summary>  6   ...

  9. 九章基础算法03:树和递归

    目录 1. 树的概念 1.1 树的结构 1.2 二叉树结构 2. 树的遍历 2.1 概述 2.2 先序遍历(preorder traversal) 2.3 中序遍历(inorder traversal ...

最新文章

  1. Jmeter性能测试 入门
  2. Firefox浏览器的安装
  3. 【vuejs小项目】一、脚手架搭建工作
  4. 数学怪兽-法国数学家庞加莱
  5. 163相册密码破解秘诀
  6. Linq------错误: Unable to determine the principal end of an association between the types
  7. python棋盘放麦粒求和递归_Python递归调用实现数字累加的代码
  8. 【干货分享】电信云/边缘云虚拟层软件StarlingX介绍
  9. php链接mysql数据库 p_php连接mysql数据库的几种方法
  10. AS3中常用到的公式
  11. 【C语言】剖析函数递归(2)
  12. 编程的智慧 强烈推荐
  13. python——基础题
  14. 服务器操作系统不能显示全屏,服务器窗口显示不全屏
  15. 零信任的三种主流技术【远航】
  16. 毕业论文word排版技巧
  17. 【Java+MySQL】使用JDBC连接MySQL 8.0数据库
  18. Android护眼模式功能小记
  19. ubuntu 编译安装opencv官网教程
  20. Android 系统稳定性 - ANR 解析

热门文章

  1. jmfapplet做一个简单的播放器
  2. 5.2_1 北师大应用心理(MAP)347心理学专业综合 持续更新
  3. 《信息用户与服务》第四版目录 胡昌平、胡潜、邓胜利著
  4. 被领导夸的时候,怎么回应?
  5. 优酷网和56网获取网络流播放地址url的方法
  6. mysql 自增字段、属性
  7. 【BUUCTF】web 之 [护网杯 2018]easy_tornado
  8. 性状的pearson相关与表型相关的差异
  9. 安装篇——用halyard安装Spinnaker
  10. 碰到边缘就反弹的金币收集游戏,译者:lixingqiu