2018年长沙理工大学第十三届程序设计竞赛 箱庭的股市

https://www.nowcoder.com/acm/contest/96/F

 

F. 箱庭的股市
方程可以转换为A[i][j]=A[i-1][j-1]+A[i-1][j],打表后发现,每一行的后一项减去前一项,得到的
是一个杨辉三角,因此A[i][j] = sum(C(i,k)) (0<=k<=j)

 

其实按照数据范围,是会超时的,但是嘛……

最主要的是求sum(C(i,k))的方法(程序注释)

 

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <list>
 6 #include <stack>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 #include <iostream>
13 using namespace std;
14 const long mod=1e9 + 7;
15 const long maxn=1e6;
16  
17 long long f[maxn+5],a[maxn+5],b[maxn+5];
18  
19 int main()
20 {
21     long m,x,y,i,ci;
22     long long be,r;
23      
24     f[0]=1;
25     a[0]=1;
26     for (i=1;i<=maxn;i++)
27     {
28         f[i]=(f[i-1]<<1)%mod;
29         a[i]=a[i-1]*i%mod;
30     }
31     
32     b[maxn]=1;
33     ci=mod-1-1;
34     r=a[maxn];
35     while (ci)
36     {
37         if ((ci & 1)==1)
38             b[maxn]=b[maxn]*r%mod;
39         ci=ci>>1;
40         r=r*r%mod;
41     }
42     /*
43     a[i+1]*b[i+1] %mod = 1
44     (a[i]*(i+1))*b[i+1] %mod = 1
45     
46     a[i]*b[i] %mod = 1
47     a[i]*((i+1)*b[i+1]) %mod = 1
48     */
49     
50     for (i=maxn-1;i>=1;i--)
51         b[i]=b[i+1]*(i+1)%mod;
52     b[0]=1;
53     
54     while (scanf("%ld%ld%ld%lld",&m,&x,&y,&be)!=EOF)
55     {  
56         x--;
57         if (x<=y)
58         {
59             printf("%lld\n",f[x]*be%mod);
60             continue;
61         }
62         r=0;
63         for (i=0;i<=y;i++)
64             r=(r+ a[x]*b[x-i]%mod*b[i]%mod )%mod;
65         printf("%lld\n",r*be%mod);
66         
67         //x<=1e6 y<=1e4 其实如果y*2<x ,求后面的值 
68     }
69     return 0;
70 }

 

超时代码:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <list>
 6 #include <stack>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 #include <iostream>
13 using namespace std;
14 const long mod=1e9 + 7;
15 const long maxn=1e4+5;
16  
17 long long f[maxn],c[maxn];
18  
19 int main()
20 {
21     long m,x,y,i,ci;
22     long long be,a,r;
23      
24     f[0]=1;
25     for (i=1;i<=10001;i++)
26         f[i]=(f[i-1]<<1)%mod;
27      
28     for (i=1;i<=10001;i++)
29         c[i]=1;
30     for (i=0;i<=10001;i++)
31     {
32         ci=mod-1-1;
33         a=i;
34         while (ci)
35         {
36             if ((ci & 1)==1)
37                 c[i]=c[i]*a%mod;
38             ci=ci>>1;
39             a=a*a%mod;
40         }
41     }
42      
43     while (scanf("%ld%ld%ld%lld",&m,&x,&y,&be)!=EOF)
44     {  
45         x--;
46         if (x<=y)
47         {
48             printf("%lld\n",f[x]*be%mod);
49             continue;
50         }
51         r=0;
52         a=1;   
53         for (i=0;i<=y;i++)
54         {
55             r=(r+a)%mod;
56             a=a*(x-i)%mod *c[i+1]%mod;
57         }
58         printf("%lld\n",r*be%mod);
59     }
60     return 0;
61 }

 

posted @ 2018-04-16 21:43  congmingyige  阅读(213)  评论(0编辑  收藏  举报