梦回吹角连营(超1e18的快速幂模板,两大数相乘处理)
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)//计算两个大数相乘
{
memset(ca, 0, sizeof ca);
memset(cc, 0, sizeof cc);
int i = 0;
while (a)
{
ca[i] = a % 10;
a /= 10;
i++;
}//将a存到数组内
i--;
int len = 0;
int t = 0;
for (int k = 0; k <= i; k++)
{
LL x = b * ca[k];
t = k;
while (x)
{
cc[t] += (x % 10);
x/= 10;
while (cc[t] >= 10)
cc[t + 1]++, cc[t] = cc[t] - 10;
t++;
}
}//得到a与b相乘的结果,存到数组内
for (int i = 100; i >= 0; i--)
if (cc[i] != 0)
{
len = i;
break;
}
LL ans = 0;
for (int i = 0; i <=len; i++)
ans = (ans + cc[i] * w[i] % mod) % mod;
return ans % mod;
}
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;
}