codeforces 204A A. Little Elephant and Interval(dp+组合数学)
来源:互联网 发布:java 标准库数据结构 编辑:程序博客网 时间:2024/06/06 09:07
题目链接:
codeforces 204A
题目大意:
给出一个l和r,求取在l和r之间的首尾相同的数的个数。
题目分析:
- 按位进行统计,计算出不大于某一个数的所有的合法的情况。然后可以利用这个前缀和求取区间和。
- 按位统计的时候,首先特判数的长度为1位和两位的情况,分别是10和9,如果当前数就是1位,那么就是这个数的大小,其他具体细节见代码.
- 然后就是统计所有不足位的情况,也就是数的长度不到给定数长度的情况,不足位的数一定小于给定数,所以直接固定首尾,结果加上
10n−2 即可。 - 足位的情况,就是枚举每一位,然后如果当前枚举为小于给定数数位上的数,那么后面可以随便放置,所以有
10n−i−1种情况 ,当前位等于给定数位上的数的时候,当前先不能判断,等后面能够确定时统计结果。
AC代码:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define MAX 27using namespace std;typedef long long LL;char l[MAX],r[MAX];LL dp[MAX],pp[MAX];void init ( ){ pp[0] = 1; for ( int i = 1 ; i < MAX ;i++ ) pp[i] = pp[i-1]*10LL;}LL solve ( char s[] ){ LL ret = 0; int n = strlen ( s ); if ( n > 1 ) ret += 10; else ret += s[0]-48+1; if ( n > 2 ) ret += 9; for ( int i = 1; i < n-2 ;i++ ) ret += 9LL*pp[i]; for ( int i = 0 ; i < n-1; i++ ) { int x = s[i]-48; if ( i == 0 ) x--; ret += x*pp[n-2-i]; } if ( n > 1 && s[n-1] >= s[0] ) ret++; return ret;}int main ( ){ init ( ); while ( ~scanf ( "%s%s" , l , r ) ) { //cout << solve( r ) << " " << solve ( l ) << endl; int n = strlen ( l ); printf ( "%I64d\n" , solve ( r ) - solve(l) + ( l[n-1] == l[0]?1LL:0LL) ); }}
0 0
- codeforces 204A A. Little Elephant and Interval(dp+组合数学)
- codeforces 204A Little Elephant and Interval (数位dp)
- CodeForces 204A Little Elephant and Interval 数位DP
- Codeforces 204A Little Elephant and Interval(数位DP)
- codeforces 204A Little Elephant and Interval
- CodeForces 204A Little Elephant and Interval
- CodeForces 204A Little Elephant and Interval
- codeforces CodeForces 204A Little Elephant and Interval (数位dp简单)
- codeforces 204(Div.1 A) Little Elephant and Interval(贪心)
- Codeforces 204A Little Elephant and Interval(数位统计)
- A. Little Elephant and Interval
- Codeforces Round #129 (Div. 1) A Little Elephant and Interval
- CodeForces 204A - Little Elephant and Interval 算答案..易错..暴力打表对拍
- codeforces 204A Little Elephant and Interval 美丽的区间转换
- <codeforces>A. Little Elephant and Rozdil
- CodeForces 220A Little Elephant and Problem
- Codeforces Round #136 (Div. 2) / 221A Little Elephant and Function (数学)
- Little Elephant and Interval
- android图片合成
- 结构体struct简介(下)
- 少走弯路的十条忠告-http://www.cnblogs.com/xdp-gacl/p/3952405.html孤傲苍狼的博客
- [模版] 位运算 转自USACO
- 一、安装pip
- codeforces 204A A. Little Elephant and Interval(dp+组合数学)
- 关于android和javascript函数的互相调用
- SignalR 2.0 系列:SignalR简介
- Fragment实现tabhost的效果
- 支付宝外卡收单 使用手册(一) COPYandPAY
- js之Array数组对象属性及方法
- C算法-求素数
- 将一个字符串中的特定字符添加颜色
- 9月2日最新易启秀源码3GB大小700套模版