Seeing the expected possibility, I suddenly thought it was a probability DP ..
This is also true.
But I tried two things: TLE and AC, but it took more than 3000 Ms ..
In addition, I think there is little difference between the two types of data ..
It should be the second type of TLE. It's time, my guess.
The idea of this question is simple: the layer-by-layer recurrence, and the state of this layer is only related to the previous layer ~
In fact, the number of solutions that can be selected each time is: 2 ^ 1 --> 2 ^ 2 --> 2 ^ 3 -->...-> 2 ^ m
The value of 2 ^ m is huge, because there are only 200 lattice n <= 200, so I only need to traverse n.
Please help me correct the first code =_=
At the beginning, I used set to deal with the serious troubles of TLE ..
1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 5 const int size = 210; 6 double dp[2][size]; 7 8 int main( ) 9 {10 cin.sync_with_stdio(false);11 int n , m , L , R , k , x , y , w;12 double ans;13 while( cin >> n >> m >> L >> R && ( n || m || L || R ) )14 {15 k = 0;16 for( int i = 0 ; i<=n ; i++ )17 {18 dp[0][i] = dp[1][i] = 0.0;19 }20 for( int i = 0 ; i<m ; i++ )21 {22 cin >> w;23 if( !i )24 {25 x = (1+w-1)%n + 1;26 y = (1+n-w)%n;27 y = (y==0) ? n : y;28 dp[k][x] = 0.5;29 dp[k][y] = 0.5;30 //cout << x << " " << y << endl;31 }32 else33 {34 for( int j = 1 ; j<=n ; j++ )35 {36 if( dp[k][j] )37 {38 x = (j+w-1)%n+1;39 y = (j+n-w)%n;40 y = (y==0) ? n : y;41 dp[!k][x] += dp[k][j] * 0.5;42 dp[!k][y] += dp[k][j] * 0.5;43 //cout << x << " " << y << endl;44 }45 dp[k][j] = 0;46 }47 k = !k;48 }49 }50 ans = 0;51 for( int i = L ; i<=R ; i++ )52 {53 ans += dp[(m+1)&1 ][i];54 //cout << dp[(m+1)&1][i] << endl;55 }56 cout << setiosflags(ios::fixed);57 cout << setprecision(4) << ans << endl;58 }59 return 0;60 }61 /*62 4 3 1 263 164 265 366 */View code
1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 5 const int size = 210; 6 double dp[2][size]; 7 8 int main( ) 9 {10 cin.sync_with_stdio(false);11 int n , m , L , R , k , x , y , w;12 double ans;13 while( cin >> n >> m >> L >> R && ( n || m || L || R ) )14 {15 k = 1;16 for( int i = 0 ; i<=n ; i++ )17 {18 dp[0][i] = dp[1][i] = 0.0;19 }20 dp[0][0] = 1.0;21 for( int i = 0 ; i<m ; i++ )22 {23 cin >> w;24 for( int j = 0 ; j<n ; j++ )25 {26 if( dp[!k][j] )27 {28 dp[k][(j+w)%n] += dp[!k][j] * 0.5;29 dp[k][(j-w+n)%n] += dp[!k][j] * 0.5;30 }31 dp[!k][j] = 0;32 }33 k = !k;34 }35 ans = 0;36 for( int i = L-1 ; i<=R-1 ; i++ )37 {38 ans += dp[ m&1 ][i];39 }40 cout << setiosflags(ios::fixed);41 cout << setprecision(4) << ans << endl;42 }43 return 0;44 }View code
Today:
Yesterday
You said
Tomorrow
This is how we were told at the age.
HDU -- 4576 -- probability dp