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

input output
1
3

题意:

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

输入

只有一个数字n(1<=n<=2000),表示给孩子们的简单数字的最大长度.

输出

输出不超过n位的简单数字个数.

[编辑]样例

[编辑]样例输入

1

[编辑]样例输出

3

思路:

进行记忆化搜索,利用大数乘的方法,寻找自守数;

前道知识:

如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数。
显然,5和6是一位自守数(5x5=25 6x6=36)
25x25=625 76x76=5776,所以25和76是两位自守数。
自守数有一个特性,以他为后几位的两个数相乘,乘积的后几位仍是这个自守数。因为5时自守数,所以以5为个位数的两个数相乘,乘积的个位仍然是5;76是自守数,所以以76为后两位数的两个数相乘,其结果的后两位仍是76,如176x576=101376。
虽然0和1的平方的个位数仍然是0和1,但是他们太“平凡”了,研究他们没有意义,所以不算自守数。
三位自守数是625和376,四位自守数是0625和9376,五位自守数是90625和09376......
我们可以看到,(n+1)位的自守数出自n位的自守数。由此得出,如果知道n位的自守数a,那么(n+1)位的自守数应当由a前面加上一个数构成。
实际上,简化一下,还能发现如下规律:
5+6=11
25+76=101
625+376=1001
......
所以,两个n位自守数,他们的和等于10^n+1
 
代码:
 #include <iostream>
#include <string>
#include<cstring>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdio> using namespace std;
int n, ans;
int a[];
int b[]; bool check(int k)
{//计算平方后的第k+1位的数值;
b[k] = ;
for (int i = ; i <= k; ++i)
b[k] += a[i] * a[k - i];
if (k)
b[k] += b[k - ] / ;
return b[k] % == a[k];
} void dfs(int k)
{
if (k >= n)
return;
for (int i=;i>=;--i)
{
a[k] = i;
if (check(k))
{
if (a[k])
{//第k+1位数不是0,表示产生了新的自守数;
ans++;
}
dfs(k + );
}
}
} int main()
{
scanf("%d", &n);
dfs();
printf("%d\n", ans);
}

ural 1698. Square Country 5(记忆化搜索)的更多相关文章

  1. URAL 1698. Square Country 5(记忆化搜索)

    题目链接 题意 : 自守数的定义:如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数.例如5*5=25,则5就是自守数.让你求不超过n位的自守数有多少 思路 : 实际上,自守数还有两个性质 ...

  2. URAL 1501. Sense of Beauty(记忆化搜索)

    题目链接 本来暴力写个TLE了,加上记忆化就A了. #include <cstring> #include <cstdio> #include <string> # ...

  3. URAL 1152. False Mirrors (记忆化搜索 状压DP)

    题目链接 题意 : 每一颗子弹破坏了三个邻近的阳台.(第N个阳台是与第1个相邻)射击后后的生存的怪物都对主角造成伤害- 如此,直到所有的怪物被消灭,求怎样射击才能受到最少伤害. 思路 : 状压,数据不 ...

  4. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  5. 记忆化搜索(DFS+DP) URAL 1223 Chernobyl’ Eagle on a Roof

    题目传送门 /* 记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数 ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + ...

  6. 记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty

    题目传送门 /* 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操 ...

  7. hdu 4856 Tunnels (记忆化搜索)

    Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  9. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

随机推荐

  1. Swift3GCD

    GCD的使用在Swift3中的方法 //串行队列 let q:DispatchQueue = DispatchQueue(label: "xiaosi") //并发队列 qos : ...

  2. ios开发判断手机是否安装微信app

    1.代码如下 if ([WXApi isWXAppInstalled]) 2.如果以上代码无效,请在plist文件中添加如下内容

  3. Openjudge-NOI题库-变幻的矩阵

     题目描述 Description 有一个N x N(N为奇数,且1 <= N <= 10)的矩阵,矩阵中的元素都是字符.这个矩阵可能会按照如下的几种变幻法则之一进行变幻(只会变幻一次). ...

  4. centos php nginx 添加到service

    1. nginx A. # vi /etc/init.d/nginx B. #!/bin/sh # Comments to support chkconfig on RedHat Linux # ch ...

  5. html绑定

    目的 html绑定可以绑定DOM元素内的HTML内容. 示例: <div data-bind="html: details"></div> <scri ...

  6. 5、sha1加密的一个坑

    OC语言写的sha1加密算法,在网上随手可以搜索到(如下便是),但是我不得不说有一些人不责任,没有提醒大家导入必要的系统头文件,从而导致错误 + (NSString *) sha1:(NSString ...

  7. jetty启动https

    <Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- if ...

  8. MapReduce库类

    Hadoop除了可以让开发人员自行编写map函数和reduce函数,还提供一些常用函数(mapper.reducer和partitioner)的类库,这些类位于 org.apache.hadoop.m ...

  9. linux: 几个常用makefile模板

    不才,总结个人常用makefile模板,以备后用. 1.编译动态库 ############################################################# # Ma ...

  10. bootstrap如何自定义5等分

    根据bootstrap源码改的1比5的栅格系统 /*5等分媒体查询样式begin*/ .col-xs-1-5,.col-sm-1-5,.col-md-1-5,.col-lg-1-5,.col-xs-4 ...