HDU 4588 Count The Carries 数位DP || 打表找规律
2013年南京邀请赛的铜牌题。。。做的非常是伤心。另外有两个不太好想到的地方。。
。。a 能够等于零,另外a到b的累加和比較大。大约在2^70左右。
首先说一下解题思路。
首先统计出每一位的1的个数,然后统一进位。
设最低位为1。次低位为2,依次类推,ans[]表示这一位上有多少个1。那么有
sum += ans[i]/2,ans[i+1] += ans[i]/2;
sum即为答案。
好了,如今问题转化成怎么求ans[]了。
打表查规律比較奇妙,上图不说话。
打表的代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 9999991
#define lowbit(x) (x&(-x)) using namespace std; void out(int x)
{
while(x)
{
printf("%d",x&1);
x >>= 1;
}
} int main()
{
for(int i = 1;i <= 100; ++i)
{
printf("i = %3d : ",i);
out(i);
puts("");
} return 0;
}
好了。重头戏来了。这个数位DP做的还是非常有成就感的。
记忆化搜索 + bfs推送
昨天事实上想这个思路了。
。
。
只是当时比較着急没写出来,心态是个非常重要的东西。
记忆化搜索部分dp[sta][site][dig] 第一维表示是否到达上限,site表示第几位,dig表示是1还是0。
那么site == 1时,也就是说最高位记录的信息是正确的。可是其它的就不是想要的了。
由于dp[sta][site][sta] 表示当前位到最低位的方案数。
所以说要把最高位的答案推送下去。
推送非常easy,每种状态最多仅仅会由两种子状态得到,所以按比例推送一下即可了。
详见代码。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 9999991
#define lowbit(x) (x&(-x)) using namespace std; _LL dp[2][105][2],dp1[2][105][2]; _LL up[105]; _LL ansa[105],ansb[105]; _LL dfs(_LL sta,_LL site,_LL dig)
{
if(dp[sta][site][dig] != -1)
return dp[sta][site][dig];
if(site == 100)
{
dp[sta][site][dig] = 1;
return 1;
} dp[sta][site][dig] = 0; if(sta == 0)
{
dp[sta][site][dig] = dfs(0,site+1,0) + dfs(0,site+1,1);
}
else
{
if(up[site+1] == 1)
{
dp[sta][site][dig] = dfs(1,site+1,1) + dfs(0,site+1,0);
}
else
{
dp[sta][site][dig] = dfs(1,site+1,0);
}
}
return dp[sta][site][dig];
} void Cal(_LL x,_LL *ans)
{
_LL temp = x,len = 100; memset(ans,0,sizeof(_LL)*(102)); if(x == 0)
return ;
while(temp)
{
up[len--] = temp%2;
temp /= 2;
} len++;
memset(dp,-1,sizeof(dp));
dfs(1,len,1);
dfs(0,len,0);
_LL i; memset(dp1,0,sizeof(dp1)); dp1[1][len][1] = max((_LL)0,dp[1][len][1]);
dp1[1][len][0] = max((_LL)0,dp[1][len][0]);
dp1[0][len][1] = max((_LL)0,dp[0][len][1]);
dp1[0][len][0] = max((_LL)0,dp[0][len][0]); for(_LL site = len;site <= 99; ++site)
{
dp1[0][site+1][1] += dp1[0][site][1]*dp[0][site+1][1]/(dp[0][site+1][1] + dp[0][site+1][0]);
dp1[0][site+1][1] += dp1[0][site][0]*dp[0][site+1][1]/(dp[0][site+1][1] + dp[0][site+1][0]);
dp1[0][site+1][0] += dp1[0][site][1]*dp[0][site+1][0]/(dp[0][site+1][1] + dp[0][site+1][0]);
dp1[0][site+1][0] += dp1[0][site][0]*dp[0][site+1][0]/(dp[0][site+1][1] + dp[0][site+1][0]); if(up[site+1] == 1)
{
if(up[site] == 1)
{
dp1[1][site+1][1] += dp1[1][site][1]*dp[1][site+1][1]/(dp[1][site+1][1] + dp[0][site+1][0]);
dp1[0][site+1][0] += dp1[1][site][1]*dp[0][site+1][0]/(dp[1][site+1][1] + dp[0][site+1][0]);
}
else
{
dp1[1][site+1][1] += dp1[1][site][0]*dp[1][site+1][1]/(dp[1][site+1][1] + dp[0][site+1][0]);
dp1[0][site+1][0] += dp1[1][site][0]*dp[0][site+1][0]/(dp[1][site+1][1] + dp[0][site+1][0]);
}
}
else
{
dp1[1][site+1][0] = dp1[1][site][0] + dp1[1][site][1];
}
} for(i = len;i <= 100; ++i)
{
ans[i] = max((_LL)0,dp1[1][i][1]) + max((_LL)0,dp1[0][i][1]);
}
} int main()
{
_LL a,b,i; while(scanf("%I64d %I64d",&a,&b) != EOF)
{
Cal(b,ansb);
Cal(max((_LL)0,a-1),ansa); _LL sum = 0; for(i = 100;i >= 1; --i)
{
sum += (ansb[i]-ansa[i])/2;
ansb[i-1] += (ansb[i]-ansa[i])/2;
}
printf("%I64d\n",sum);
} return 0;
}
HDU 4588 Count The Carries 数位DP || 打表找规律的更多相关文章
- HDU 5753 Permutation Bo (推导 or 打表找规律)
Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...
- HDU 5795 A Simple Nim(SG打表找规律)
SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...
- HDU 4588 Count The Carries 数学
Count The CarriesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- HDU 4588 Count The Carries 计算二进制进位总数
点击打开链接 Count The Carries Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU 5795 A Simple Nim (博弈 打表找规律)
A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...
- HDU 4588 Count The Carries(数学统计)
Description One day, Implus gets interested in binary addition and binary carry. He will transfer al ...
- HDU 4588 Count The Carries(找规律,模拟)
题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #includ ...
- HDU 4588 Count The Carries (数学,计数)
题意:给定两个十进制数,求二进制中,从x加到y的二进制进了多少位. 析:把这些数字的二进制纵向罗列出来,然后一位一位的把和加起来,最终得到总的进位数.从1到x,第i位上1的总数是x左移i+1位再右移i ...
- hdu 4588 Count The Carries
思路:容易发现二进制表示的数的最低位规律是01010101……:接着是001100110011……:接着是:0000111100001111…… 这样我们发现每一位的循环节是2^(i+1),前2^i是 ...
随机推荐
- 覆盖率測试工具gcov的前端工具_LCOV_简单介绍
1.Gcov是进行代码运行的覆盖率统计的工具.它随着gcc的公布一起公布的,它的使用也非常easy,须要在编译和链接的时候加上-fprofile-arcs -ftest-coverage生成二进制文件 ...
- Func委托和Action委托
http://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate The difference between Func a ...
- Laravel-错误调试与记录日志
Laravel-错误调试与记录日志 标签(空格分隔): php 错误调试 配置 修改/config/app.php 'debug' => env('APP_DEBUG', true), 开启de ...
- SVN在vs2013中使用
http://download.csdn.net/download/show_594/9112963 内包含VisualSVN 5.0.1的官方原版安装包及破解文件VisualSVN.Core.L.d ...
- 虚拟机下安装mysql
虚拟机下CentOS6.8下安装MYSQL5.6 方法: 整理修改于 http://www.cnblogs.com/liuyi2614/p/6382183.html 开始时: 普通用户是$ root用 ...
- spinlock参考资料
spinlock:http://irl.cs.ucla.edu/~yingdi/web/paperreading/smp_locking.pdf
- HDU 3342 Legal or Not(判断环)
Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...
- mutt发邮件
在 /etc/Muttrc 文件添加以下内容: set from="laughingliang@chaincar.com" set use_from=yes set envel ...
- ML words
samples:样本 multi-dimensional entry / multivariate data:多属性记录 features:特征,属性 supervised learning:监督学习 ...
- javascript--记忆函数
function memory(val) { if(!memory.cached) {//判断是否创建了缓存 memory.cached = {}; } if(memory.cached[val] ! ...