CodeForces 710E Generate a String (DP)

分奇偶讨论,注意奇数时候可以往上一位再除二

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e7+10;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
    if(y==0) return x;
    return gcd(y,x%y);
}
int n,x,y;
ll dp[maxn];
int main(){
    cin>>n>>x>>y;
    dp[0]=0;
    rep(i,1,maxn){
        dp[i]=dp[i-1]+x;
        if(i&1) dp[i]=min(dp[i],dp[(i+1)/2]+x+y);
        else dp[i]=min(dp[i/2]+y,dp[i]);
    }
    cout<<dp[n]<<"\n";
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/90057100