倍增

#include <iostream>

using  namespace std;

int main ()

{
    int n;
    cin>>n;
    int aps=0,kind,num,choice,blank;
    for(int i=0;i<3;i++)
    {
        cin>>kind>>num;
        choice=kind,blank=num;
        while(choice<n)//利用位运算进行大幅度调节
            choice<<=1,blank<<=1;
        while (choice>n)//进行微调
            choice-=kind,blank-=num;
        while (choice<n)//微调保证超出需要
            choice+=kind,blank+=num;
        if(blank<aps||aps==0)
            aps=blank;
    }
        cout<<aps<<endl;
    return 0;
}

n<<1==n*2;

通过倍增来进行优化

例题:P1909 买铅笔

https://www.luogu.org/problemnew/show/P1909

原文地址:https://www.cnblogs.com/zwx7616/p/10734319.html