light 1205 - Palindromic Numbers(数位dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1205
题解:这题作为一个数位dp,是需要咚咚脑子想想的。这个数位dp方程可能不是很好想到,由于回文串的性质肯定要考虑到对称方面,那么不妨设dp[len][sta][flag]
表示len到sta这些字符串是否能构成回文。这里的数位dp有些特殊由于要考虑到回文的性质会涉及到回朔具体看一下代码。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
int dig[] , now_dig[];
ll dp[][][];
ll dfs(int len , int sta , int flag , int first) {
if(len == ) return (ll)flag;
if(dp[len][sta][flag] != - && !first) return dp[len][sta][flag];
int t = (first ? dig[len] : );
ll res = ;
for(int i = ; i <= t ; i++) {
now_dig[len] = i;
if(!i && len == sta) {
res += dfs(len - , sta - , flag , first && i == t);
}
else if(flag && len <= (sta + ) / ) {
res += dfs(len - , sta , i == now_dig[sta - len + ] , first && i == t);
}
else {
res += dfs(len - , sta , flag , first && i == t);
}
}
if(!first) dp[len][sta][flag] = res;
return res;
}
ll getnum(ll x) {
if(x < ) return ;
if(x == ) return ;
int len = ;
while(x) {
dig[++len] = x % ;
x /= ;
}
return dfs(len , len , , );
}
int main() {
int t;
ll n , m;
int Case = ;
scanf("%d" , &t);
memset(dp , - , sizeof(dp));
while(t--) {
scanf("%lld%lld" , &m , &n);
if(m < n) swap(m , n);
printf("Case %d: %lld\n" , ++Case , getnum(m) - getnum(n - ));
}
return ;
}
light 1205 - Palindromic Numbers(数位dp)的更多相关文章
- light oj 1205 - Palindromic Numbers 数位DP
思路:搜索的时候是从高位到低位,所以一旦遇到非0数字,也就确定了数的长度,这样就知道回文串的中心点. 代码如下: #include<iostream> #include<cstdio ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- LightOJ 1205 Palindromic Numbers
数位DP.... Palindromic Numbers Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %l ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
- poj 3252 Round Numbers(数位dp 处理前导零)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- uva 10712 - Count the Numbers(数位dp)
题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
随机推荐
- 【nodejs原理&源码赏析(9)】用node-ssh实现轻量级自动化部署
目录 一. 需求描述 二. 预备知识 IP+端口访问 域名访问 三. Nodejs应用的手动部署 四. 基于nodejs的自动部署 4.1 package.json中的scripts 4.2 自动化发 ...
- SpringMvc新建实例配置
一.创建项目: 1.建立新的动态web项目: 2.为项目命名为:SpringMVC_01 3.添加tomcat运行时环境\依赖库 如果是MyEclipse的话创建web项目时就不需要此步骤 右键项目 ...
- JDK的命令行工具系列 (一) jps、jstat
概述 在我们进行故障定位和性能分析时, 可以使用Java Dump(也叫Dump文件)来帮助排查问题, 它记录了JVM运行期间的内存占用和线程执行等情况.其中Heap Dump文件是二进制格式, 它保 ...
- .NET中的值类型与引用类型
.NET中的值类型与引用类型 这是一个常见面试题,值类型(Value Type)和引用类型(Reference Type)有什么区别?他们性能方面有什么区别? TL;DR(先看结论) 值类型 引用类型 ...
- AUTOSAR学习之RTE - 基本概念
1.什么是RTE? The Run-Time Environment (RTE) is at the heart of the AUTOSAR ECU architecture. The RTE is ...
- Netty服务端启动过程相关源码分析
1.Netty 是怎么创建服务端Channel的呢? 我们在使用ServerBootstrap.bind(端口)方法时,最终调用其父类AbstractBootstrap中的doBind方法,相关源码如 ...
- Go中配置文件读取的几种方式
日常开发中读取配置文件包含以下几种格式: json 格式字符串 K=V 键值对 xml 文件 yml 格式文件 toml 格式文件 前面两种书写简单,解析过程也比较简单.xml形式书写比较累赘,yml ...
- Java Web基础面试题整理
Tomcat的缺省端口是多少,怎么修改 tomcat默认缺省端口是8080 修改方法: 找到Tomcat目录下的conf文件夹 进入conf文件夹里面找到server.xml文件 打开server.x ...
- poj 1286 polya定理
Necklace of Beads Description Beads of red, blue or green colors are connected together into a circu ...
- 不得不会的10点Java基础知识
1.实例变量和类变量 实例变量:指每个对象独立的,修改其中一个对象的实例变量,不会影响其他实例变量的值,变量值无 static 关键字修饰: 类变量:是指所有对象共享的,其中一个对象把该变量的值修改了 ...