梦回吹角连营(2)(快速幂&快乘)

该文章介绍了一种利用快速幂和进制转换简化表达式的方法,处理大数乘法并求解f(n)对10000000033取模的问题,适用于IT竞赛中的高效编程

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

给定f(n)=(a+1)*n^a+(a+2)*n^(a+1)+...+b*n^(b-1)
求f(n)%10000000033

Input

输入一个正整数T(T<=10),表示有T组数据,每组数据包括三个整数a,b,n
(0<=n<=10^9,1<=a <= b-1<=10^20)

Output

输出 f(n)%10000000033 的结果

Sample Input

1
1 2 3

Sample Output

6

思路:

化简一下式子,得到:

得到 S(a),S(b),用快速幂模板

注意:这题数非常大,两数相乘用快乘模板。

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define per(i,a,b) for(int i=a;i<=b;i++)
#define ber(i,a,b) for(int i=a;i>=b;i--)
const int N = 1e5 + 5;
const LL mod = 10000000033;
long long n, a, b,aa,bb;
char sa[33], sb[33];
LL mul(LL a, LL b, LL mod)
{
    LL ans = 0;
    while (b)
    {
        if (b & 1)
            ans =(ans+a)%mod;
        b >>= 1;
        a = (a + a) % mod;
    }
    return ans;
}
LL quick(LL a, LL b, LL mod)
{
    LL ans = 1;
    while (b)
    {
        if (b & 1)
         ans = mul(ans, a,mod);
         b >>= 1;
        a = mul(a, a,mod);
    }
    return ans;
}
void into()
{
    int lena = (int)strlen(sa+1), lenb = (int)strlen(sb + 1);
    per(i, 1, lena)
    {
        a = (a*10 + sa[i] - '0') % mod;
        aa = (aa*10 + sa[i] - '0') % (mod - 1);
    }
    per(i, 1, lenb)
    {
        b = (b*10 + sb[i] - '0') % mod;
        bb = (bb*10 +sb[i] -'0') % (mod - 1);
    }
}
int main()
{  
    int T;
    cin >> T;
    while (T--)
    {
        a = aa = b = bb = 0;
        cin >> sa+1 >> sb+1 >> n;
        into();
        if (n == 0)
        {
            cout << 0 << endl;
            continue;
        }
        if (n == 1)
        {
            LL ni = quick(2, mod - 2, mod);
            cout << ((mul(mul(b, b + 1, mod),ni,mod)- mul(mul(a, a + 1, mod), ni, mod)) % mod + mod) % mod << endl;
            continue;
        }
        LL ni = quick(n - 1, mod - 2, mod);
        LL anb = quick(n, bb, mod),ana=quick(n,aa,mod);
        anb = mul(b, anb, mod) - mul(ni,anb-1,mod);
        anb = (anb+mod) % mod;
        anb = mul(anb, ni, mod);
        ana = mul(a, ana, mod) - mul(ni, ana - 1, mod);
        ana = (ana % mod + mod) % mod;
        ana = mul(ana, ni, mod);
        cout <<((anb - ana)%mod+mod)%mod << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值