CoderForces-Round60D(1117) Magic Gems

D. Magic Gems

time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Reziba has many magic gems. Each magic gem can be split into MM normal gems. The amount of space each magic (and normal) gem takes is 11 unit. A normal gem cannot be split.

Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is NN units. If a magic gem is chosen and split, it takes MM units of space (since it is split into MM gems); if a magic gem is not split, it takes 11 unit.

How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is NN units? Print the answer modulo 10000000071000000007 (109+7109+7). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ.

Input

The input contains a single line consisting of 22 integers NN and MM (1N10181≤N≤1018, 2M1002≤M≤100).

Output

Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is NN units. Print the answer modulo 10000000071000000007 (109+7109+7).

Examples
input
Copy
4 2
output
Copy
5
input
Copy
3 2
output
Copy
3

In the first example each magic gem can split into 22 normal gems, and we know that the total amount of gems are 44.

Let 11 denote a magic gem, and 00 denote a normal gem.

The total configurations you can have is:

  • 11111111 (None of the gems split);
  • 00110011 (First magic gem splits into 22 normal gems);
  • 10011001 (Second magic gem splits into 22 normal gems);
  • 11001100 (Third magic gem splits into 22 normal gems);
  • 00000000 (First and second magic gems split into total 44 normal gems).

Hence, answer is 55.

 题解:

  • 考虑 dpdp , f[i]f[i] 表示用 ii 个单位空间的方案数,答案即为 f[n]f[n].
  • 对于一个位置,我们可以放 MagicMagic 的,占 mm 空间,也可以放 NormalNormal 的,占 11 空间.
  • 转移方程即为 f[i]=f[i1]+f[im]f[i]=f[i−1]+f[i−m] ,边界条件为 f[0]=f[1]=f[2]=f[m1]=1f[0]=f[1]=f[2]=…f[m−1]=1.
  • 直接转移是 O(n)O(n) 的,无法通过,需要矩阵优化.

 

 

也可以用杜教BM,求线性递推式;

参考代码:(矩阵快速幂)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define Mod 1000000007
 5 const double PI = acos(-1.0);
 6 const double eps = 1e-6;
 7 const int INF = 0x3f3f3f3f;
 8 const int N = 100 + 5;
 9 struct Matrix {
10   ll n , m;
11   ll grid[N][N];
12   Matrix () { n = m = 0; memset(grid , 0 , sizeof(grid)); }
13 };
14 
15 Matrix mul(Matrix a,Matrix b) 
16 {
17     Matrix c;
18     c.n = a.n;c.m = b.m;
19     for(ll i=1;i<=c.n;++i)
20         for(ll j=1;j<=c.m;++j) 
21         {
22             ll cnt = 0;
23             for(ll k=1;k<=a.m;++k) 
24             {
25                 c.grid[i][j] = (c.grid[i][j] + a.grid[i][k] * b.grid[k][j]);
26                 cnt++;
27                 if(cnt % 8 == 0) c.grid[i][j] %= Mod;
28             }
29           c.grid[i][j] %= Mod;
30         }
31   return c;
32 }
33 Matrix QuickMul(Matrix a ,ll k) 
34 {
35     if(k == 1) return a;
36     Matrix mid = QuickMul(a ,(k >> 1));
37     if(k & 1) return mul(mul(mid , mid),a);
38     else return mul(mid , mid);
39 }
40 ll n , m;
41 int main() 
42 {
43     cin >> n >> m;
44     if(n < m) {return puts("1") , 0;}
45     if(n == m) return puts("2") , 0;
46     Matrix basic; basic.n = m; basic.m = 1;
47     for(ll i=1;i<=m;++i) basic.grid[i][1] = (i == m) ? 2 : 1;//{1,1,1...1,m}T
48     Matrix base; base.n = base.m = m;
49     
50     for(ll i = 1; i <= m - 1; i++) base.grid[i][i + 1] = 1;
51     base.grid[m][1] = base.grid[m][m] = 1;
52     
53     Matrix ans = mul(QuickMul(base , n - m) , basic);
54     cout << ans.grid[m][1] << endl;
55     return 0;
56 }
View Code

杜教BM

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define rep(i,a,n) for (int i=a;i<n;i++)
 4 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 5 #define pb push_back
 6 #define mp make_pair
 7 #define all(x) (x).begin(),(x).end()
 8 #define fi first
 9 #define se second
10 #define SZ(x) ((int)(x).size())
11 typedef vector<int> VI;
12 typedef long long ll;
13 typedef pair<int,int> PII;
14 const ll mod=1000000007;
15 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1) { if(b&1)res=res*a%mod; a=a*a%mod;  }  return res;  }
16 ll _,n,m,dp[321];
17 namespace linear_seq {
18     const int N=10010;
19     ll res[N],base[N],_c[N],_md[N];
20     vector<ll> Md;
21     void mul(ll *a,ll *b,int k)
22     {
23         rep(i,0,k+k) _c[i]=0;
24         rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]= (_c[i+j]+a[i]*b[j])%mod;
25         for (int i=k+k-1;i>=k;i--)  if (_c[i])
26             rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
27         rep(i,0,k) a[i]=_c[i];
28     }
29     int solve(ll n,VI a,VI b)
30     {
31         ll ans=0,pnt=0;
32         int k=SZ(a);
33         assert(SZ(a)==SZ(b));
34         rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
35         Md.clear();
36         rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
37         rep(i,0,k) res[i]=base[i]=0;
38         res[0]=1;
39         while ((1ll<<pnt)<=n) pnt++;
40         for (int p=pnt;p>=0;p--)
41         {
42             mul(res,res,k);
43             if ((n>>p)&1)
44             {
45                 for (int i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
46                 rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
47             }
48         }
49         rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
50         if (ans<0) ans+=mod;
51         return ans;
52     }
53     VI BM(VI s) {
54         VI C(1,1),B(1,1);
55         int L=0,m=1,b=1;
56         rep(n,0,SZ(s)) {
57             ll d=0;
58             rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
59             if (d==0) ++m;
60             else if (2*L<=n) {
61                 VI T=C;
62                 ll c=mod-d*powmod(b,mod-2)%mod;
63                 while (SZ(C)<SZ(B)+m) C.pb(0);
64                 rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
65                 L=n+1-L; B=T; b=d; m=1;
66             } else {
67                 ll c=mod-d*powmod(b,mod-2)%mod;
68                 while (SZ(C)<SZ(B)+m) C.pb(0);
69                 rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
70                 ++m;
71             }
72         }
73         return C;
74     }
75     int gao(VI a,ll n) {
76         VI c=BM(a);
77         c.erase(c.begin());
78         rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
79         return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
80     }
81 };
82 int main()
83 {
84     scanf("%lld%lld",&n,&m);
85     vector<int> v;
86     for(int i=1;i<m;++i) v.push_back(1);
87     for(ll i=1;i<=m;++i) dp[i]=i+1,v.push_back(dp[i]);
88     for(int i=m+1;i<=10;++i) dp[i]=dp[i-1]+dp[i-m],v.push_back(dp[i]);
89 
90     printf("%lld\n",linear_seq::gao(v,n-1)%mod);
91     return 0;
92 }
View Code

 

 

posted @ 2019-02-20 20:22  StarHai  阅读(237)  评论(0编辑  收藏  举报