数位dp

数位DP-数字游戏

半腔热情 提交于 2020-01-13 17:12:40
数字游戏 科协里最近很流行数字游戏。 某人命名了一种不降数,这种数字必须满足从左到右各位数字呈非下降关系,如 123,446。 现在大家决定玩一个游戏,指定一个整数闭区间 [a,b],问这个区间内有多少个不降数。 输入格式 输入包含多组测试数据。 每组数据占一行,包含两个整数 a 和 b。 输出格式 每行给出一组测试数据的答案,即 [a,b] 之间有多少不降数。 数据范围 1≤a≤b≤231−1 输入样例: 1 9 1 19 输出样例: 9 18 题解: 利用类似前缀和的思想 [a, b]范围满足要求的数=[0,b]范围满足要求的数 - [0,a-1]范围满足要求的数 所以我们可以封装求[0, x]满足要求的数字为一个函数即可 一个数字x, 最右边当成第一位,数字长度为len的话,最左侧就为第len位,把他们存储到a数组中。 从最左侧开始搜索, pos记录枚举哪一位了, 需要记录前一位数字pre,pre如果比当前枚举的数字i大的话,就continue不予统计。 需要用一个limit记录当前位能枚举的上界,比如19 这个2位数,最左侧一位能枚举的范围是0-1, 不能超过1, 如果limit为false当前位能枚举到9,否则只能到最大a[pos] dfs的含义就是从左往右枚举每一位,看是否符合要求。 为了优化,记忆化即可,需要注意有limit的情形不能用记忆化,比如求

Gym - 102439H(数位DP)

泄露秘密 提交于 2020-01-12 23:09:48
Fibonacci numbers — well-known integer sequence, where F0=0, F1=1 and Fn=Fn−1+Fn−2 for n>1. Lesha doesn’t like this sequence and all the numbers x, such that we can get positive Fibonacci number by crossing out several digits. For example, Lesha doesn’t like number 193, because it is possible to cross 9 out and get F6=13. Your task is to find the number of integers from 0 to n which Lesha likes. Input The first line contains a single integer t — the number of test cases. Each of the following t lines contains a single integer n — number, until which you have to count numbers which Lesha likes.

(数位dp)POJ3252 Round Numbers

情到浓时终转凉″ 提交于 2020-01-09 15:46:35
POJ3252 Round Numbers 题意&思路: 规定在二进制数下,0的个数大于等于1的个数的数字为“round number”,给你一个区间,让你找“round number”。 数位dp,太难了啊啊啊啊…… dp[i][s0][s1]表示第i位的0数量和1数量。记录下前导0的情况。 代码: # define ll long long # include <iostream> # include <stdio.h> # include <stdlib.h> # include <algorithm> # include <string.h> # include <ctype.h> # include <queue> # include <set> # include <stack> # include <cmath> const int N = 1e6 + 10 ; const int mod = 1e7 + 9 ; const int maxn = 0x3f3f3f3f ; const int minn = 0xc0c0c0c0 ; const int inf = 99999999 ; using namespace std ; ll dp [ 40 ] [ 40 ] [ 40 ] = { 0 } , a [ 40 ] ; ll dfs ( ll len , ll s0

Balanced Number (HDU-3709)数位DP

一笑奈何 提交于 2020-01-08 20:35:51
A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the number, the distance from a digit to the pivot is the offset between it and the pivot. Then the torques of left part and right part can be calculated. It is balanced if they are the same. A balanced number must be balanced with the pivot at some of its digits. For example, 4139 is a balanced number with pivot fixed at 3. The torqueses are 4*2 + 1*1 = 9 and 9*1 = 9, for

POJ 3252 Round Numbers(数位DP)

陌路散爱 提交于 2020-01-08 17:32:39
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

Round Numbers(POJ-3252) (数位DP)

↘锁芯ラ 提交于 2020-01-07 21:03:39
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

F(x) (HDU-4734)(数位DP)

时光总嘲笑我的痴心妄想 提交于 2020-01-07 17:54:41
For a decimal number x with n digits (A nA n-1A n-2 ... A 2A 1), we define its weight as F(x) = A n * 2 n-1 + A n-1 * 2 n-2 + ... + A 2 * 2 + A 1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B, inclusive, whose weight is no more than F(A). Input The first line has a number T (T <= 10000) , indicating the number of test cases. For each test case, there are two numbers A and B (0 <= A,B < 10 9) Output For every case,you should output "Case #t: " at first, without quotes. The t is the case number starting from 1. Then output the answer.

HDU 4734 F(x) 数位dp

一世执手 提交于 2020-01-03 11:12:23
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4734 F(x) Time Limit: 1000/500 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) 问题描述 For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B, inclusive, whose weight is no more than F(A). 输入 The first line has a number T (T <= 10000) , indicating the number of test cases. For each test case, there are two numbers A and B (0 <= A,B < 109) 输出 For every case

poj 3252 Round Numbers 数位DP

不问归期 提交于 2020-01-03 10:15:25
"Round Number "被称为其二进制形式中0的个数比1的个数多。求[x,y]区间内“Round Number”的个数。 解1: dp[pos][num],到当前数位pos,0的数量减去1的数量为num的方案数,一个简单的问题,中间某个pos位上num可能为负数(这不一定是非法的,因为我还没枚举完嘛,只要最终的num>=0才能判合法,中途某个pos就不一定了) Hash 最小就-32吧(好像),直接加上32,把32当0用。 这题主要是要想讲一下lead的用法,显然我要统计0的数量,前导零是有影响的。 #pragma comment(linker, "/STACK:10240000,10240000") #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<queue> #include<set> #include<vector> #include<map> #include<stack> #include<cmath> #include<algorithm> using namespace std; const double R=0.5772156649015328606065120900; const int N=1e5+5; const int mod=1e9+7;

poj 3252 Round Numbers 数位dp

本秂侑毒 提交于 2020-01-03 10:15:08
题目大意:求区间[l,r]之间的数字的二进制形式中0的个数大于等于1 的数字的个数。 题目思路:比较特殊二进制的第一位必须是1,所有需要一个记录一下第一位是否被填过。剩下的就是一般的数位DP了 #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h> #include<queue> #include<math.h> #include<map> #define INF 0x3f3f3f3f #define MAX 1000005 #define Temp 1000000000 #define mod 2520 #define LL long long using namespace std; int bit[MAX]; LL dp[100][100][100]; LL dfs(int pos,int sum0,int sum1,int limit,int fst) { if(pos<0) { if(fst)return 1;//若第一位始终没有被填过,那么这个数是0, else { if(sum0 >= sum1)return 1; return 0; } } if(!limit && dp[pos][sum0][sum1