每日算法

每日算法

those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

2020.3.12


luogu -P1028 数得计算

记忆化

记忆化的思考方式: 主要就是从搜索开始进行思考然发现哪些分枝会大量用到重复的部分 我们只需要将其进行记录下次直接访问即可

#include <iostream>
#include <algorithm>

using namespace std;
const int N = 100001;
int n;long long f[N];
int dfs(int mid)
{
	if(f[mid] != 0)return f[mid];
	for(int i = 1;i <= mid;i ++)
	{
		f[mid] += dfs(i / 2) + 1;
	}
	return f[mid];
}
int main()
{
	cin >> n;f[1] = 1;
	cout << dfs(n / 2) + 1;
	return 0;
} 

dp做法 (一重循环搞定)

dp的做法f[当前总方案数量] = f[前一次方案数量] + f[当前数字的 / 2 的方案数] + 1

即 f[i] = f[i - 1] + f[i / 2] + 1

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100001;
int n; long long f[N];
int main()
{
	cin >> n;
	for(int i = 1;i <= n / 2 ;i ++)	
	{
		f[i] = f[i - 1] + f[i / 2] + 1;
	}
	cout << f[n / 2] + 1 << endl;
	return 0;
} 

luogu -P2437 蜜蜂路线

递推 + 斐波那契数列

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;
const int N = 10010;

string f[N];
int n , m; // m ---> n 
string add(string a,string b)
{
	int la = a.length(),lb = b.length();
	if(la >= lb)
	{
		for(int i = 0;i < la - lb;i++)b = "0" + b;
	}else{
		for(int i = 0;i < lb - la;i++)a = "0" + a;
	}
	
	string ans;
	int j = 0;
	for(int i = max(la,lb) - 1;i >= 0 ;i--)
	{
		int sum = a[i] - '0' + b[i] - '0' + j;
		j = 0;
		while(sum - 10 >= 0)
		{
			sum -= 10;j++;	
		} 
		ans += to_string(sum);
	}
	reverse(ans.begin(),ans.end());
	if(j > 0)ans = to_string(j) + ans;
	return ans;
}
int main()
{
	
	cin >> m >> n;
	f[1] = "1";f[2] = "1";
	for(int i = 3;i <= n;i ++)
	{
		f[i] = add(f[i-1],f[i-2]);
	}		
	
	cout << f[n - m + 1];
	return 0;
} 
原文地址:https://www.cnblogs.com/wlw-x/p/12483538.html