码迷,mamicode.com
首页 > 其他好文 > 详细

【LuoGu题解】 P1362 【兔子数】

时间:2019-10-30 22:42:41      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:cst   注意   观察   std   ==   题意   res   判断   找规律   

依题意模拟暴力打表找规律,注意到:符合题意的数中只包含\(0,1,2,3\),大于\(3\)的数,平方后都会进位,进位导致\(S(x)*S(x)<S(x*x)\)

观察数据范围,最大满足题意的数字有\(10\)位,那么我们枚举每一位上的数字,然后暴力判断是否为兔子数就行了。

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define Mi return
#define manchi 0

using namespace std;

inline int read()
{
    int num=0,w=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    while(isdigit(ch)) num=(num<<1)+(num<<3)+ch-'0',ch=getchar();
    Mi num*w;
}

int l,r,ans;
long long tmp1,tmp2;

inline int cal(long long x)
{
    int res=0;
    while(x)
    {
        res+=x%10;
        x/=10;
    }
    return res;
}

int main()
{
    l=read();r=read();
    for(int a1=0;a1<=3;a1++)
        for(int a2=0;a2<=3;a2++)
            for(int a3=0;a3<=3;a3++)
                for(int a4=0;a4<=3;a4++)
                    for(int a5=0;a5<=3;a5++)
                        for(int a6=0;a6<=3;a6++)
                            for(int a7=0;a7<=3;a7++)
                                for(int a8=0;a8<=3;a8++)
                                    for(int a9=0;a9<=3;a9++)
                                        for(int a10=0;a10<=3;a10++)
                                        {
                                            tmp1=a1*1e8+a2*1e7+a3*1e6+a4*1e5+a5*1e4+a6*1e3+a7*1e2+a8*10+a9+a10*1e9;
                                            if(tmp1<l || tmp1>r) continue;
                                            tmp2=1LL*tmp1*tmp1;
                                            if(cal(tmp1)*cal(tmp1)==cal(tmp2)) ans++;
                                        }
    printf("%d",ans);
    Mi manchi;
}

【LuoGu题解】 P1362 【兔子数】

标签:cst   注意   观察   std   ==   题意   res   判断   找规律   

原文地址:https://www.cnblogs.com/fengzi8615/p/11768274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!