Adjacent Bit Counts HDU - 3284 [DP]

<marquee width="600"> Fighting!~~ </marquee>

Adjacent Bit Counts

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 497    Accepted Submission(s): 414


Problem Description
For a string of n bits x 1, x 2, x 3, …, x n, the adjacent bit count of the string ( AdjBC(x)) is given by

x1*x2 + x2*x3 + x3*x4 + … + xn-1*xn

which counts the number of times a 1 bit is adjacent to another 1 bit. For example:

AdjBC(011101101) = 3
AdjBC(111101101) = 4
AdjBC(010101010) = 0

Write a program which takes as input integers n and k and returns the number of bit strings x of n bits (out of 2 n) that satisfy AdjBC(x) = k. For example, for 5 bit strings, there are 6 ways of getting
AdjBC(x) = 2:

11100, 01110, 00111, 10111, 11101, 11011
 

Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set is a single line that contains the data set number, followed by a space, followed by a decimal integer giving the number (n) of bits in the bit strings, followed by a single space, followed by a decimal integer (k) giving the desired adjacent bit count. The number of bits (n) will not be greater than 100 and the parameters n and k will be chosen so that the result will fit in a signed 32-bit integer.
 

Output
For each data set there is one line of output. It contains the data set number followed by a single space, followed by the number of n-bit strings with adjacent bit count equal to k.
 

Sample Input
101 5 22 20 83 30 174 40 245 50 376 60 527 70 598 80 739 90 8410 100 90
 
Sample Output
1 62 634263 18612254 1682125015 448747646 1609167 229373088 991679 1547610 23076518
 

Source
 

Recommend
zhuweicong


题意:ans=sigma (x[i]*x[i-1]) 其中xi只能为0或者1,问长度为n,ans==k有多少种情况?

思路:
dp[n][k][num]代表以num为结尾,长度为n,和为k的方案数.那么很容易有如下状态转移方程
dp[n][k][1]=dp[n-1][k][0]+dp[n-1][k-1][1];
dp[n][k][0]=dp[n-1][k][1]+dp[n-1][k][0];

#include <stdio.h>
#include <iostream>
#include <string.h>
typedef long long ll;

using namespace std;

const int MAX_N=1e6+6;
int dp[105][105][2];

void init(){
    ///求解dp[2][1~105][1]//
    dp[2][1][1]=1,dp[2][0][1]=1;
    ///求解dp[2][1~105][0]//
    dp[2][1][0]=0,dp[2][0][0]=2;
    ///求解dp[2~105][0][1] 求解dp[2~105][0][0]////  这两个值互相联系,要一起求
    for(int i=3;i<=104;i++) dp[i][0][1]=dp[i-1][0][0],dp[i][0][0]=dp[i-1][0][0]+dp[i-1][0][1];
    for(int n=3;n<=104;n++){
        for(int k=1;k<=104;k++){
            dp[n][k][1]=dp[n-1][k][0]+dp[n-1][k-1][1];
            dp[n][k][0]=dp[n-1][k][1]+dp[n-1][k][0];
        }
    }
}

int main(void){
   init();
   int T;
   cin >>T;
   while(T--){
        int num,x,y;
        scanf("%d%d%d",&num,&x,&y);
        printf("%d %d\n",num,dp[x][y][1]+dp[x][y][0]);
   }
}


全部评论

相关推荐

点赞 评论 收藏
分享
06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务