Codeforces 251C Number Transformation (Number theory)

Source: Internet
Author: User

Codeforces 251C Number Transformation (Number theory)
Number Transformationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Little Petya likes positive integers a lot. Recently his mom has presented him a positive integerA. There's only one thing Petya likes more than numbers: playing with little Masha. It turned out that Masha already has a positive integerB. Petya decided to turn his numberAInto the numberBConsecutively Ming the operations of the following two types:

  1. Subtract 1 from his number.
  2. 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.

    Input

    The 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.

    Output

    Print a single integer-the required minimum number of seconds needed to transform numberAInto numberB.

    Sample test (s) input
    10 1 4
    Output
    6
    Input
    6 3 10
    Output
    2
    Input
    1000000000000000000 1 3
    Output
    666666666666666667
    Note

    In 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;}
        
       


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.