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 ...
随机推荐
- YTU 3002: 出栈顺序(栈和队列)
3002: 出栈顺序(栈和队列) 时间限制: 1 Sec 内存限制: 128 MB 提交: 80 解决: 20 题目描述 给出一个入栈序列,和一个出栈序列,判断该出栈序列是否正确. 输入 输入包含 ...
- 使用KNN对MNIST数据集进行实验
由于KNN的计算量太大,还没有使用KD-tree进行优化,所以对于60000训练集,10000测试集的数据计算比较慢.这里只是想测试观察一下KNN的效果而已,不调参. K选择之前看过貌似最好不要超过2 ...
- 数据库连接池(DBCP:为数据统一建立一个缓冲池,现在企业开发使用)
数据库连接池:(里面放了许多连接数据的链接,负责分配,管理,释放数据库连接,可重复使用连接,而不新建 )为数据统一连接建立一个缓冲池,放好了一定数据库连接,使用时在缓冲池里面拿,用完之后再还给缓冲池 ...
- poj3372 Candy Distribution
可以证明: f(k) = k *(k - 1)/ 2 (1 ≤ k ≤ n)是n的完全剩余系当且仅当n = 2 ^ t. http://poj.org/problem?id=3372
- 【20160924】GOCVHelper MFC增强算法(3)
//获得当前目录路径 static CString GetLocalPath(){ CString csCfgFilePath; GetModuleFi ...
- 2016年11月26日 星期六 --出埃及记 Exodus 20:17
2016年11月26日 星期六 --出埃及记 Exodus 20:17 "You shall not covet your neighbor's house. You shall not c ...
- JLINK使用教程详解,以及与JTAG区别
对于一个新手来说,一切都不容易. 而从头学起也是一件非常美好的事. 观看 调试ARM,要遵循ARM的调试接口协议,JTAG就是其中的一种.当仿真时,IAR.KEIL.ADS等都有一个公共的调试 ...
- 逐个后移,匹配符合要求的选项,ie7有bug
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- V-rep学习笔记:曲柄摇杆机构
在ADAMS中创建一个曲柄摇杆机构很方便,但是V-rep中建模就比较麻烦.下面将自己在V-rep中建立曲柄摇杆机构模型的过程记录下来(由于对V-rep不是很熟,可能会有一些错误,只能等以后发现了再改进 ...
- CMD方式修改MySQL的root用户密码
1.CMD下,进行mysql的bin目录下; 2.输入mysql -u root -p,输入旧密码,进入mysql状态: 3.MySQL>use MySQL; 4.update user set ...