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. jsp四大作用域之Session

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

  2. 使用工具Source Monitor测量您Java代码的环复杂度

    代码的环复杂度(Cyclomatic complexity,有时也翻译成圈复杂度)是一种代码复杂度的衡量标准,在1976年由Thomas J. McCabe, Sr. 提出. 来看看计算公式. 代码环 ...

  3. Oracle RAC/Clusterware 多种心跳heartbeat机制介绍 RAC超时机制分析

    ORACLE RAC中最主要存在2种clusterware集群件心跳 &  RAC超时机制分析: 1.Network Heartbeat 网络心跳 每秒发生一次: 10.2.0.4以后网络心跳 ...

  4. Raid 6与raid 5的区别

    RAID5和RAID6有下面几个区别: 1.冗余和数据恢复能力 RAID组级别 冗余及数据恢复能力 数据恢复策略 RAID 5 存在分散在不同条带上的奇偶校验数据 允许一块数据盘故障,并可通过奇偶校验 ...

  5. Python-OpenCV——Image Blurring(Image Smoothing)

    通过将图像与低通滤波器内核卷积来实现图像模糊.它有助于消除噪音.它实际上从图像中去除了高频内容(例如:噪声,边缘).因此在此操作中边缘会有点模(嗯,有模糊技术,也不会模糊边缘). OpenCV主要提供 ...

  6. 一个.java文件内只能写一个class吗

    先给结论:当然不是!! 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致.一个文件中可以不含public类,如果只有一个非public类,此时可以跟文件名不同. 为 ...

  7. 分类回归树(CART)

    概要 本部分介绍 CART,是一种非常重要的机器学习算法.   基本原理   CART 全称为 Classification And Regression Trees,即分类回归树.顾名思义,该算法既 ...

  8. oc中将CGRect、CGSize、CGPoint等结构体转换为字符串

    CGRect rect = CGRectMake(160, 230, 200, 200); CGPoint point = CGPointMake(20, 20); CGSize size =  CG ...

  9. 27. Remove Element@python

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  10. NOIP模拟赛 路面修整

    [题目描述] FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中. 整条路被分成了N段,N个 ...