博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Google Code Jam 2014 Round 1B Problem B
阅读量:7280 次
发布时间:2019-06-30

本文共 1869 字,大约阅读时间需要 6 分钟。

二进制数位DP,涉及到数字的按位与操作。

#include 
#include
#include
#include
using namespace std;#define MAX_LEN 50long long A, B, K;int a[MAX_LEN], b[MAX_LEN], k[MAX_LEN];long long memoize[MAX_LEN][2][2][2];void input(){ scanf("%lld%lld%lld", &A, &B, &K);}int convert(long long A, int a[]){ int i = 0; while (A) { a[i] = A & 1; A >>= 1; i++; } return i;}long long dfs(int current_bit, bool less_a, bool less_b, bool less_k){ if (current_bit == -1) { if (less_a && less_b && less_k) { return 1; } return 0; } if (memoize[current_bit][less_a][less_b][less_k] != -1) return memoize[current_bit][less_a][less_b][less_k]; bool one_a = less_a || a[current_bit] == 1; bool one_b = less_b || b[current_bit] == 1; bool one_k = less_k || k[current_bit] == 1; // a0 b0 long long ret = dfs(current_bit - 1, one_a, one_b, one_k); // a1 b0 if (one_a) { ret += dfs(current_bit - 1, less_a, one_b, one_k); } // a0 b1 if (one_b) { ret += dfs(current_bit - 1, one_a, less_b, one_k); } // a1 b1 if (one_a && one_b && one_k) { ret += dfs(current_bit - 1, less_a, less_b, less_k); } return memoize[current_bit][less_a][less_b][less_k] = ret;}int main(){ int t; scanf("%d", &t); for (int i = 0; i < t; i++) { printf("Case #%d: ", i + 1); input(); memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(k, 0, sizeof(k)); convert(A, a); convert(B, b); convert(K, k); memset(memoize, -1, sizeof(memoize)); long long ans = dfs(31, false, false, false); printf("%lld\n", ans); } return 0;}
View Code

 

转载于:https://www.cnblogs.com/rainydays/p/4260803.html

你可能感兴趣的文章
【ZOJ 1221】Risk
查看>>
【笔记】一些linux实用函数技巧【原创】
查看>>
数据结构图文解析之:二分查找及与其相关的几个问题解析
查看>>
arcgis for javascript之ArcGISDynamicMapServiceLayer图层控制的实现
查看>>
php框架的制作原理
查看>>
【Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之四】使用绑定C++至Lua的自己定义类...
查看>>
python中使用mahotas包实现高斯模糊
查看>>
【SVN Working copy is too old (format 10, created by Subversion 1.6)】解决方式
查看>>
ros与下位机通信常用的c++ boost串口应用--22
查看>>
Codeforces Beta Round #6 (Div. 2 Only) D. Lizards and Basements 2 dfs
查看>>
adbWireless 简单教程
查看>>
Hadoop Version History and Feature
查看>>
html5手机网站需要加的那些meta/link标签,html5 meta全解
查看>>
Codeforces Beta Round #9 (Div. 2 Only) B. Running Student 水题
查看>>
Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)
查看>>
PHPer书单
查看>>
沉浸式导航栏
查看>>
Python中docstring文档的写法
查看>>
SSH配置文件和SSM配置文件的写法
查看>>
获取图片中感兴趣区域的信息(Matlab实现)
查看>>