【CYH-02】NOIp考砸后虐题赛:函数:题解

这道题貌似只有@AKEE 大佬A掉,恭喜!

还有因为c++中支持两个参数数量不同的相同名称的函数调用,所以当时就没改成两个函数,这里表示抱歉。

这道题可直接用指针+hash一下,然后就模拟即可。

代码:

#include<bits/stdc++.h>
using namespace std;
const int Mo=10000000;
struct node 
{
    long long int state,ans;
    node* next;
}*Hash[Mo+10],*p;
long long max(long long a,long long b,long long c,long long d,long long e) 
{
    return max(a,max(b,max(c,max(d,e))));
}
long long max(long long a,long long b,long long c,long long d) 
{
    return max(a,max(b,max(c,d)));
}
long long f(long long x) 
{
    if(x==0)return 0;
    p=Hash[x%Mo];
    while(p!=NULL)
    {
        if(p->state==x)return p->ans;
        p=p->next;
    }
    long long anss=max(x,f(x/2)+f(x/3)+f(x/8)+f(x/9));
    p=new node;
    p->state=x;
    p->ans=anss;
    p->next=Hash[x%Mo];
    Hash[x%Mo]=p;
    return anss;
}
long long f(long long a,long long b) 
{
    long long anss=max(a+b,f(a/2)+f(a/3)+f(a/8)+f(a/9)+b,f(b/2)+f(b/3)+f(b/8)+f(b/9)+a,f(b/2)+f(b/3)+f(b/8)+f(b/9)+f(a/2)+f(a/3)+f(a/8)+f(a/9));
    return anss;
}
int main() 
{
	//freopen("function.in","r",stdin);
	//freopen("function.out","w",stdout);
    long long int a,b;
    while(cin>>a>>b) 
    {
        cout<<f(a,b)<<endl;
    }
    return 0;
}

另附AKEE大佬代码:(%%%)

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unordered_map>
#include <algorithm>

using namespace std;

typedef long long ll;
const int MAXN=10000005;

ll n,m,f[MAXN];
unordered_map<ll,ll> has;
ll solve(ll n)
{
    if(n<=10000000)return f[n];
    if(has.count(n))return has[n];
    return has[n]=max(solve(n/2)+solve(n/3)+solve(n/8)+solve(n/9),n);
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("code.in","r",stdin);
    //freopen("code.out","w",stdout);
    #endif
    f[0]=0;
    for(int i=1;i<=10000000;i++)
    	f[i]=max(f[i/2]+f[i/3]+f[i/8]+f[i/9],i*1ll);
    while(cin>>n>>m)
    	cout<<solve(n)+solve(m)<<endl;
    return 0;
}
posted @ 2018-12-09 15:14  vercont  阅读(148)  评论(0编辑  收藏  举报