CodeForces 204A - Little Elephant and Interval 算答案..易错..暴力打表对拍
2013-08-05 13:13
447 查看
题意:
找出[l,r]范围内..第一位数字和最后一位数字相同的数的个数...1<=l<=r<=10^18
题解:
这个显然要找出一些关系和联系..推出1~(l-1)...与 1~r 的符合条件的个数..后者再减去前者..为了方便处理.先将数字转为字符串..sprintf就好了...举个例子..如要找1~3021内符合条件的个数..3021是长度为4的十进制数..那么把1~3位的所有算进来..一共9+9+10*9=108...在把1000~2999的加进来..2*100...再把千位为3的加进来.由于最有一位小于3..所以要向前借一位..所以有2个..那么答案就是108+200+2=310个...
我在写这题的时候就出现了很多细节错误..所以写了个暴力的来对拍..很好使!
Program:
找出[l,r]范围内..第一位数字和最后一位数字相同的数的个数...1<=l<=r<=10^18
题解:
这个显然要找出一些关系和联系..推出1~(l-1)...与 1~r 的符合条件的个数..后者再减去前者..为了方便处理.先将数字转为字符串..sprintf就好了...举个例子..如要找1~3021内符合条件的个数..3021是长度为4的十进制数..那么把1~3位的所有算进来..一共9+9+10*9=108...在把1000~2999的加进来..2*100...再把千位为3的加进来.由于最有一位小于3..所以要向前借一位..所以有2个..那么答案就是108+200+2=310个...
我在写这题的时候就出现了很多细节错误..所以写了个暴力的来对拍..很好使!
Program:
#include<iostream> #include<stdio.h> #include<cmath> #include<string.h> #include<stack> #include<queue> #include<algorithm> #define oo 1000000007 #define ll long long #define MAXN 1000005 using namespace std; char s[25]; ll pow10[20]; ll cnt(ll x) { int len,i; ll ans=0,t,p; sprintf(s+1,"%I64d",x); len=strlen(s+1); for (int i=1;i<len;i++) { t=i-2; if (t<0) t=1; else t=pow10[t]; ans+=9*t; } if (len-2>=0) t=pow10[len-2]; else t=1; ans+=(s[1]-'1')*t; p=0; for (i=2;i<len;i++) p=p*10+(s[i]-'0'); if (s[1]>s[len]) p--; ans+=p+1; return ans; } int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); ll l,r; pow10[0]=1; for (int i=1;i<=18;i++) pow10[i]=pow10[i-1]*10; while (~scanf("%I64d%I64d",&l,&r)) printf("%I64d\n",cnt(r)-cnt(l-1)); return 0; }
相关文章推荐
- codeforces 204A Little Elephant and Interval 美丽的区间转换
- codeforces 204(Div.1 A) Little Elephant and Interval(贪心)
- codeforces 204(Div.1 A) Little Elephant and Interval(贪心)
- CF 205 CLittle Elephant and Interval 分类讨论(计数) D(暴力,水题)
- CodeForces 204B - Little Elephant and Cards
- codeforces 221D Little Elephant and Array
- Codeforces 221 A. Little Elephant and Function
- Codeforces 221 E. Little Elephant and Shifts
- [Codeforces 204C] Little Elephant and Furik and Rubik (期望)
- [广义后缀自动机] BZOJ 3473 字符串 & BZOJ 3277 串 & Codeforces 204E #129 (Div. 1) E. Little Elephant and Strings
- Codeforces 221 B. Little Elephant and Numbers
- Codeforces-258D:Little Elephant and Broken Sorting(概率DP)
- CodeForces 220B Little Elephant and Array 莫队算法
- Codeforces 221 C. Little Elephant and Problem
- Codeforces 221 D. Little Elephant and Array
- Little Elephant and Array - CodeForces 220 B 树状数组
- CodeForces 220A Little Elephant and Problem
- CodeForces-204E:Little Elephant and Strings (广义后缀自动机求出现次数)
- codeforces 204B- Little Elephant and Cards
- 线段树 CodeForces 220B - Little Elephant and Array