以前刷过的数位dp
TOJ1688: Round Numbers
Description
The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets to be milked first. They can't even flip a coin because it's so hard to toss using hooves.
They have thus resorted to "round number" matching. The first cow picks an integer less than two billion. The second cow does the same. If the numbers are both "round numbers", the first cow wins,
otherwise the second cow wins.
A positive integer N is said to be a "round number" if the binary representation of N has as many or more zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has two zeroes and two ones; thus, 9 is a round number. The integer 26 is 11010 in binary; since it has two zeroes and three ones, it is not a round number.
Obviously, it takes cows a while to convert numbers to binary, so the winner takes a while to determine. Bessie wants to cheat and thinks she can do that if she knows how many "round numbers" are in a given range.
Help her by writing a program that tells how many round numbers appear in the inclusive range given by the input (1 ≤ Start < Finish ≤ 2,000,000,000).
Input
Line 1: Two space-separated integers, respectively Start and Finish.
Output
Line 1: A single integer that is the count of round numbers in the inclusive range Start..Finish
Sample Input
2 12
Sample Output
6
Source
0的个数进行dp
#include<bits/stdc++.h>
using namespace std;
int dp[][],a[],l,r;
int dfs(int pos,int st,int pre,int lim)
{
if(pos<)return st<=;
if(!lim&&!pre&&dp[pos][st]!=-)return dp[pos][st];
int mi=lim?a[pos]:,ans=;
for(int i=;i<=mi;i++)
if(pre&&!i)ans+=dfs(pos-,st,,lim&&i==a[pos]);
else ans+=dfs(pos-,i?st+:st-,,lim&&i==a[pos]);
if(!lim&&!pre)dp[pos][st]=ans;
return ans;
}
int la(int x)
{
int tot=;
while(x)a[tot++]=x&,x>>=;
return dfs(tot-,,,);
}
int main()
{
memset(dp,-,sizeof dp);
scanf("%d%d",&l,&r);
printf("%d\n",la(r)-la(l-));
return ;
}
不要62
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
Input输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。
Output对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。
Sample Input
1 100
0 0
Sample Output
80
很简单的数位dp
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define pll pair<long long,long long>
#define pii pair<int,int>
#define pq priority_queue
const int N=,MD=1e9+,INF=0x3f3f3f3f;
const ll LL_INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-,e=exp(),PI=acos(-.);
int c[N],dp[N][];
int dfs(int pos,int st,int pre,int lim)
{
if(pos<)return ;
if(!lim&&dp[pos][st]!=-)return dp[pos][st];
int last=lim?c[pos]:,ans=;
for(int i=; i<=last; i++)
if(pre==&&i==||i==)continue;
else ans+=dfs(pos-,i==,i,lim&&(i==last));
if(!lim)dp[pos][st]=ans;
return ans;
}
int slove(int q)
{
int cnt=;
while(q)c[cnt++]=q%,q/=;
return dfs(cnt-,,-,);
}
int main()
{
ios::sync_with_stdio(false),cin.tie(),cout.tie();
int x,y;
while(cin>>x>>y,x+y)
{
memset(dp,-,sizeof dp);
cout<<slove(y)-slove(x-)<<"\n";
}
return ;
}
Amount of Degrees
18 = 2 4+2 1,
20 = 2 4+2 2.
Input
Output
Example
input | output |
---|---|
15 20 |
3 |
也不难吧,论文题里的模板题
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define pll pair<long long,long long>
#define pii pair<int,int>
#define pq priority_queue
const int N=,MD=1e9+,INF=0x3f3f3f3f;
const ll LL_INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-,e=exp(),PI=acos(-.);
int c[N],dp[N][N];
int x,y,k,b;
int dfs(int pos,int st,int lim)
{
if(st>k||st+pos+<k)return ;
if(pos<)return st==k;
if(!lim&&dp[pos][st]!=-)return dp[pos][st];
int last=lim?c[pos]:b-,ans=,mi=min(last,);
for(int i=;i<=mi;i++)ans+=dfs(pos-,st+i,lim&&(i==last));
if(!lim)dp[pos][st]=ans;
return ans;
}
int slove(int q)
{
int cnt=;
while(q)c[cnt++]=q%b,q/=b;
return dfs(cnt-,,);
}
int main()
{
ios::sync_with_stdio(false),cin.tie(),cout.tie();
cin>>x>>y>>k>>b;
memset(dp,-,sizeof dp);
cout<<slove(y)-slove(x-);
return ;
}
上海邀请赛的
链接:https://www.nowcoder.com/acm/contest/163/J
来源:牛客网
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
We will not argue with this and just count the quantity of beautiful numbers from 1 to N.
输入描述:
The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
Each test case contains a line with a positive integer N (1 ≤ N ≤ 10
12
).
输出描述:
For each test case, print the case number and the quantity of beautiful numbers in [1, N].
输入例子:
2
10
18
输出例子:
Case 1: 10
Case 2: 12
-->
输出
Case 1: 10
Case 2: 12
很难想到从数位和出发
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[];
ll dp[][][][],n;
ll dfs(int pos,int st,int sum,int mod,bool lim)
{
if(pos<) return mod==&&st==sum;
if(sum>st||sum+*(pos+)<st)return ;
if(!lim&&dp[st][pos][sum][mod]>=) return dp[st][pos][sum][mod];
ll ans=;
int mi=lim?a[pos]:;
for(int i=;i<=mi;i++)ans+=dfs(pos-,st,sum+i,(mod*+i)%st,lim&&i==mi);
if(!lim) dp[st][pos][sum][mod]=ans;
return ans;
}
ll cal(ll n)
{
int pos=;
while(n) a[pos++]=n%,n/=;
ll ans=;
for(int i=;i<=;i++)ans+=dfs(pos-,i,,,);
return ans;
}
int main()
{
int T,ca=;
memset(dp,-,sizeof(dp));
scanf("%d",&T);
while(T--)scanf("%lld",&n),printf("Case %d: %lld\n",++ca,cal(n));
return ;
}
以前刷过的数位dp的更多相关文章
- HDU5787 K-wolf Number 数位dp
分析:赛场上也知道是裸的数位dp,但是无奈刷数位dp题刷的太少了,并不能写出来 一点感想:赛后补题,看了题解的map记录状态,一脸蒙逼,也是非常的不爽,然后想看别人写的,不是递归就是写的比较乱 而且我 ...
- 数位DP专题
这周开始刷数位DP,在网上找到一份神级数位DP模板,做起题目来爽歪歪. http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html in ...
- 数位DP入门Ural1057
CF一战让我觉得很疲倦,所以今天感觉很慢. 昨天解D题时候,因为太累,根本连题目都没看,今天看了之后感觉不会做,听闻是数位DP问题. 有某神说过,DP的功力建立在刷过的题上,我真的毫无功力可言. 介绍 ...
- 数位dp整理
数位dp的思想就在于递归,记录当前的某一个唯一状态,依次递归下去,要注意唯一. 数位dp常设的状态有当前位置,上一数字,是否具有前导零,是否有限制. 1.CodeForces 55DBeautiful ...
- hihoCoder #1770 : 单调数(数位dp)
题面 我们定义一个数是单调数,当且仅当构成这个数每一个数位都是单调不降或不增的. 例如 \(123\) 和 \(321\) 和 \(221\) 和 \(111\) 是单调的,而 \(312\) 不是单 ...
- 数位dp小练
最近刷题的同时还得填填坑,说来你们也不信,我还不会数位dp. 照例推几篇博客: 数位DP讲解 数位dp 的简单入门 这两篇博客讲的都很好,不过代码推荐记搜的形式,不仅易于理解,还短. 数位dp的式子一 ...
- 数位DP复习小结
转载请注明原文地址http://www.cnblogs.com/LadyLex/p/8490222.html 之前学数位dp的时候底子没打扎实 虚的要死 这次正好有时间……刷了刷之前没做的题目 感觉自 ...
- 【数位DP】【SCOI2009】windy数
传送门 Description \(windy\)定义了一种\(windy\)数.不含前导零且相邻两个数字之差至少为\(2\)的正整数被称为\(windy\)数.\(windy\)想知道, 在\(A\ ...
- [Bzoj4521][Cqoi2016]手机号码(数位dp)
4521: [Cqoi2016]手机号码 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 870 Solved: 505[Submit][Status ...
随机推荐
- 抓取GridView "编辑"模式下,TextBox修改后的数值
[FAQ]抓取GridView "编辑"模式下,TextBox修改后的数值 -- ASP.NET专题实务「上集」Ch.10 抓取GridView "编辑"模式下 ...
- 协议学习之 vamei博客系列 总结
1. 分层: 物理层(physical layer) 所谓的物理层,是指光纤.电缆或者电磁波等真实存在的物理媒介.这些媒介可以传送物理信号,比如亮度.电压或者振幅.对于数字应用来说,我们只需要两种物理 ...
- springmvc 的原理分析
1. 用户发送请求至前端控制器(DispatcherServlet) 2.DispatcherServlet 将受到的请求调用HandlerMapping 处理映射器 3.处理器映射器根据配置注解找到 ...
- JavaScript内存泄露,闭包内存泄露如何解决
本文原链接:https://cloud.tencent.com/developer/article/1340979 JavaScript 内存泄露的4种方式及如何避免 简介 什么是内存泄露? Java ...
- Context 使用不当造成内存泄露
问题: Activity中的context被传递给了一个生命周期长过activity的对象(通常为静态单实例变量),导致activity不能正常被销毁. 示例:Activity 调用 ChatMgr ...
- python 函数内使用全局变量
x = def change_global(): global x x = x + change_global() print(x) result: 2
- String中关于BeanFactory
org.springframework.beans及org.springframework.context包是Spring IoC容器的基础.BeanFactory提供的高级配置机制,使得管理任何性质 ...
- 响应者链和Hit-Test 机制
概念: 响应者 : 对用户交互动作事件进行响应的对象.响应者链:成为处理事件的响应者的先后顺序链. 1.Hit-Test 机制 当用户触摸(Touch)屏幕进行交互时,系统首先要找到响应者(Respo ...
- 如何挂载一个镜像文件(how to mount an image file)
如何挂载一个镜像文件(how to mount an image file) 08/16/2012master 4 Comments 在使用KVM或Xen虚拟化的情况下,经常需要使用镜像文件(imag ...
- DNA Pairing-freecodecamp算法题目
DNA Pairing 1.要求 DNA 链缺少配对的碱基.依据每一个碱基,为其找到配对的碱基,然后将结果作为第二个数组返回. Base pairs(碱基对)是一对 AT 和 CG,为给定的字母匹配缺 ...