Malek Dance Club(递推)
1 second
256 megabytes
standard input
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 ;
}
Malek Dance Club(递推)的更多相关文章
- Codeforces H. Malek Dance Club(找规律)
题目描述: Malek Dance Club time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 【BZOJ-2476】战场的数目 矩阵乘法 + 递推
2476: 战场的数目 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 58 Solved: 38[Submit][Status][Discuss] D ...
- 从一道NOI练习题说递推和递归
一.递推: 所谓递推,简单理解就是推导数列的通项公式.先举一个简单的例子(另一个NOI练习题,但不是这次要解的问题): 楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可 ...
- Flags-Ural1225简单递推
Time limit: 1.0 second Memory limit: 64 MB On the Day of the Flag of Russia a shop-owner decided to ...
- 利用Cayley-Hamilton theorem 优化矩阵线性递推
平时有关线性递推的题,很多都可以利用矩阵乘法来解决. 时间复杂度一般是O(K3logn)因此对矩阵的规模限制比较大. 下面介绍一种利用利用Cayley-Hamilton theorem加速矩阵乘法的方 ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- 简单递推 HDU-2108
要成为一个ACMer,就是要不断学习,不断刷题...最近写了一些递推,发现递推规律还是挺明显的,最简单的斐波那契函数(爬楼梯问题),这个大家应该都会,看一点稍微进阶了一点的,不是简单的v[i] = v ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
随机推荐
- C语言实现二叉树-01版
故事是这样开始的,项目经理有一天终于还是拍拍我肩膀说: 无论你的链表写得多么的好,无论是多么的灵活,我也得费老半天才查找到想要的数据: 这让我的工作非常苦恼,听说有一种叫做二叉树的数据结构,你看能不能 ...
- Atitit. 单点登录sso 的解决方案 总结
Atitit. 单点登录sso 的解决方案 总结 1. 系统应用场景and SSO模式选型 2 2. 系统应用的原则与要求 2 2.1. 开发快速简单::绝大部分系统来说,开发快速简单为主 2 2. ...
- atitit.无线网卡 不能搜索到WiFi 无线路由信号的解决不能上网
atitit.无线网卡 不能搜索到WiFi 无线路由信号的解决不能上网 #---现象 pc机无线网卡不能搜索到无线路由信号.. 但是笔记本和手机是可以的... 不过pc机无线网卡能搜索到别的路由的信号 ...
- javaweb学习总结(四)——Http协议
一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...
- Leetcode 231 Power of Two 数论
同样是判断数是否是2的n次幂,同 Power of three class Solution { public: bool isPowerOfTwo(int n) { ) && ((( ...
- Hello.class所在路径下, 输入命令:java Hello.class,会出现什么结果,为什么?
所在路径下, 输入命令:java Hello.class: 因为DOS没有规定路径,所有么有在默认路径下找到Hello.class文件,导致提示 错误: 找不到或无法加载主类 Hello.class.
- 通过weburl 启动windows程序
1. 注册表修改 建立一个reg文件 执行导入 以RunLocal协议为例子 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\RunL ...
- Entity Framework6 with Oracle
Entity Framework6 with Oracle(可实现code first) Oracle 与2个月前刚提供对EF6的支持.以前只支持到EF5.EF6有很多有用的功能 值得升级.这里介绍下 ...
- centos 6.4 /var/log/secure 不记录日志的問題
先确保日志服务开启:不妨重启下日志服务:由于目前RHEL 6/centos 6已经使用rsyslog替换了syslog.,所以不要在找/etc/syslog.conf了:重启命令:/etc/init. ...
- RMAN duplciate 准备时,需要检查的target数据库参数内容
SQL> show parameter audit_file_dest; NAME TYPE VALUE------ ...