Created
April 13, 2015 14:10
-
-
Save balamark/a8b7c6efe7dbfd64414d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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