Ural 1698. Square Country 5 记忆化搜索

来源:互联网 发布:期货数据库 编辑:程序博客网 时间:2024/06/11 17:38

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 762 = 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

inputoutput
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);}


原创粉丝点击