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

USACO November 2006

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

HDU - 2089

杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。 
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。 
不吉利的数字为所有含有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

URAL - 1057

Create a code to determine the amount of integers, lying in the set [ XY] and being a sum of exactly K different integer degrees of B.
Example. Let X=15, Y=20, K=2, B=2. By this example 3 numbers are the sum of exactly two integer degrees of number 2:
17 = 2 4+2 0
18 = 2 4+2 1
20 = 2 4+2 2.

Input

The first line of input contains integers X and Y, separated with a space (1 ≤ X ≤  Y ≤ 2 31−1). The next two lines contain integers K and B (1 ≤ K ≤ 20; 2 ≤  B ≤ 10).

Output

Output should contain a single integer — the amount of integers, lying between Xand Y, being a sum of exactly K different integer degrees of B.

Example

input output
15 20
2
2
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
来源:牛客网

Beautiful Numbers
时间限制:C/C++ 8秒,其他语言16秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

NIBGNAUK is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by the sum of its digits.

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

-->

示例1

输入

复制

2
10
18

输出

复制

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的更多相关文章

  1. HDU5787 K-wolf Number 数位dp

    分析:赛场上也知道是裸的数位dp,但是无奈刷数位dp题刷的太少了,并不能写出来 一点感想:赛后补题,看了题解的map记录状态,一脸蒙逼,也是非常的不爽,然后想看别人写的,不是递归就是写的比较乱 而且我 ...

  2. 数位DP专题

    这周开始刷数位DP,在网上找到一份神级数位DP模板,做起题目来爽歪歪. http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html in ...

  3. 数位DP入门Ural1057

    CF一战让我觉得很疲倦,所以今天感觉很慢. 昨天解D题时候,因为太累,根本连题目都没看,今天看了之后感觉不会做,听闻是数位DP问题. 有某神说过,DP的功力建立在刷过的题上,我真的毫无功力可言. 介绍 ...

  4. 数位dp整理

    数位dp的思想就在于递归,记录当前的某一个唯一状态,依次递归下去,要注意唯一. 数位dp常设的状态有当前位置,上一数字,是否具有前导零,是否有限制. 1.CodeForces 55DBeautiful ...

  5. hihoCoder #1770 : 单调数(数位dp)

    题面 我们定义一个数是单调数,当且仅当构成这个数每一个数位都是单调不降或不增的. 例如 \(123\) 和 \(321\) 和 \(221\) 和 \(111\) 是单调的,而 \(312\) 不是单 ...

  6. 数位dp小练

    最近刷题的同时还得填填坑,说来你们也不信,我还不会数位dp. 照例推几篇博客: 数位DP讲解 数位dp 的简单入门 这两篇博客讲的都很好,不过代码推荐记搜的形式,不仅易于理解,还短. 数位dp的式子一 ...

  7. 数位DP复习小结

    转载请注明原文地址http://www.cnblogs.com/LadyLex/p/8490222.html 之前学数位dp的时候底子没打扎实 虚的要死 这次正好有时间……刷了刷之前没做的题目 感觉自 ...

  8. 【数位DP】【SCOI2009】windy数

    传送门 Description \(windy\)定义了一种\(windy\)数.不含前导零且相邻两个数字之差至少为\(2\)的正整数被称为\(windy\)数.\(windy\)想知道, 在\(A\ ...

  9. [Bzoj4521][Cqoi2016]手机号码(数位dp)

    4521: [Cqoi2016]手机号码 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 870  Solved: 505[Submit][Status ...

随机推荐

  1. uvm_reg_block——寄存器模型(七)

    这是寄存器模型的顶层 //------------------------------------------------------------------------ // Class: uvm_ ...

  2. jsp跳转标签<jsp:forward>

    forward.jsp <%@ page language="java" contentType="text/html; charset=utf-8" p ...

  3. Python3获取大量电影信息:调用API

    实验室这段时间要采集电影的信息,给出了一个很大的数据集,数据集包含了4000多个电影名,需要我写一个爬虫来爬取电影名对应的电影信息. 其实在实际运作中,根本就不需要爬虫,只需要一点简单的Python基 ...

  4. python爬虫之路——构造URL集

    例某网站的URL集是这样的 https://www.555zw.com/book/40/40934/10334793.html https://www.555zw.com/book/40/40934/ ...

  5. UVA 12898 - And Or 与和或 (思路题)

    思路就是有零一变化的位Or以后一定是1,And以后一定是0:那么如果b的二进制更长那么就把包含a的部分全部置为1或0,如果一样长那么就把不同的部分置为1或0. 今天被这题坑的地方:1默认是int,如果 ...

  6. groff - groff 文档排版系统前端

    总览 (SYNOPSIS) groff [ -abehilpstvzCENRSUVXZ ] [ -wname ] [ -Wname ] [ -mname ] [ -Fdir ] [ -Idir ] [ ...

  7. 【PowerShell语音计算器】

    [PowerShell语音计算器]带中文发音功能的计算器程序,支持鼠标和小键盘输入,支持多种数值转人民币大写,如:123.4--->壹佰贰拾叁点肆圆. 版本号 1.51 下载:http://fi ...

  8. java基础—static关键字

    一.static关键字

  9. Silverlight日记:动态操作Grid

    一,动态生成Grid public static Grid CreateGrid(List<T_METER> List) { var g = new Grid(); if (null == ...

  10. 作业题:输出单个字符 输入单个字符 scanf printf

    输出单个字符用putchar() #include <iostream> using namespace std; int main(){ char x='B'; char y='O'; ...