Codeforces Round #563 (Div. 2) E. Ehab and the Expected GCD Problem

 

https://codeforces.com/contest/1174/problem/Eios

 

dp數組

好題spa

 

*(if 知足條件)code

知足條件 *1blog

不知足條件 *0three

 

///這代碼雖然寫着方便,可是常數有點大內存

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 #define ll long long
10 
11 const double eps=1e-8;
12 const ll inf=1e9;
13 const ll mod=1e9+7;
14 const int maxn=1e6+10;
15 
16 /*
17 dp題
18 檢查:
19 1.認真檢查公式
20 2.認真檢查代碼
21 3.造一個規模適中的數據
22 手動推導
23 對應程序的結果
24 判斷是否相同
25 */
26 
27 ///+1 20->21 2->3
28 ///0->-1 前面加一個輔助數組,這個數組裏的全部元素數值爲0
29 ///x1*y1+x2*y2+x3*y3+...x20*y20 有可能會超long long, 因此全部乘法運算後面都加%mod
30 ///1e18*k k<=9
31 int f[maxn][21][3],v[21][3];
32 
33 int main()
34 {
35     int n,two,three,i,j,k;
36     scanf("%d",&n);
37     two=log(n+eps)/log(2);///+eps
38     three=(1.0*n/(1<<two)>=1.5);
39     for (i=0;i<=two;i++)
40         for (j=0;j<=three;j++)
41             v[i][j]=n/((1<<i)*(j==0?1:3));
42 
43     f[1][two][0]=1;
44     if (three==1)
45         f[1][two-1][1]=1;
46     for (i=2;i<=n;i++)
47         for (j=0;j<=two;j++)
48             for (k=0;k<=three;k++)
49                 ///乘1ll,最後強制轉換long long 轉 int
50                 f[i][j][k]=( 1ll*f[i-1][j][k]*(v[j][k]-(i-1))*((v[j][k]-(i-1))>=0)%mod + 1ll*f[i-1][j+1][k]*(v[j][k]-v[j+1][k])*(j!=two)%mod + 1ll*f[i-1][j][k+1]*(v[j][k]-v[j][k+1])*(k!=three)%mod )%mod;
51     printf("%d",f[n][0][0]);
52     return 0;
53 }

 

推薦好比說這個xiongdi的代碼string

https://codeforces.com/contest/1174/submission/56391704it

 

超內存的代碼io

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 #define ll long long
10 
11 const double eps=1e-8;
12 const ll inf=1e9;
13 const ll mod=1e9+7;
14 const int maxn=1e6+10;
15 
16 /*
17 dp題
18 檢查:
19 1.認真檢查公式
20 2.認真檢查代碼
21 3.造一個規模適中的數據
22 手動推導
23 對應程序的結果
24 判斷是否相同
25 */
26 
27 ///+1 20->21 2->3
28 ///0->-1 前面加一個輔助數組,這個數組裏的全部元素數值爲0
29 ///x1*y1+x2*y2+x3*y3+...x20*y20 有可能會超long long, 因此全部乘法運算後面都加%mod
30 ///1e18*k k<=9
31 ll f[maxn][21][3],v[21][3];
32 
33 int main()
34 {
35     int n,two,three,i,j,k;
36     scanf("%d",&n);
37     two=log(n+eps)/log(2);///+eps
38     three=(1.0*n/(1<<two)>=1.5);
39     for (i=0;i<=two;i++)
40         for (j=0;j<=three;j++)
41             v[i][j]=n/((1<<i)*(j==0?1:3));
42 
43     f[1][two][0]=1;
44     if (three==1)
45         f[1][two-1][1]=1;
46     for (i=2;i<=n;i++)
47         for (j=0;j<=two;j++)
48             for (k=0;k<=three;k++)
49                 f[i][j][k]=( f[i-1][j][k]*(v[j][k]-(i-1))*((v[j][k]-(i-1))>=0)%mod + f[i-1][j+1][k]*(v[j][k]-v[j+1][k])*(j!=two)%mod + f[i-1][j][k+1]*(v[j][k]-v[j][k+1])*(k!=three)%mod )%mod;
50 
51     printf("%lld",f[n][0][0]);
52     return 0;
53 }
相關文章
相關標籤/搜索