A Very Simple Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1022 Accepted Submission(s): 500Problem DescriptionThis is a very simple problem. Given three integers N, x, and M, your task is to calculate out the following value:InputThere are several test cases. For each case, there is a line with three integers N, x, and M, where 1 ≤ N, M ≤ 2*109, and 1 ≤ x ≤ 50.
The input ends up with three negative numbers, which should not be processed as a case.OutputFor each test case, print a line with an integer indicating the result.Sample Input100 1 100003 4 1000-1 -1 -1Sample Output5050444Source
【分析】
感觉我的思想离正解还是太远太远,根本不会这样想。
首先我们发现x很小,k很大,k达到了10^9,for一遍都不行,这样我们就想到ans可能存在递推式,然后可以用矩阵加速。
转自:http://972169909-qq-com.iteye.com/blog/1863402
太厉害了,我自己都好难再推一次~~
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 60
#define LL long long LL c[Maxn][Maxn];
LL n,x,M; struct node
{
LL a[Maxn][Maxn];
}t[]; void init()
{
memset(c,,sizeof(c));
for(LL i=;i<=;i++) c[i][]=;
for(LL i=;i<=;i++)
for(LL j=;j<=;j++)
c[i][j]=(c[i-][j-]+c[i-][j])%M;
} void get_un()
{
memset(t[].a,,sizeof(t[].a));
for(LL i=;i<=x+;i++) t[].a[i][i]=%M;
} void mul(LL now,LL y,LL z)
{
for(int i=;i<=x+;i++)
for(int j=;j<=x+;j++)
{
t[].a[i][j]=;
for(LL k=;k<=x+;k++)
t[].a[i][j]=(t[].a[i][j]+t[y].a[i][k]*t[z].a[k][j])%M;
}
t[now]=t[];
} void qpow(LL b)
{
get_un();
while(b)
{
if(b&) mul(,,);
mul(,,);
b>>=;
}
} int main()
{
while()
{
scanf("%lld%lld%lld",&n,&x,&M);
if(n==-&&x==-&&M==-) break;
init();
for(LL i=;i<=x;i++)
{
for(LL j=;j<=i;j++) t[].a[i][j]=(x*c[i][j])%M;
for(LL j=i+;j<=x+;j++) t[].a[i][j]=;
}
for(LL i=;i<x;i++) t[].a[x+][i]=;
t[].a[x+][x]=t[].a[x+][x+]=%M;
// get_un();
qpow(n+);
printf("%lld\n",t[].a[x+][]);
}
return ;
}
[HDU 3483]
2016-09-25 22:07:05