Coderforces 204A
2012-08-10 10:58
141 查看
此题比赛时没有好的方法不会做,最后从别人那里学到了一个好的方法,就是从小到大慢慢数,从11到99开始数数,直到l < 100...1 < r < 9000..9
当加到这一步时,输出count就是所求结果,比如 47 1024 首先是两位数的 11 到99 ,47在里面 ,此时数目是5,再到三位数,101 到 999,此时1024大于他们
数目count直接加上9*10(101 到191共10个),接着是四位数,1001 到 9999 ,1024在里面,此事要计算1001到1024首尾相同的数的个数,总共有3个,所以
总数就是98个。
View Code
当加到这一步时,输出count就是所求结果,比如 47 1024 首先是两位数的 11 到99 ,47在里面 ,此时数目是5,再到三位数,101 到 999,此时1024大于他们
数目count直接加上9*10(101 到191共10个),接着是四位数,1001 到 9999 ,1024在里面,此事要计算1001到1024首尾相同的数的个数,总共有3个,所以
总数就是98个。
View Code
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> using namespace std; int main() { int i, j; long long a, b; long long x, l, r, count, num; count = 0LL; x = 10; cin>>l>>r; for(i=1; i<=9; i++) { if(l<=i && i<=r) count++; } for(int len=2; len<=18; len++)//长度从小到大递增 { for(int d=1; d<=9; d++)//每个长度的数,都要计算count个数 { a = d*x + d; b = (d + 1)*x - 10 + d; long long ln = max(a,l); long long rn = min(b,r); if(ln <= rn) { long long l0 = ln/10; if(ln%10 > d)//比如上面的r == 47时 l0++; long long r0 = rn/10; if(rn%10 < d) r0--; count += r0 - l0 + 1; } } x = x*10; } printf("%I64d\n",count); //system("pause"); return 0; }
相关文章推荐
- 文章标题 coderforces 609E : Minimum spanning tree for each edge (MST+LCA)
- CoderForces 632B Alice, Bob, Two Teams
- 文章标题 coderforces 755C : PolandBall and Forest(并查集)
- 文章标题 coderforces 148D : Bag of mice (概率DP)
- Coderforces Bit++
- coderforces 335B--Palindrome [字符串好题]
- coderforces 348A Mafia(二分)
- Coderforces 348A
- 【CoderForces】#296 Div2 C(线段树+set)
- Coderforces Football
- Coderforces Boy or Girl
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- coderforces 702C Cellular Network(二分)
- Coderforces 374 (Div2)
- Beaver's Calculator coderforces 70A1 蓝桥杯
- 【DP】coderforces 567F
- Coderforces Taxi
- Coderforces 242E XOR on Segment
- 文章标题 Coderforces 343D : Water Tree(dfs序+线段树)
- coderforces 702A Maximum Increase