码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces #307(Div. 2) Problem D. GukiZ and Binary Operations

时间:2015-06-15 10:51:30      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

这个题刚开始是看错题了。。一点思路都没有

到后来把题目看对了以后,有一点思路,但还是不会做。。无奈只能看官方题解

官方题解是这样的:

First convert number k into binary number system. If some bit of k is 0 than the result of or opertion applied for every adjacent pair of those bits in array a must be 0, that is no two adjacent those bits in array a are 1. We should count how many times this is fulfilled. If the values were smaller we could count it with simply dpi = dpi - 1 + dpi - 2, where dpi is equal to number of ways to make array od i bits where no two are adjacent ones. With first values dp1 = 2 and dp2 = 3, we can see that this is ordinary Fibonacci number. We can calculate Fibonacci numbers up to 1018 easily by fast matrix multiplication. If some bit at k is 1 than number of ways is 2n — \t{(number of ways bit is 0)}, which is also easy to calculate. We must be cearful for cases when 2l smaller than k (solution is 0 then) and when l = 63 or l = 64. Total complexity is 技术分享.

看了以后大概懂了他的思路。将每一二进制位看成一步,则根据乘法原理,总方案数=每一位的方案数的积。

如果k的某一位二进制值为零,则发现每个相邻元素不能均为1.然后dp即可,是斐波那契数列,矩阵乘法O(log n) 无压力

如果k的某一位二进制值为1,则必然存在两个相邻元素的值为1,然后发现f[n]=2^(n-1)+f[n-1] 答案为2*f[n-1],然而这个式子让人很不爽。考虑到这是计数类问题,从反面考虑,则方案为2^n-dp[n]( dp[n]是指某一二进制位为零时其取值 ),然后就爽了。。

Codeforces #307(Div. 2) Problem D. GukiZ and Binary Operations

标签:

原文地址:http://www.cnblogs.com/tsingyawn/p/4576322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!