以前刷过的数位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 ...
随机推荐
- JavaScript中函数对象和对象的区别
function Test (word) { console.log (word); } Test('哈哈,我是函数'); new Test('哈哈,我是对象'); //将以上的调用方式换种通俗易懂的 ...
- 对Yii 2.0模型rules的理解(load()无法正确装载数据)
在实际开发中,遇到数据表新增字段而忘记了在对应模型中rules规则中添加新增的字段,而导致load()方法装载不到新增字段,导致新增字段无法写入数据库中. 解决办法:在新增字段后及时在对应模型ru ...
- 理解Postgres性能
目录[-] 理解Postgres性能 理解缓存和缓存命中率 理解索引用途 Heroku Dashboard示例 索引缓存命中率 理解Postgres性能 对于很多应用程序开发人员来说数据库就是一个黑盒 ...
- 【Python图像特征的音乐序列生成】关于图像特征的描述词
查阅了很久的资料,决定依据Yoshida的<Image retrieval system using impression words>这篇论文里的词语来定义. Yoshida 等的 Ar ...
- linux python升级及全局环境变量设置
1.下载pythonwget https://www.python.org/ftp/python/3.4.5/Python-3.4.5.tgz 或者去官网下载压缩包 2.安装python3依赖yum ...
- Winform C# 编程 1
http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL01Wmfc0B-QfB2pS1y13peTbq
- CF Gym 100187D Holidays (数学,递推)
题意:给n个元素,从n中选两个非空集合A和B.问有多少中选法? 递推: dp[n]表示元素个数为n的方案数,对于新来的一个元素,要么加入集合,要么不加入集合自成一个集合.加入集合有三种选择,A,B,E ...
- Linux常用快捷键以及如何查看命令帮助
1.1 Linux系统快速操作常用快捷键 快捷键名称 快捷作用 Ctrl + a 将光标移至行首 Ctrl + e 将光标移至行尾 Ctrl + u 前提光标在行尾,则清除当前行所有的内容(有空 ...
- 拖拽图片到另一个div里
HTML代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 教程笔记《JavaScript深入浅出》
一.数据类型 javascript是弱数据类型语言,不需要显式的定义类型,一共有如下六种数据类型 五种基本类型:number,string,boolean,null,undefined 一种复合类型: ...