USACO 2014 US Open Odometer /// 枚举

题目大意:

给定区间 l r

求区间包含多少个数 它们各个位的数只有一个不一样

注意 多个位但多个数为0单个数为x的情况

这种情况只有 x000 即把单个数放在首位才是正确的

同样注意 多个位但单个数为0多个数为x的情况

这种情况要注意避免 0xxx 的出现

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define mem(i,j) memset(i,j,sizeof(i))
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define gcd(i,j) __gcd(i,j);
const int N=1e5+5;
const int mod=1e9+7;
const double eps=1e-8;
 
LL l,r;
bool get(LL a,int w,LL b,int s) {
    LL res=0;
    while(--w) res=res*10+b, s--;
    res=res*10+a;
    while(--s) res=res*10+b;
    return l<=res && res<=r;
} // 得到 共s位第w位为a其他位为b的数 判断是否在l r区间内
int main()
{
    scanf("%lld%lld",&l,&r);
    int S=(int)log10((double)(max(l,r)))+1;
    LL ans=0;
    for(int s=3;s<=S;s++)  // 枚举位数
        for(LL i=0;i<=9;i++) // 枚举多个的数
            for(LL j=0;j<=9;j++) if(i!=j) { // 枚举一个的数
                if(i==0) {
                    if(get(j,1,0,s)) ans++;
                } else {
                    for(int w=1+(j==0);w<=s;w++) 
                        if(get(j,w,i,s)) ans++;// +(j==0)避免出现0222这种数
                }
            }
    printf("%lld
",ans);

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zquzjx/p/10549153.html