标签:codeforces dp 组合数学
给出一个l和r,求取在l和r之间的首尾相同的数的个数。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 27
using 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) );
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 204A A. Little Elephant and Interval(dp+组合数学)
标签:codeforces dp 组合数学
原文地址:http://blog.csdn.net/qq_24451605/article/details/48517907