wikioi天梯 1011 数的计算 (记忆化递归)

题目描述 Description

我们要求找出具有下列性质数的个数(包含输入的自然数n):

先输入一个自然数n(n<=1000),然后对此自然数按照如下方法进行处理:

1.          不作任何处理;

2.          在它的左边加上一个自然数,但该自然数不能超过原数的一半;

3.          加上数后,继续按此规则进行处理,直到不能再加自然数为止.

输入描述 Input Description

一个数n

输出描述 Output Description

满足条件的数的个数

样例输入 Sample Input

6

样例输出 Sample Output

6

数据范围及提示 Data Size & Hint

6个数分别是:

6

16

26

126

36

136

好久不做记忆化递归的题了,刷天梯正好遇到这题……

刚开始想递归还怕超时,后面感觉记忆化应该可以,然后就写了,试了最大的n=1000秒出才放心……

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<bitset>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define llson j<<1,l,mid
#define rrson j<<1|1,mid+1,r
#define INF 1000000007
#define seed 13131
#define seed1 1313
#define maxn 500005
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll a[1005];
ll dfs(int n)
{
    if(!n||n==1) return 0;
    if(a[n]) return a[n];
    ll sum=n/2;
    for(int i=n/2;i>1;i--)
        sum+=(a[i]=dfs(i));
    return sum;
}
int main()
{
    int n;
    cin>>n;
    cout<<dfs(n)+1<<endl;
    return 0;
}
时间: 2024-08-01 02:41:50

wikioi天梯 1011 数的计算 (记忆化递归)的相关文章

1011数的计算

1011 数的计算 2001年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n<=1000),然后对此自然数按照如下方法进行处理: 1.          不作任何处理; 2.          在它的左边加上一个自然数,但该自然数不能超过原数的一半; 3.          加上数后,继续按此规则进行处理,直到不能

POJ 1579 Function Run Fun 记忆化递归

典型的记忆化递归问题. 这类问题的记忆主要是利用数组记忆.那么已经计算过的值就能够直接返回.不须要进一步递归了. 注意:下标越界.递归顺序不能错,及时推断是否已经计算过值了,不要多递归. 或者直接使用动态规划法填好表也是能够的. #include <stdio.h> #include <limits.h> const int MAX_N = 21; int W[MAX_N][MAX_N][MAX_N]; int getValue(int a, int b, int c) { if

Bailian1664 Placing apples【递推+记忆化递归】

1664:Placing apples 总时间限制: 1000ms 内存限制: 65536kB 描述 We are going to place M same apples into N same plates. There could be some empty plates. How many methods do we have? When we have 7 applesand 3 plates, the methods, (1, 5, 1) and (5, 1, 1) are the

【动态规划】【记忆化搜索】CODEVS 1011 数的计算 2001年NOIP全国联赛普及组

设答案为f(n),我们显然可以暴力地递归求解: f(n)=f(1)+f(2)+……+f(n/2). 但是n=1000,显然会超时. 考虑状态最多可能会有n种,经过大量的重复计算,所以可以记忆下来,减少不必要的计算. 1 #include<cstdio> 2 using namespace std; 3 int n; 4 long long memory[1001]; 5 long long f(int cur) 6 { 7 if(memory[cur]) return memory[cur];

luogu P2657 [SCOI2009]windy数 数位dp 记忆化搜索

题目链接 luogu P2657 [SCOI2009]windy数 题解 我有了一种所有数位dp都能用记忆话搜索水的错觉 代码 #include<cstdio> #include<algorithm> inline int read() { int x = 0,f = 1; char c = getchar(); while(c < '0' || c > '9') c = getchar(); while(c <= '9' && c >= '

1011 数的计算

题目描述 Description 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n<=1000),然后对此自然数按照如下方法进行处理: 1.          不作任何处理; 2.          在它的左边加上一个自然数,但该自然数不能超过原数的一半; 3.          加上数后,继续按此规则进行处理,直到不能再加自然数为止. 输入描述 Input Description 一个数n 输出描述 Output Description 满足条件的数的个数 样例

lightoj 1011 Marriage Ceremonies (暴力)(记忆化dp,状压) 转

转载自:http://blog.csdn.net/xindoo/article/details/9173949 暴力TLE 1 #include <iostream> 2 #include <string.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 int vis[17]; 8 int n, ans, sum; 9 int map[19][19]; 10 11 void dfs(int x) 12 { 1

P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs

题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵循的穿越路线来确保奶牛的乐趣.为了实现这个让奶牛在牛棚里来回穿梭的方案,FJ在第i号隔间上张贴了一个“下一个隔间”Next_i(1<=Next_i<=N),告诉奶牛要去的下一个隔间:这样,为了收集它们的糖果,奶牛就会在牛棚里来回穿梭了. FJ命令奶牛i应该从i号隔间开始收集糖果.如果一只奶牛回到某

搜索分析(DFS、BFS、递归、记忆化搜索)

搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2)递归(从中间向两边) 1 //递归一定要写成记忆化递归 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool vis[11]; 5 int count1=0; 6 7 void search(int n){ 8 count1++; 9