少女折寿中

本文介绍了两种快速乘法算法的实现方法:一种适用于大数运算的快速乘法,通过位操作进行乘法运算;另一种为O(1)时间复杂度的快速乘法,通过特定的数学技巧减少模运算的时间开销。此外,还提供了一种优化后的输入输出方法,以提高程序的整体运行效率。

 

 

 

 

/*
        快速乘法        
*/
#include <cstdio>
#include <iostream>

void read (long long &now)
{
    register char word = getchar ();
    for (now = 0; !isdigit (word); word = getchar ());
    for (; isdigit (word); )
    {
        now = (now << 1) + (now << 3) + word - '0';
        word = getchar ();
    }
}

long long Mod = 10000;
    
struct Mul
{
    protected : long long x;
        
    public : long long operator * (Mul now) const
    {
        long long res = 0, pos = this->x;
        
        pos = pos % Mod;
        now.x = now.x % Mod;
        
        for (; now.x; now.x >>= 1)
        {
            if (now.x & 1)
                res = (res + pos) % Mod;
            pos = (pos + pos) % Mod;
        }
        return res;
    }
    
    public : long long operator = (const int &now)
    {
        x = now;
    }
};
    
Mul A, B;
    
int main ()
{
    long long a, b;
    read (a);
    read (b);
    A = a;
    B = b;
    printf ("%lld", A * B);
}

 

 

 

/*
        O(1)快速乘
*/
#include <cstdio> 

long long Mod = 100;

struct Int
{
    long long x;
    
    long long operator * (Int now) const
    {
        long long res = (this->x * now.x - (long long)((long 

long)this->x / Mod * now.x + 1.0e-8) * Mod);
        return res < 0 ? res + Mod : res;
    }
};

int main ()
{
    Int a, b;
    a.x = 200;
    b.x = 3;
    printf ("%lld", a * b);

}

 

 

 

/*
        IO读入优化
*/
#include <iostream>
#include <cstdio>
/*
void read (int &now)
{
    register char word = getchar ();
    for (now = 0; !std :: isdigit (word); word = getchar ());
    for (; std :: isdigit (word); now = now * 10 + word - '0', word = getchar ());
}*/

inline char getcha ()
{
    static char buf[100000], *pos_1 = buf, *pos_2 = buf;
    return pos_1 == pos_2 && (pos_2 = (pos_1 = buf) + fread (buf, 1, 100000, stdin), pos_1 == pos_2) ? EOF : *pos_1 ++;
}
inline int read (int &now)
{
    register char word = getcha ();
    for (now = 0; !std :: isdigit (word); word = getcha ());
    for (; isdigit (word); now = now * 10 + word - '0', word = getcha ());
}

int main (int argc, char *argv[])
{
    freopen ("1.in", "r", stdin);
    int N;
    read (N);
    read (N);
    read (N);
    printf ("%d", N);

    return 0;
}

 

转载于:https://www.cnblogs.com/ZlycerQan/p/7309231.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值