hdu4588Count The Carries
去年南京邀请赛的水题,当时找规律过的,看它长得很像数位dp,试了试用数位dp能不能过,d出每位上有多少个1,然后TLE了。。然后用规律优化了前4位,勉强过了。
附数位dp代码及找规律代码。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define N 1000000
#define LL __int64
int dp[][][];
int di[],o[],oo[];
int s[N][],tg,pp[];
bool f[N];
int dfs(int i,int e,bool z,int k)
{
if(i<) return z;
if(!e&&dp[i][k][z]!=-) return dp[i][k][z];
int mk = e?di[i]:;
int ans = ;
for(int j = ; j <= mk ; j++)
{
int ct = dfs(i-,e&&(j==mk),(z|((i==k)&&j)),k);
ans+=ct;
}
return e?ans:dp[i][k][z] = ans;
}
int cc(int x,int k)
{
int p1 = (x-pp[k]+)/pp[k+];
int qq = p1*pp[k];
if((x-pp[k]+)%pp[k+]>pp[k]) qq += pp[k];
else qq+=((x-pp[k]+)%pp[k+]);
return qq;
}
//void cal(int x,int y)
//{
// int g = 0,i;
// int tx = x;
// memset(o,0,sizeof(o));
// if(x<=0)
// o[0] = o[1] = 0;
// else{
// o[0] = (x+1)/2;
// o[1] = cc(x,1);
// if(x>=3)
// o[2] = cc(x,2);
// if(x>=7)
// o[3] = cc(x,3);
// }
// while(x>0)
// {
// di[g++] = x%2;
// x/=2;
// }
// for(i = 4 ; i <= g-1 ; i++)
// {
// o[i] = dfs(g-1,1,0,i);
// }
// int t = 0;
// LL ans = 0;
// if(y<=0)
// oo[0] = oo[1] = o[2] = o[3] = 0;
// else{
// oo[0] = ((y+1)/2);
// oo[1] = cc(y,1);
// if(y>=3)
// oo[2] = cc(y,2);
// if(y>=7)
// oo[3] = cc(y,3);
// }
// g = 0;
// while(y)
// {
// di[g++] = y%2;
// y/=2;
// }
// for(i = 4 ;i <= g-1 ; i++)
// {
// oo[i] = dfs(g-1,1,0,i);
// }
// for(i = 0 ; i <= g-1; i++)
// {
// t = (t+oo[i]-o[i])/2;
// ans+=t;
// }
// while(t)
// {
// t = (t)/2;
// ans+=t;
// }
// printf("%I64d\n",ans);
//}
LL cal(int a,int b)
{
LL ans = ;
int t = ,s1,s2;
if(a>=) s1 = (a+)/;
if(b>=) s2 = (b+)/;
t = (s2-s1)/;
ans+=t;
for(int i = ;i < ;i++)
{
if(a>=pp[i]) s1 = cc(a,i);
else s1=;
if(b>=pp[i]) s2 = cc(b,i);
else s2=;
t = (t+s2-s1)/;
ans+=t;
}
while(t)
{
t/=;
ans+=t;
}
return ans;
}
int main()
{
int a,b;
pp[] = ;
for(int i = ; i < ; i++)
pp[i] = pp[i-]*;
memset(dp,-,sizeof(dp));
int t = ;
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==b)
printf("0\n");
else
printf("%I64d\n",cal(a-,b));
}
return ;
}
hdu4588Count The Carries的更多相关文章
- ACM:SCU 4437 Carries - 水题
SCU 4437 Carries Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice ...
- 2015弱校联盟(1) - B. Carries
B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...
- 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 ...
- Carries SCU - 4437
Carries frog has nn integers a1,a2,-,ana1,a2,-,an, and she wants to add them pairwise. Unfortunately ...
- 二分 + 模拟 - Carries
Carries Problem's Link Mean: 给你n个数,让你计算这n个数两两组合相加的和进位的次数. analyse: 脑洞题. 首先要知道:对于两个数的第k位相加会进位的条件是:a%( ...
- HDU 4588 Count The Carries(数学统计)
Description One day, Implus gets interested in binary addition and binary carry. He will transfer al ...
- Carries
Carries frog has nn integers a1,a2,…,ana1,a2,…,an, and she wants to add them pairwise. Unfortunately ...
- HDU 4588 Count The Carries(找规律,模拟)
题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #includ ...
随机推荐
- CGRectGet系列
CGRectGetHeight返回label本身的高度 CGRectGetMinY返回label顶部的坐标 CGRectGetMaxY 返回label底部的坐标 CGRectGetMinX 返回lab ...
- discuz模板文件列表
template/default/common模板公共文件夹,全局相关 |--block_forumtree.htm 树形论坛版块分支js文件 |--block_thread.htm特 ...
- html+css复习之第1篇
1. 保证在火狐浏览器字体<12px,苹果横屏的时候字体显示大小还是12px html { background: #fff; -webkit-text-size-adjust: 100%; - ...
- [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...
- HDU 3746:Cyclic Nacklace
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 测试-Animator的“当前剪辑CurrentAnimatorStateInfo”
左边是Transport到Idle,右边是完全到Idle 当进入过渡时,事实上Transport状态并未结束,通过下面打印出来的hash可以看出还是Transport的状态 过渡结束才正式变为Idle ...
- Uva(10048),最短路Floyd
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 关于 android 中 postDelayed方法的讲解
代码如下: 这是一种可以创建多线程消息的函数 使用方法: 1,首先创建一个Handler对象 Handler handler=new Handler(); 2,然后创建一个Runnable对象 Run ...
- ubuntu安装和查看已安装
说明:由于图形化界面方法(如Add/Remove... 和Synaptic Package Manageer)比较简单,所以这里主要总结在终端通过命令行方式进行的软件包安装.卸载和删除的方法. 一.U ...
- linux网络基础--学习笔记