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

Description

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

Input

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

Output

输出 f(n)%10000000033 的结果

Sample Input

2
1 2 3
2 1 3

Sample Output

12
12

思路:

两数相乘可能会超1e18,所以用到快乘。

代码:

#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 + 1000;
const long long mod = 10000000033;
long long n, a, b;
long long w[1000];
int ca[1000], cb[1000], cc[1000];
void into()
{
        w[0] = 1;
    for (int i = 1; i <= 1000; i++)
        w[i] = w[i - 1] * 10 % mod;
}
LL seek(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 = seek(ans, a, mod);
        b = b >> 1;
        a = seek(a, a, mod);
    }
    return ans;
}
int main()
{
    int T;
    into();
    cin >> T;
    while (T--)
    {
        cin >> a >> b >> n;
        if (a > b)
            swap(a, b);
        if (n == 0)
        {
            cout << 0 << endl;
            continue;
        }
        if (n == 1)
        {
            cout << b - a + 1 << endl;
            continue;
        }
        LL ans = quick(n - 1, mod - 2, mod);
        ans = seek(ans, quick(n, a, mod), mod);
        ans = seek(ans, quick(n, b-a+1, mod) - 1, mod);
        cout << (ans % mod + mod) % mod << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值