https://www.luogu.org/problem/show?pid=1362
我看到的时候一点头绪都没有汗
Number 这题告诉你s(A*A)=s(A)*s(A)且A*A<10^18
所以s(A*A)<18*9<13*13 即s(A)<13
搜索+适当剪枝即可
证明一个定理:
rabbit number的各位数字一定<=3
若某数字x的一位,a>=4
那么它在该位的贡献是a^2的
而在x中该位自乘进了一位
故贡献为 a^2/10+a^2%10
#include<bits/stdc++.h>
#define Ll long long
using namespace std;
int a[10];
int ans,l,r;
int get(Ll a){
int ans=0;
for(;a;a/=10)ans+=a%10;
return ans;
}
void check(){
Ll temp=0;
for(int i=1;i<=9;i++)temp=temp*10+a[i];
if(l<=temp&&temp<=r&&get(temp)*get(temp)==get(temp*temp))ans++;
}
void dfs(int x){
if(x>9){check();return;}
for(int i=0;i<=3;i++)a[x]=i,dfs(x+1);
}
int main()
{
scanf("%d%d",&l,&r);
dfs(1);
if(r==1e9)ans++;
printf("%d",ans);
}