a^b-b^a - SGU 112(高精度快速幂)

分析:直接上吧,建议不要使用模板,否则没啥意义了。

代码如下:

===================================================================================================================================

#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
using namespace std;

const int MAXN = 10007;

struct BigNum
{
    int size, op;///长度, op=1表示非负数,-1表示负数
    int num[MAXN];///数,下标从0开始

    BigNum(){
        op = size = 1;
        memset(num, false, sizeof(num));
    }
    void GetText(int n)
    {
        size = log10(n)+1;

        for(int i=0; i<size; i++)
        {
            num[i] = n % 10;
            n /= 10;
        }
    }
    void CarryBit()
    {///进位
        for(int i=0; i<size; i++)
        {
            if(i+1==size && num[i] >= 10)
            {
                num[i+1] = 0;
                size += 1;
            }

            num[i+1] += num[i] / 10;
            num[i] %= 10;
        }
    }
    void BorrowBit()
    {///借位
        for(int i=0; i<size; i++)
        {
            if(num[i] < 0)
            {
                num[i+1] -= 1;
                num[i] += 10;
            }
        }

        while(size > 1 && !num[size-1])
            size --;
    }
    bool operator < (const BigNum &a)const
    {///非负数比较
        if(size > a.size)return false;
        if(size < a.size)return true;

        for(int i=0; i<size; i++)
        {
            if(num[i] > a.num[i])return false;
            if(num[i] < a.num[i])return true;
        }

        return false;
    }
    BigNum operator * (const BigNum &a)const
    {
        BigNum result;

        result.size = size + a.size - 1;

        for(int i=0; i<size; i++)
        for(int j=0; j<a.size; j++)
        {
            result.num[i+j] += num[i] * a.num[j];
        }
        result.CarryBit();

        return result;
    }
    friend BigNum operator - (const BigNum &_a, const BigNum &_b)
    {
        BigNum result, a=_a, b=_b;

        if(a < b)
        {
            result.op = -1;
            swap(a, b);
        }
        result.size = a.size;

        for(int i=0; i<a.size; i++)
            result.num[i] = a.num[i] - b.num[i];
        result.BorrowBit();

        return result;
    }
    friend BigNum operator ^(const BigNum &_a, const int &_m)
    {
        BigNum result, a=_a;
        result.num[0] = 1;
        int m = _m;
        while(m)
        {
            if(m & 1)
                result = result * a;
            a = a * a;

            m >>= 1;
        }

        return result;
    }
    void Out()
    {
        if(op == -1)printf("-");
        for(int i=size-1; i>=0; i--)
            printf("%d", num[i]);
        printf("\n");
    }
};

int main()
{
    int a, b;
    BigNum m, n;

    scanf("%d%d", &a, &b);

    m.GetText(a);
    n.GetText(b);

    m = m ^ b;
    n = n ^ a;

    BigNum ans = m - n;

    ans.Out();

    return 0;
}
时间: 2025-01-03 13:53:34

a^b-b^a - SGU 112(高精度快速幂)的相关文章

高精度快速幂(Java版)

1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 import java.text.*; 5 6 public class Main { 7 public static void main(String[] args) { 8 Scanner cin = new Scanner(System.in); 9 BigInteger a, b, m, ans; 10 while (cin.hasNext()) { 11

随手练——麦森数(高精度快速幂)

https://www.luogu.org/problemnew/show/P1045 第一步:求位数 第二步:高精度快速幂的实现 第一版: 用vector做的,中间把超过500位的去除,但是实际跑起来比较慢 #include <iostream> #include <stdio.h> #include <math.h> #include <vector> #include <algorithm> using namespace std; int

高精度快速幂(算法描述)

板子题: 链接 题意 求2^p-1 的后500位和位数: 位数好求,最后一位-1,如果最后一位不是0 ,则无需往前借位,然而2^p不可能出现最后一位是0的情况:所以2^p-1和2^p位数相同. 对于求a^b问题,直接快速幂就好了 然鹅,这道题的数非常大,P(1000<P<31000001000<P<3100000).最大的一个是P=3021377P=3021377,它有909526位. 普通快速幂 long long quick(int a,int b) { ans=1; num=

HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)

Sum Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4704 Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases. 题意:给定一

Counting - SGU 117(快速幂)

题目大意:求下面N个数里面有多少个数的M次方能整除K 代码如下: ======================================================== #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXN = 10007; const int oo = 1e9+7; int QuickPow(int a, int

SGU 112 a^b-b^a

JAVA大数.... a^b-b^a Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u [Submit]   [Go Back]   [Status] Description You are given natural numbers a and b. Find ab-ba. Input Input contains numbers a and b (1≤a,b≤100). Output Write

ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度

Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Problem Description You might have noticed that there is the new fashion among rich people to have their yards tiled with black and white tile

SGU - 117 - Counting (快速幂取模!)

SGU - 117 Counting Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u Submit Status Description Find amount of numbers for given sequence of integer numbers such that after raising them to the M-th power they will be divided by

高精度快速预览打开dwg文件的CAD控件CAD Image DLL介绍及下载

CAD Image DLL对于DXF格式, DWG格式(AutoCAD R12 到AutoCAD 2004/2005), PLT 以及 HPGL/HPGL2文件都有快速的显示速度和精度,开发者再也不会为如何打开dwg文件?dwg格式用什么打开?犯愁了.CAD Image DLL价格经济,是理想的商业化的程序内置CAD控件,包括Visual Basic, MS Visual C++, MS Visual Studio .NET, Borland Delphi, Borland C++ Builde