前置知识
解法
推式子:
\(\begin{aligned} Z_n+Z_{n-1}-2Z_{n-2}&=(Z_n-Z_{n-2})+(Z_{n-1}-Z_{n-2}) \\ &=(S_n+Q_n-S_{n-2}-Q_{n-2})+(S_{n-1}+Q_{n-1}-S_{n-2}-Q_{n-2}) \\ &= ((n-1)^k+n^k+(n-1)^{n-1}+n^n)+((n-1)^k+(n-1)^{n-1}) \\ &=n^n+n^k+2(n-1)^k+2(n-1)^{n-1} \end{aligned}\)
最终结果使用快速幂计算即可。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define sort stable_sort
#define endl '\n'
ll qpow(ll a,ll b,ll p)
{
ll ans=1;
while(b>0)
{
if(b&1)
{
ans=ans*a%p;
}
b>>=1;
a=a*a%p;
}
return ans%p;
}
int main()
{
ll n,k,p=10000007;
while(cin>>n>>k)
{
if(n==0&&k==0)
{
break;
}
else
{
cout<<(((qpow(n,n,p)+qpow(n,k,p))%p+(2*qpow(n-1,n-1,p))%p)+(2*qpow(n-1,k,p))%p)%p<<endl;
}
}
return 0;
}
标签:题解,ll,long,ZSUM,Add,ans,define
From: https://www.cnblogs.com/The-Shadow-Dragon/p/17739725.html