Ural 1698. Square Country 5 记忆化搜索

1698. Square Country 5

Time limit: 2.0 second
Memory limit: 64 MB
The first arithmetical operation taught to the children of the Square country is the calculation of squares of positive integers. At the first lesson the children are provided with “easy” numbers, calculating a square of which can be done by writing a few digits in front of them (i.e. 76 is an easy number because 76 2 = 5776). Of course, the numbers cannot contain leading zeroes. The task shouldn't be too difficult, so the easy numbers shouldn't contain more than  n digits. How many different easy numbers can teachers prepare for the first lesson?

Input

The only input line contains an integer  n  (1 ≤  n ≤ 2000) , the maximal length of the easy number the children can be provided with.

Output

Output the number of different easy numbers consisting of at most  n digits.

Sample

input output
1
3
Problem Author: Alex Samsonov
Problem Source: Ural SU Contest. Petrozavodsk Winter Session, February 2009
方块国的孩子们学到的第一个算术运算是正整数的平方计算.孩子们首先学的是那些"简单"数字的平方计算.所谓简单数字就是那些只需要在原数后加几个数字就可得到原数平方的数字(例如,76是简单数字,因为762=5776).当然,所有`数字都不能包含前导0.平方运算不能太麻烦,所以简单数字都不能超过n位.老师们总共可以准备多少简单数字? 

#include <stdio.h>
#include <math.h>
int n, ans;
int a[2005];
int b[2005];

bool check(int k)
{
    b[k] = 0;
    for (int i = 0; i <= k; ++i)
        b[k] += a[i] * a[k - i];
    if (k)
        b[k] += b[k - 1] / 10;
    return b[k] % 10 == a[k];
}

void dfs(int k)
{
    if (k >= n)
        return;
    for (int i = 9; i >= 0; --i)
    {
        a[k] = i;
        if (check(k))
        {
            if (a[k])
            {
                ans++;
            }
            dfs(k + 1);
        }
    }
}

int main()
{
    scanf("%d", &n);
    dfs(0);
    printf("%d\n", ans);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值