Choose any integerXFrom 2K, Random Sive. Then subtract number (A mod x) From his numberA. OperationAmod xMeans taking the remainder from division of numberABy numberX.Petya performs one operation per second. each time he chooses an operation to perform during the current move, no matter what kind of operations he has got Med by that moment. in particle, this implies that he can perform the same operation any number of times in a row.
Now he wonders in what minimum number of seconds he cocould transform his numberAInto numberB. Please note that numbersXIn the operations of the second type are selected anew each time, independently of each other.
InputThe only line contains three integersA,B(1? ≤?B? ≤?A? ≤? 1018) andK(2? ≤?K? ≤? 15 ).
Please do not use the % lld specifier to read or write 64-bit integers in bytes ++. It is preferred to use the cin, coutstreams or the % I64d specifier.
OutputPrint a single integer-the required minimum number of seconds needed to transform numberAInto numberB.
Sample test (s) input10 1 4
Output6
Input6 3 10
Output2
Input1000000000000000000 1 3
Output666666666666666667
NoteIn the first sample the sequence of numbers that Petya gets as he tries to obtain numberBIs as follows: 10? →? 8? →? 6? →? 4? →? 3? →? 2? →? 1.
In the second sample one of the possible sequences is as follows: 6? →? 4? →? 3.
Question:
Give you two numbersA,B(1? ≤?B? ≤?A? ≤? 1018) and k (2? ≤?K? ≤? 15 ).. I want you to change a to B. You can perform two operations
1. Put A-1.
2. a-(a % c). 2 <= c <= k.
You can only perform one of the two operations in each step. C can be selected at will.
Ask how many steps do you need to change a to B.
Ideas:
Because the data range is large, it cannot be simply searched. You must find other rules.
Operation 1 has nothing to say. For operation 2. For each c, a must be a multiple of c. Set cm to 2 ~ The minimum public multiple of k. A = x * cm + y. If y = 0, operation 2 is invalid. In this case, you must use operation 1. The fastest way to reduce a is to first convert a to x * cm and then subtract-1. then subtract to (x-1) * cm and then loop. It is represented by dp [I. The minimum number of steps required to reduce I to 0. The number of steps required for not performing the above operation is 1 + dp [cm-1]. Therefore, we can directly calculate and reduce a to a-B.
For details, see the code:
#include
using namespace std;const int INF=0x3f3f3f3f;const double eps=1e-8;const double PI=acos(-1.0);const int maxn=370360;int dp[maxn],k,vis[maxn],sp[maxn],head,tail;long long q[maxn];int gcd(int x,int y){ int tp=x%y; while(tp) { x=y; y=tp; tp=x%y; } return y;}int dfs(int x){ int tp=INF,i; if(dp[x]!=-1) return dp[x]; for(i=2;i<=k;i++) if(x%i!=0) tp=min(dfs(x-x%i),tp); tp=min(dfs(x-1),tp); return dp[x]=tp+1;}int bfs(int st,int ed){ int i; memset(vis,0,sizeof vis); head=tail=0; vis[st]=1; q[tail]=st; sp[tail++]=0; while(head
b) ans+=dp[a%mc],a-=a%mc; df=a-b; rep=df/mc; a=a-rep*mc; ans+=rep*(1+dp[mc-1]); df=a-b; if(df>0) { if(a%mc==0) ans+=bfs((a-1)%mc,b%mc)+1; else ans+=bfs(a%mc,b%mc); } printf("%I64d\n",ans); } return 0;}