(打表)P1362 兔子数

P1362 兔子数

Luogu

应用 

题库训练比赛记录讨论

    • 286通过
    • 962提交
  • 题目提供者yeszy 管理员
  • 评测方式云端评测
  • 标签高性能
  • 难度普及/提高-
  • 时空限制1000ms / 128MB

 提交  题解   

  • 提示:收藏到任务计划后,可在首页查看。

最新讨论显示

推荐的相关题目显示

题目描述

设 S(N ) 表示 N 的各位数字之和,如 S(484) = 4+8+4 = 16, S(22) = 2+2 = 4。如果一个正整数满足 S(x*x) = S(x) *S(x),我们称之为 Rabbit N umber。比方说,22 就是一个 Rabbit N umber,因为 S(484) = S(22) *S(22)。

现在,给出一个区间 [L, R],求在该区间内的 Rabbit N umber 的个数。

输入输出格式

输入格式:

输入仅一行,为空格隔开的两个数 L 和 R。

输出格式:

输出仅一行一个整数,表示所求 Rabbit N umber 的个数。

输入输出样例

输入样例#1: 复制

样例1:22 22

样例2:484 484

样例3:1 58

样例4:58 484

样例5:1000000000 1000000000

输出样例#1: 复制

样例1:1

样例2:0

样例3:12

样例4:24

样例5:1

说明

1 <= L <= R <= 10^9

​
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=5e5+5;
ll a[]={0,1,2,3},cut=0,l,r;
ll mapp[N],p=0;
ll fun(ll n){
    ll ans=0;
    while(n){
        ans+=n%10;
        n/=10;
    }
    return ans;
}
/*int main(){            //  解法一
    ll l,r,leap=0;
    ll ans=0;
    scanf("%lld %lld",&l,&r);
    ll sum=0;
    for(int i0=0;i0<4&&!leap;i0++){
        for(int i1=0;i1<4&&!leap;i1++){
            for(int i2=0;i2<4&&!leap;i2++){
                for(int i3=0;i3<4&&!leap;i3++){
                    for(int i4=0;i4<4&&!leap;i4++){
                        for(int i5=0;i5<4&&!leap;i5++){
                            for(int i6=0;i6<4&&!leap;i6++){
                                for(int i7=0;i7<4&&!leap;i7++){
                                    for(int i8=0;i8<4&&!leap;i8++){
                                        for(int i9=0;i9<4&&!leap;i9++){
                                            ans=1000000000*a[i0]+100000000*a[i1]+10000000*a[i2]+1000000*a[i3]+100000*a[i4]+10000*a[i5]+1000*a[i6]+100*a[i7]+10*a[i8]+a[i9];
                                            if(ans<l||ans>r) continue;
                                            if(fun(ans*ans)==fun(ans)*fun(ans))
                                                sum++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    printf("%lld\n",sum);
    return 0;
}*/

void dfs(ll sum,ll ans){     //  解法二
    if(sum>=l&&sum<=r)
        if(fun(sum*sum)==fun(sum)*fun(sum))
            mapp[p++]=sum;
    if(ans>r) return ;
    for(int i=0;i<4;i++)
        dfs(sum*10+a[i],ans*10);
}

int main(){
    scanf("%lld %lld",&l,&r);
    dfs(0,1);
    sort(mapp,mapp+p);
    for(int i=0;i<p;i++){
        if(i==0)
            cut++;
        else{
            if(mapp[i]!=mapp[i-1])
               cut++;
        }
    }
    printf("%lld\n",cut);
    return 0;
}

​

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/87992335
今日推荐