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 ...
随机推荐
- ectouch第二讲之 文件结构
相信大家在ectouch官网都注意到了,ectouch采用的MVC框架,之前一直以为它用的和ecshop一样都是smarty,本鸟默默按照smarty的文件结构研究了好几天,结果是各种文件对不上号.无 ...
- HDU 4405:Aeroplane chess(概率DP入门)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description Hzz loves ...
- Java8新特性之一、时间日期API
package com.effective.common.base.date; import java.time.Instant; import java.time.LocalDate; import ...
- 【转】MYSQL入门学习之一:基本操作
转载地址:http://www.2cto.com/database/201212/173868.html 1.登录数据库 www.2cto.com 命令:mysql -u usern ...
- java.lang.IllegalStateException异常简单分析和简单解决
我们在做文件上传或者下载,或者过滤等操作时,可能要用到页面的输出流. 例如在JSP使用: response.reset(); response.setContentType(”application/ ...
- ThinkPHP使用PHPmailer发送Email邮件
下面介绍thinkphp如何使用phpmailer发送邮件,使用这个邮件发送类,配置好参数后,一句话即可发送邮件.仅适合于thinkphp框架. 第一步,下载类库 将Mail.class.php复制到 ...
- 验证码识别--type5
验证码识别--type5 每一种验证码都是由人设计出来.在设计过程中,可能由于多个方面的原因,造成了这样或那样的可以被利用的漏洞.验证码识别,首先需要解决的问题就是发现这些漏洞--然后利用漏洞解决问题 ...
- 好玩的代码之C++实现CPU满载
#include <windows.h> #include <iostream> #include <cstdlib> using namespace std; D ...
- linux下查看电脑配置
1. 查看cpu ~$ cat /proc/cpuinfo 2. 查看内存占用 ~$ cat /proc/meminfo 3. 硬盘分区 $ cat /proc/partitions 4. ubunt ...
- Java总结反射
[案例1]通过一个对象获得完整的包名和类名 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package Reflect; /** * 通过一个对象获得完整的包名和类名 ...