A. Little Elephant and Interval
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r).
The Little Elephant has to find the number of such integers x (l ≤ x ≤ r),
that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will
be included in the answer and 47, 253 or 1020 will
not.
Help him and count the number of described numbers x for a given pair l and r.
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) —
the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams
or the %I64dspecifier.
On a single line print a single integer — the answer to the problem.
2 47
12
47 1024
98
看了其他博客都是数位dp,我用了不同的解法。对于n,m,分别算出cal(n),cal(m),即(0,n]符合结果的数,然后算出cal(n)-cal(m-1);
#include<stdio.h>
#include<string.h>
int b[100]={0,1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77,88,99,101};
__int64 pow(int n,int m)
{
int i;
__int64 sum=1;
for(i=1;i<=m;i++){
sum=sum*n;
}
return sum;
} __int64 cal(__int64 n)
{
int len=0,i,j,sum;
if(n>=0 && n<=9)return n;
/*if(n>=10 && n<=99){ //这里两位数的可以在这里特判一下,时间会减少30ms;
for(i=1;i<=20;i++){
if(n>=b[i]){
sum=i;
}
else break;
}
return sum;
}*/ __int64 x=n,ans=0,t,num;
int a[100];
memset(a,0,sizeof(a));
while(x>0){
a[++len]=x%10;x=x/10;
}
ans=ans+9;
for(i=2;i<=len-1;i++){
ans=ans+9*pow(10,i-2);
}
if(len>2) ans=ans+a[len]*(pow(10,len-2))-pow(10,len-2); if(a[1]>=a[len]){
if(len>2)
{
ans=ans+(n-a[len]*pow(10,len-1))/10+1;
return ans;
}
else {ans=ans+a[len];return ans;}
}
t=n;
while((t%10)!=a[len]){ //如果最高位和最低位不同,那么先让这个数每次减一,减到最低位等于一开始的最高位,
t--; //然后看最高位的数是否改变,如果没有改变,那么总数加上除了首尾的中间一些数,如57895,取中间为789.
} //如果改变,则直接返回
if(len==2){
ans=ans+a[len]-1;return ans;
} if(t-a[len]*pow(10,len-1)<0){
return ans;
}
else{
ans=ans+(t-a[len]*pow(10,len-1))/10+1;
return ans;
}
} int main()
{
__int64 n,m,n1,m1;
int i,j;
//while(scanf("%I64d",&n)!=EOF)
while(scanf("%I64d%I64d",&n,&m)!=EOF)
{
printf("%I64d\n",cal(m)-cal(n-1));
//printf("%I64d\n",cal(n));
}
return 0;
}
今天又用数位dp的方法做了一下。
思路:先预处理出dp[i][j]表示第i位为j符合要求的数的个数,然后就和普通的数位dp一样了,算的时候算[0,r)的个数,那么最后答案就是solve(m+1)-solve(n)。这里注意pow函数精度不够,所以要自己手写一个。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000050
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef long double ldb;
ll po[25];
void init1()
{
int i,j;
po[0]=1;
for(i=1;i<=18;i++)po[i]=po[i-1]*10;
}
ll dp[24][12];
void init()
{
int i,j,k;
memset(dp,0,sizeof(dp));
for(j=0;j<=9;j++)dp[1][j]=1;
for(i=2;i<=19;i++){
for(j=0;j<=9;j++){
if(j==0){
for(k=0;k<=9;k++){
dp[i][j]+=dp[i-1][k];
}
}
else{
dp[i][j]=po[i-2];
}
}
}
}
ll solve(ll x)
{
int i,j,len=0;
ll t=x;
int wei[20];
while(t){
wei[++len]=t%10;
t/=10;
}
wei[len+1]=0;
if(len==1){
return x;
}
ll sum=0;
for(i=len;i>=1;i--){
if(i==len){
for(j=0;j<wei[i];j++){
sum+=dp[i][j];
}
}
else{
if(i==1){
if(wei[1]>wei[len] ){
sum++;
}
}
else{
for(j=0;j<wei[i];j++){
sum+=po[i-2];
}
}
}
}
return sum;
}
int main()
{
//freopen("o.txt","w",stdout);
ll n,m;
int i,j;
init1();
init();
while(scanf("%I64d%I64d",&n,&m)!=EOF){
printf("%lld\n",solve(m+1)-solve(n) );
}
return 0;
}
A. Little Elephant and Interval的更多相关文章
- Codeforces D. Little Elephant and Interval(思维找规律数位dp)
题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces 204A Little Elephant and Interval
http://codeforces.com/problemset/problem/204/A 题意:给定一个[L,R]区间,求这个区间里面首位和末尾相同的数字有多少个 思路:考虑这个问题满足区间加减, ...
- codeforces 204(Div.1 A) Little Elephant and Interval(贪心)
题意: 有一种个位数与最高位数字相等的数字,求在l,r的范围内,这样的数字的个数. 思路: 找下规律就知道当当n>10的时候除去个位以后的答案等于n/10,然后考虑第一个数字是否小于最后一个.小 ...
- Codeforces Round #129 (Div. 2)
A. Little Elephant and Rozdil 求\(n\)个数中最小值的个数及下标. B. Little Elephant and Sorting \[\sum_{i=1}^{n-1}{ ...
- Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx
问题: 在linux服务器上使用maven编译war时报错: 16:41:35 [FATAL] Non-resolvable parent POM for ***: Failure to find * ...
- [LeetCode] Find Right Interval 找右区间
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout
$interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...
- MySQL interval()函数
INTERVAL(N,N1,N2,N3,..........) INTERVAL()函数进行比较列表(N,N1,N2,N3等等)中的N值.该函数如果N<N1返回0,如果N<N2返回1,如果 ...
随机推荐
- redis存json数据时选择string还是hash
redis存json数据时选择string还是hash 我们在缓存json数据到redis时经常会面临是选择string类型还是选择hash类型去存储.接下来我从占用空间和IO两方面来分析这两种类型的 ...
- EGADS框架处理流程分析
最近在搞异常检测相关的工作,因此调研了业界常用的异常检测系统.通过查阅相关资料,发现业界对雅虎开源的EGADS系统评价比较高,其git项目已有980个star.这周阅读了项目的源码,梳理了系统框架的基 ...
- redis持久化怎么选?成年人从来不做选择...
前言 面试官:你知道 redis 是的怎么做持久化的吗? 我:我知道 redis 有两种方式,一种是 RDB,一种是 AOF. 面试官:那这两种方式具体是怎么做的,它们的区别是什么,生产环境中到底应该 ...
- 【Spring】Spring中的Bean - 1、Baen配置
Bean配置 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 什么是Spring中的Bean? Spring可以被看作是一个 ...
- 【Oracle】用sqlplus登录的各种方式
1.本地登录 sqlplus / as sysdba 2.账号密码登录 sqlplus user/passwd 3.选择实例登录 sqlplus user/passwd@实例名 例如 sqlplu ...
- PKU2186 Popular Cows 受欢迎的牛
题目描述 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N(N<=10000)头牛,给你M(M<=50000)对整数(A,B),表示牛A认为牛B受欢迎.这种关系是具有传递性的,如果A认为B ...
- Zju1100 Mondriaan
题目描述 有一个m行n列的矩阵,用1*2的骨牌(可横放或竖放)完全覆盖,骨牌不能重叠,有多少种不同的覆盖的方法? 你只需要求出覆盖方法总数mod p的值即可. 输入格式 三个整数数n,m,p,m< ...
- HTML基础复习3
CSS 可以理解为对HTML的一种补充 CSS由两部分组成:选择器.声明,声明中包含属性和值 CSS中的选择器 HTML标签选择器 类选择器 在标签上使用class属性为标签起个类名,在CSS中使用. ...
- SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
作为后台服务开发,在日常工作中我们天天都在跟数据库打交道,一直在进行各种CRUD操作,都会使用到数据库连接池.按照发展历程,业界知名的数据库连接池有以下几种:c3p0.DBCP.Tomcat JDBC ...
- ctfshow_djb杯
桐桑又开始摸鱼了 ctfshow的比赛整的一手好活.djb杯. web1_veryphp 打开就是源码: 1 <?php 2 error_reporting(0); 3 highlight_fi ...