xtu summer individual 1 E - Palindromic Numbers
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Description
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this problem you will be given two integers i j, you have to find the number of palindromic numbers between i and j (inclusive).
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing two integers i j (0 ≤ i, j ≤ 1017).
Output
For each case, print the case number and the total number of palindromic numbers between i and j (inclusive).
Sample Input
4
1 10
100 1
1 1000
1 10000
Sample Output
Case 1: 9
Case 2: 18
Case 3: 108
Case 4: 198
解题:回文数字。分成两种类型判断,一种是长度为奇数个的,一种是长度为偶数个的。
举个栗子。。。
20 是长度为2的,偶数长度,只要枚举dp[1][2]+d[1]什么意思呢?d[1]就是长度为1 的回文数字有多少个!dp[1][2]表示以1开始,长度为2的回文数字的个数。
再说120.。枚举d[2],由于是奇数,最后是遇到只有一个数字的情况,这是只要枚举最中间这一位就可以了,把低位与高位设置成一致的,1x1,只有从0开始枚举x,只要还在120的范围内,每枚举一个就加一,一旦不在120的范围内立即跳出循环。
再说一个偶数的长度1234.。。d[3]+dp[0][2]+dp[1][2],把低位跟高位一致后,1221,判断这个数是不是在1234内,是就加一,不是就加0,好吧加0就是不加,你赢了!!!!!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
LL dp[][],d[];
int len,bit[];
void init() {
int i,j;
for(i = ; i < ; i++)
dp[i][] = dp[i][] = ;
for(i = ; i < ; i++) {
for(j = ; j < ; j++) {
dp[j][i] = *dp[][i-];
}
}
d[] = ;//以0开始的。。。0就是
for(i = ; i < ; i++) {
for(j = ; j < ; j++)
d[i] += dp[j][i];
d[i] += d[i-];
}//算出0-长度为i的所有回文数字数目
}
LL go(int e) {
if(e < ) return ;
LL sum = ;
for(int i = ; i < ; i++) {
sum += dp[i][e];
}
return sum;
}
LL cal(LL n) {
if(n < ) return n+;
LL x = n,ans = ,y = ;
int i,j,k,v,u;
for(len = ; x; x /= , len++)
bit[len] = x%;
ans += d[len-];
for(j = ,v = len>>,i = len-; i >= v; i--,j++) {
if(i == len-) {
for(k = ; k < bit[i]; k++)
ans += dp[k][len];
} else if(i == j) {
u = i;break;
} else {
for(k = ; k < bit[i]; k++)
ans += dp[k][len-j*];
}
}
if(i == j) {//奇数个长度,最后结果受最中间的那位影响
for(i = ,j = len-; i < j; i++,j--)
bit[i] = bit[j];
for(k = ; k < ; k++){
bit[u] = k;
for(y = i = ; i < len; i++)
y = y*+bit[i];
if(y <= n) ans++;
else break;
}
}else{//偶数个长度,最后结果受最后一位影响
for(i = ,j = len-; i < j; i++,j--)
bit[i] = bit[j];
for(y = i = ; i < len; i++)
y = y*+bit[i];
if(y <= n) ans++;
}
return ans;
}
int main() {
init();
int t,ks = ;
LL a,b,c;
scanf("%d",&t);
while(t--){
scanf("%lld %lld",&a,&b);
if(a > b) swap(a,b);
printf("Case %d: %lld\n",ks++,cal(b)-cal(a-));
}
return ;
}
xtu summer individual 1 E - Palindromic Numbers的更多相关文章
- xtu summer individual 1 D - Round Numbers
D - Round Numbers Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u D ...
- LightOJ 1205 Palindromic Numbers
数位DP.... Palindromic Numbers Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %l ...
- Lightoj1205——Palindromic Numbers(数位dp+回文数)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- LightOJ - 1396 :Palindromic Numbers (III)(逐位确定法)
Vinci is a little boy and is very creative. One day his teacher asked him to write all the Palindrom ...
- LightOJ - 1205:Palindromic Numbers (数位DP&回文串)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- [暑假集训--数位dp]LightOj1205 Palindromic Numbers
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 2 D - Colliders
Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...
随机推荐
- P2345 奶牛集会andP2657 低头一族
做法是一样的 题目背景 MooFest, Open 题目描述 约翰的N 头奶牛每年都会参加“哞哞大会”.哞哞大会是奶牛界的盛事.集会上的活动很 多,比如堆干草,跨栅栏,摸牛仔的屁股等等.它们参加活动时 ...
- BZOJ1132: [POI2008]Tro(叉积 排序)
题意 世上最良心题目描述qwq 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 Sol 直接模拟是$n^3$的. 考虑先枚举一个$i$,那么我们要算的就是$\sum_ ...
- wkWebView 的一些问题
导语 WKWebView 是苹果在 WWDC 2014 上推出的新一代 webView 组件,用以替代 UIKit 中笨重难用.内存泄漏的 UIWebView.WKWebView 拥有60fps滚动刷 ...
- Android MVVM小结
一.概念 关于MVC.MVP与MVVM的概念就不介绍了,总之一句话,MVVM概念出现比MVP早,MVP比MVC早,作为程序员就应该去学习最新的技术不是?详细的概念介绍移步这里吧,https://www ...
- js中传统事件绑定模拟现代事件处理
大家都知道,IE中的现代事件绑定(attachEvent)与W3C标准的(addEventListener)相比存在很多问题, 例如:内存泄漏,重复添加事件并触发的时候是倒叙执行等. 下面是用传统事件 ...
- This is such a crock of shit—From Scent of a woman
- Mr. Slade. - This is such a crock of shit! - Mr. Trask. - Please watch your language, Mr. Slade. Y ...
- IOS代码收集
http://mobile.51cto.com/hot-410417.htm 退回输入键盘: - (BOOL) textFieldShouldReturn:(id)textField{ [textFi ...
- FPGA编程技巧系列之按键边沿检测
抖动的产生: 通常的按键所用开关为机械弹性开关,当机械触点断开.闭合时,由于机械触点的弹性作用,一个按键开关在闭合时不会马上稳定地接通,在断开时也不会一下子断开.因而在闭合及断开的瞬间均伴随有一连串的 ...
- 阿里云服务器ECS部署应用教程
购买阿里云服务器 大多数云服务器默认安装的语言运行环境版本都很旧了,python用的还是2.7,JDK用的还是1.6的,在ECS云服务器中可以自行安装,包括python3.4之类的. 在次页面购买EC ...
- html自己写响应式布局(说起来很高大上的样子,但是其实很简单)
第一步,打开电脑中安装的Sublime Text3,新建demo文件夹用来存放文件,在里面新建一个HTML文件,通过Tab快捷键迅速创建一个HTML模板,并命名标题. 第二步,在Body标签里添加三个 ...