A Very Simple Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 778 Accepted Submission(s): 392
Problem Description
This is a very simple problem. Given three integers N, x, and M, your task is to calculate out the following value:
Input
There are several test cases. For each case, there is a line with three integers N, x, and M, where 1 ≤ N, M ≤ 2*10
9, and 1 ≤ x ≤ 50.
The input ends up with three negative numbers, which should not be processed as a case.
The input ends up with three negative numbers, which should not be processed as a case.
Output
For each test case, print a line with an integer indicating the result.
Sample Input
100 1 10000 3 4 1000 -1 -1 -1
Sample Output
5050 444
Source
Recommend
参考代码1(利用第二种方法建立递推式,运行时间比较少,400ms+)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
int n,x,m;
ll C[55][55];
struct mat
{
ll d[55][55];
}A,B,E;
mat multi(mat a,mat b)
{
mat ans;
REP(i,0,x+1)
{
REP(j,0,x+1)
{
ans.d[i][j]=0;
REP(k,0,x+1)
if(a.d[i][k]&&b.d[k][j])
ans.d[i][j]=(ans.d[i][j]+a.d[i][k]*b.d[k][j])%m;
}
}
return ans;
}
mat quickmulti(ll n)
{
if(n==0) return E;
if(n==1) return A;
mat p=E,q=A;
while(n)
{
if(n&1)
{
n--;
p=multi(p,q);
}
else
{
n>>=1;
q=multi(q,q);
}
}
return p;
}
int main()
{
CLR(E.d);
REP(i,0,52)
E.d[i][i]=1;
while(~scanf("%d%d%d",&n,&x,&m))
{
if(n<0&&x<0&&m<0)
break;
REP(i,0,50)
{
C[i][0]=C[i][i]=1;
rep(j,1,i)
C[i][j]=(C[i-1][j-1]+C[i-1][j])%m;
}
CLR(A.d);
A.d[0][0]=1;
REP(i,1,x+1)
A.d[0][i]=(C[x][i-1]*x%m)%m;
REP(i,1,x+1)
REP(j,1,i)
A.d[i][j]=(C[i-1][j-1]*x%m)%m;
CLR(B.d);
REP(i,0,x+1)
B.d[i][0]=x;
mat ans=quickmulti(n-1);
ans=multi(ans,B);
printf("%I64d\n",ans.d[0][0]);
}
}
参考代码二(第一种方法从通项公式上建立递推式,运行时间长,容易超时,第一次写1950ms险过)
因此,在能够从前n项和上建立递推式尽量从前n项和上建立递推式。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
int n,x,m;
ll C[55][55];
struct mat
{
ll d[55][55];
} A,B,E1;
struct Mat
{
mat m[2][2];
} M,E;
mat multi(mat &a,mat &b)
{
mat ans;
REP(i,0,x)
{
REP(j,0,x)
{
ans.d[i][j]=0;
REP(k,0,x)
{
if(a.d[i][k]&&b.d[k][j])
{
ans.d[i][j]=(ans.d[i][j]+a.d[i][k]*b.d[k][j]);
if(ans.d[i][j]>=m)
ans.d[i][j]%=m;
}
}
}
}
return ans;
}
mat add(mat &a,mat &b)
{
mat ans;
REP(i,0,x)
{
REP(j,0,x)
{
ans.d[i][j]=a.d[i][j]+b.d[i][j];
if(ans.d[i][j]>=m)
ans.d[i][j]%=m;
}
}
return ans;
}
Mat Multi(Mat &a,Mat &b)
{
Mat ans;
CLR(ans.m);
rep(i,0,2)
{
rep(j,0,2)
{
rep(k,0,2)
{
mat ans1=multi(a.m[i][k],b.m[k][j]);
ans.m[i][j]=add(ans.m[i][j],ans1);
}
}
}
return ans;
}
int main()
{
CLR(E1.d);
CLR(E.m);
REP(i,0,50)
E1.d[i][i]=1;
E.m[0][0]=E.m[1][1]=E1;
while(~scanf("%d%d%d",&n,&x,&m))
{
if(n<0&&x<0&&m<0)
break;
REP(i,0,50)
{
C[i][0]=C[i][i]=1;
rep(j,1,i)
C[i][j]=(C[i-1][j-1]+C[i-1][j])%m;
}
CLR(A.d);
A.d[0][0]=x;
REP(i,1,x)
A.d[0][i]=(C[x][i-1]*x%m)%m;
REP(i,1,x)
REP(j,1,i)
A.d[i][j]=(C[i-1][j-1]*x%m)%m;
CLR(B.d);
REP(i,0,x) B.d[i][0]=x;
CLR(M.m);
M.m[0][0]=E1,M.m[0][1]=M.m[1][1]=A;
Mat m1=E;
n--;
while(n)
{
if(n&1) m1=Multi(m1,M);
M=Multi(M,M);
n>>=1;
}
mat ans=m1.m[0][1];
ans=add(ans,E1);
ans=multi(ans,B);
printf("%I64d\n",ans.d[0][0]);
}
}