Skip to content

Instantly share code, notes, and snippets.

@balamark
Created April 13, 2015 14:10
Show Gist options
  • Save balamark/a8b7c6efe7dbfd64414d to your computer and use it in GitHub Desktop.
Save balamark/a8b7c6efe7dbfd64414d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <string>
#include <map>
#include <functional>
using namespace std;
int V1, V2, T, D;
int bt(int v, int level, int sum){
//prune early if v is too high to reduce back to V2
//note: x/y round up use x+y-1/y
if(D!=0 && T-1-level < ((v-V2)+(D-1))/D) return -1;
if(level==T-1){
if(v==V2) return sum;
else return -1;
}
int ans;
for(int i=v+D;i>=v-D;i--){
if((ans=bt(i, level+1, sum+i))>0){
return ans;
}
}
return -1;
}
int main(){
cin>>V1>>V2>>T>>D;
cout<<bt(V1, 0, V1)<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment