【LuoGu题解】 P1362 【兔子数】

依题意模拟暴力打表找规律,注意到:符合题意的数中只包含(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;
}

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