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 || 打表找规律的更多相关文章

  1. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  2. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...

  3. HDU 4588 Count The Carries 数学

    Count The CarriesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  4. HDU 4588 Count The Carries 计算二进制进位总数

    点击打开链接 Count The Carries Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  5. HDU 5795 A Simple Nim (博弈 打表找规律)

    A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...

  6. HDU 4588 Count The Carries(数学统计)

    Description One day, Implus gets interested in binary addition and binary carry. He will transfer al ...

  7. HDU 4588 Count The Carries(找规律,模拟)

    题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #includ ...

  8. HDU 4588 Count The Carries (数学,计数)

    题意:给定两个十进制数,求二进制中,从x加到y的二进制进了多少位. 析:把这些数字的二进制纵向罗列出来,然后一位一位的把和加起来,最终得到总的进位数.从1到x,第i位上1的总数是x左移i+1位再右移i ...

  9. hdu 4588 Count The Carries

    思路:容易发现二进制表示的数的最低位规律是01010101……:接着是001100110011……:接着是:0000111100001111…… 这样我们发现每一位的循环节是2^(i+1),前2^i是 ...

随机推荐

  1. TCO14 1B L3: EagleInZoo, dp,tree

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=13117&rd=15950 看到树,又是与节点有关,通常是d ...

  2. 9-第一个app项目

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  3. Asp.Net 中使用 水晶报表(上)

    Asp.Net中使用水晶报表(上) 在我们对VS.Net中的水晶报表(Crystal Reports)进行研究之前,我和我朋友对如何将这个复杂的东东加入我们的Web应用有着非常的好奇心.一周以后,在阅 ...

  4. Linux环境下源码安装PostgreSQL

    1.下载PostgreSQL源码包,并保存到Linux操作系统的一个目录下 2.解压PostgreSQL源码包 :tar zxvf postgresql-9.2.4.tar.gz 或 tar jxvf ...

  5. 安装Debugging Tools时出现错误Setup could not find the file WinSDK_amd64的处理

    安装Debugging Tools时出现错误Setup could not find the file WinSDK_amd64的处理 1.软件来源:  微软官网下载SDK ISO安装包(含有debu ...

  6. vue的webpack打包

    一个完整的项目离不开 开发环境 生产环境 测试环境 这三个环境 首先解释一下这三个环境的含义 开发环境:开发环境是程序猿们专门用于开发的服务器,配置可以比较随意,为了开发调试方便,一般打开全部错误报告 ...

  7. letCode(771 Jewels and Stones )

    问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...

  8. 常用模块re模块(正则表达式)

    re模块 一:什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中, ...

  9. (转载)详细图解mongodb下载、安装、配置与使用

    记得在管理员模式下运行CMD,否则服务将启动失败 转载:http://blog.csdn.net/boby16/article/details/51221474 详细图解,记录 win7 64 安装m ...

  10. jumpserver 安装python 报错

    环境centos7.5 pip3 insatll ./python-gssapi-0.6.4.tar.gz  报错 Command "python setup.py egg_info&quo ...