Malek Dance Club
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidentally Natalia Fan Club also has 2nmembers. Each member of MDC is assigned a unique id i from 0 to 2n - 1. The same holds for each member of NFC.

One of the parts of this tradition is one by one dance, where each member of MDC dances with a member of NFC. A dance pair is a pair of numbers (a, b) such that member a from MDC dances with member b from NFC.

The complexity of a pairs' assignment is the number of pairs of dancing pairs (a, b) and (c, d) such that a < c andb > d.

You are given a binary number of length n named x. We know that member i from MDC dances with member  from NFC. Your task is to calculate the complexity of this assignment modulo 1000000007 (109 + 7).

Expression  denotes applying «XOR» to numbers x and y. This operation exists in all modern programming languages, for example, in C++ and Java it denotes as «^», in Pascal — «xor».

注意到n很小,如果能够求出递推公式,问题将很容易得到解决。

x长度为n,f(x)表示complexity,显然f(0)=0,f(1)=1。当n>1时,f(0x)和f(1x)可由f(x)推出。

(1)求f(0x): i 分别取 0,1,...,2^n-1,j分别取2^n,...,2^(n+1) - 1, 统计(i,i  xor 0x)与(j, j xor 0x)能组成多少对,注意j xor 0x的第一位是1,而i xor 0x的第一位是0,故而 j xor 0x > i  xor 0x,而 j > i,故(i,i  xor 0x)与(j, j xor 0x)不能配对。统计(j, j xor 0x)内部能组成多少对,所有j的第一位相同, 导致j xor 0x的第一位都相同,故而j的第一位是没有比较意义的,去掉没有影响,故(j, j xor 0x)的配对数为f(x)。所以f(0x)=2f(x)

(2)求f(1x):  i 分别取 0,1,...,2^n-1,j分别取2^n,...,2^(n+1) - 1, 统计(i,i  xor 1x)与(j, j xor 1x)能组成多少对,注意j xor 1x的第一位是0,而i xor 1x的第一位是1,故而 i  xor 1x > j xor 1x ,而 i < j,故(i,i  xor 1x)与(j, j xor 1x)之间能产生2^(2n)对。统计(j, j xor 0x)内部能组成多少对,所有j的第一位都相同, 导致j xor 1x的第一位都相同,故而j的第一位是没有比较意义的,去掉没有影响,故(j, j xor 0x)的配对数为f(x)。所以f(1x)=2f(x)+2^(2n)

综上:

  • f(0x) = 2f(x)
  • f(1x) = 2f(x) + 2^2n
 #include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std; char x[];
const int m = ;
int n; long long POW(long long a, long long b)
{
if(!b) return ;
long long c = POW(a, b>>);
c = (c * c) % m;
if(b & )
{
c = (c * a) % m;
}
return c;
} int f(int k)
{
if(k == n - )
{
if(x[k] == '') return ;
else return ;
}
if(x[k] == '') return ( * f(k + )) % m;
else return (( * f(k + )) % m + POW(, n - k - )) % m;
} int main()
{
while(scanf("%s", x) != EOF)
{
n = strlen(x);
printf("%d\n", f());
}
return ;
}

最新文章

  1. 北京培训记day2
  2. map的用法
  3. ZPL打印中文信息
  4. 程序猿每个VPN真卡手
  5. Calibrating delay loop... 问题以及解决方法(RealARM开发板)
  6. FZU 2221 RunningMan(跑男)
  7. win7笔记本无线连上无法上网
  8. sql 将Null 值转化成空字符串
  9. 重新认识Swift中的可选型(Swift2.1)
  10. leetcode_question_119 Pascal&#39;s Triangle II
  11. Thrift项目Server端开发流程
  12. iOS9以后 GDataXMLNode修改方式
  13. [SDOI 2009]HH去散步
  14. Android的内存分配与回收
  15. scrapy设置代理的方法
  16. &lt;4&gt;Python切片功能剖析
  17. KMSpico 无后门下载
  18. 用SoapUI 测试Web Service
  19. Linux学习笔记14—文件的压缩与打包
  20. 将Web项目War包部署到Tomcat服务器

热门文章

  1. 【原】《Git教程》学习笔记
  2. CentOS 6.5 无网环境安装R及Rstudio的方法的方法
  3. 从混战到三足鼎立,外卖O2O下一个谁先出局?
  4. 公钥,私钥,SSL(讲的很生动) (转) 对称加密、非对称加密初探
  5. FreeCodeCamp 中级算法(个人向)
  6. aehyok.com的成长之路三——框架结构
  7. GO語言基礎教程:數據類型,變量,常量
  8. 通过修改host文件来允许和禁止主机的访问
  9. React Native入门遇到的一些问题
  10. 江豚科技|专业移动APP开发与移动互联网解决方案