Luo Gu P3286 [SCOI2014] Fang Bobo mall tour

Title: Luo Gu P3286 [SCOI2014] Fang Bobo mall tour

Thinking

Digital DP

dalao say that this is the number of bits dp water problem , really I was too dishes ...
they are impossible to come out. This question is at the lectures as examples, probably understand the thinking and simply repeat it.
First of all, according to the scope and meaning of the questions section, it is easy to see digital dp.
However, conventional and digital dp different, we do not know the final assembly point for each digit.
So we might as well all handpicked stones eventually gathered in the lowest position (first). The total cost of this case referred to as \ (cost \) , may be obtained by a simple digital dp.
But this is clearly not the optimal solution, and some number of stones gathered in a higher position for less expense. So we moved bit by bit.
For example, digital \ (i \) , stones are now gathered in the first (least significant bit), we should move to second place stones, then \ (i \) to reduce the cost of a higher position and a second position to move, \ (i \) of the first movement cost increase, similar to the change in the root dp dp tree.
With \ (delta (i) \) represents a number of stones from the poly i first moved in together in second place, spent a price reduction (this sentence a bit long).
When (delta (i) <0 \ ) \ , it indicates that the cost of spending is not reduced, but increased, then we do not move stones i, otherwise i put the stones were moved to the second place. Based on the above operation, the total cost is now \ (cost- \ sum_ {I} = L ^ R & lt \ max (Delta (I), 0) \) .
The formula\ (\ sum_ {i = l } ^ r \ max (delta (i), 0) \) can be reused a number of computing dp bit (similar to the second transducer root of dp dp).
Just now we can move the stones are displaced from the first to the second position, then after the above-described method, the stone can be shifted from third to second displacement, displaced from the third to the fourth end, all ... stones were moved to the optimum position
.
It is understood in conjunction with the code.


Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll l,r,k,a[80],dp[80][4000][2][2];
ll dfs(int pos,int sum,bool lead,bool limit){
    if(pos==0) return sum;
    ll &ans=dp[pos][sum][lead][limit];
    if(ans!=-1) return ans;
    ans=0;
    for(int i=0,up=limit?a[pos]:k-1;i<=up;++i){
        if(!i&&lead) ans+=dfs(pos-1,0,true,limit&&i==a[pos]);
        else ans+=dfs(pos-1,sum+i*(pos-1),false,limit&&i==a[pos]);
    }
    return ans;
}
ll dfs(int to,int pos,int sum,bool lead,bool limit){
    if(pos==0) return max(sum,0);
    ll &ans=dp[pos][sum+500][lead][limit];
    if(ans!=-1) return ans;
    ans=0;
    for(int i=0,up=limit?a[pos]:k-1;i<=up;++i){
        if(!i&&lead) ans+=dfs(to,pos-1,0,true,limit&&i==a[pos]);
        else if(pos>=to) ans+=dfs(to,pos-1,sum+i,false,limit&&i==a[pos]);
        else ans+=dfs(to,pos-1,sum-i,false,limit&&i==a[pos]); 
    }
    return ans;
}
ll solve(ll num){
    memset(dp,-1,sizeof(dp));
    int len=0;
    while(num) a[++len]=num%k,num/=k;
    ll res=dfs(len,0,true,true);
    for(int i=2;i<=len;++i){
        memset(dp,-1,sizeof(dp));
        res-=dfs(i,len,0,true,true);
    }
    return res;
}
int main(){
    scanf("%lld%lld%d",&l,&r,&k);
    printf("%lld\n",solve(r)-solve(l-1));
    return 0;
}

Guess you like

Origin www.cnblogs.com/yu-xing/p/11311698.html