题意:
· 给你下列关系,求解f(n)
· f(0) = 0,
· f(n) = g(n, f(n-1)),
g(x,y) =((y-1)x5 +x3 – xy + 3x + 7y) % 9973
给你n,输出f(n)
分析:
f(n)=k1[n]*f(n) +k2[n]
k1[n] = n^5-n+7
k2[n] = n^5-n^3-3*n
m(M)为9973
f(x+m) = (A*f(x)+B)%m
f(x+2m) = (A*f(x+m)+B)%m
注意点:A是常量,B是随n%M变化而变化的
input | output |
---|---|
50 | 6300 |
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
const int M = 9973;
int f_mp[M+3];
int k1[M+3],k2[M+3];
int ppow(int n,int p)
{
int ted = n;
while(p>1)
{
ted%=M;
ted*=n;
p--;
}
return ted%M;
}
int dfs(int x)
{
if(x==0)
{
f_mp[0]=0;
k1[0]= 7;k2[0] = 0;
return 0;
}
int temp = dfs(x-1);
int temp2 = ppow(x,5);
int temp3 = ppow(x,3);
k1[x] = (((temp2-x + 7)%M+M)%M);
temp = (temp * k1[x])%M;
k2[x]=(-(temp2 - temp3 - (3 *x)%M +M)%M+M)%M;
temp = ((temp +k2[x] )%M+M)%M;
f_mp[x]=temp;
return temp;
}
void solve(int n)
{
int A = 1;;
int B = 0;
int p = n%M;
int q = n/M;
dfs(M);
int i;
int k;
for(i = p+M;i>=p+1;i--)
{
k=i%M;
B = ((B+A*k2[k])%M+M)%M;
A=((A*k1[k])%M+M)%M;
}
int ans = f_mp[p];
for(i=0;i<q;i++)
{
ans = (((A*ans)%M+B)%M+M)%M;
}
cout << ans << endl;
}
int main()
{
int n ;
while(cin>>n)
{
solve(n);
}
return 0;
}