【数论】FOJ 2238 Daxia & Wzc's problem

题目链接:

  http://acm.fzu.edu.cn/problem.php?pid=2238

题目大意:

  已知等差数列A(0)的首项a和公差d,求出数列A(0)前n项和,得到新数列A(1);以此类推,最终求A(m)的第i项mod1000000007

题目思路:

  【动态规划】

  不难推出c[i][j]=c[i-1][j]+c[i][j-1]

  但是i太大不能直接递推,m<=1000不能矩阵快速幂。

  通过推导可以求出c[i][j]=a*C(n+m-1,n-1)+d*C(n+m-1,n-2)

  求模时除改为乘逆元(amod-2)

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define mod 1000000007
26 #define MAX 0x7f7f7f7f
27 #define PI 3.14159265358979323
28 #define N 1004
29 using namespace std;
30 typedef long long LL;
31 int cas,cass;
32 int n,m,lll,ans;
33 int a,d;
34 LL aa,bb,cc,dd;
35 LL mi(LL x,int y)
36 {
37     LL sum=1;
38     while(y)
39     {
40         if(y&1)sum=(sum*x)%mod;
41         x=(x*x)%mod;
42         y>>=1;
43     }
44     return sum;
45 }
46 int main()
47 {
48     #ifndef ONLINE_JUDGE
49     freopen("1.txt","r",stdin);
50 //    freopen("2.txt","w",stdout);
51     #endif
52     int i,j,k;
53 //    for(scanf("%d",&cas);cas;cas--)
54 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
55     while(~scanf("%d",&a))
56 //    while(~scanf("%d",&n))
57     {
58         scanf("%d%d%d",&d,&m,&n);
59         for(i=n,aa=1;i<=n+m-1;i++)
60             aa=(aa*i)%mod;
61         for(i=2,bb=1;i<=m;i++)
62             bb=(bb*i)%mod;
63         bb=mi(bb,mod-2);
64         for(i=n-1,cc=1;i<=n+m-1;i++)
65             cc=(cc*i)%mod;
66         for(i=2,dd=1;i<=m+1;i++)
67             dd=(dd*i)%mod;
68         dd=mi(dd,mod-2);
69         aa=(aa*bb)%mod;aa=(aa*a)%mod;
70         cc=(cc*dd)%mod;cc=(cc*d)%mod;
71         printf("%lld
",(aa+cc)%mod);
72     }
73     return 0;
74 }
75 /*
76 //
77 
78 //
79 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/5783720.html