#include<bits/stdc++.h> using namespace std; int n,m,p,dp[2][10000004],w; int main() { cin>>n>>m>>p; for(int i = 1;i <= m; i++) dp[0][i] = i % p; for(int i = 1;i < n; i++) { long long suma = 0; for(int j = 1;j <= m; j++) { dp[1][j] = 0; suma = (suma + dp[0][m - j + 2] - dp[0][j - 1] + p) % p; dp[1][j] = (dp[1][j] + suma) % p; dp[1][j] = (dp[1][j] + dp[1][j - 1]) % p; long long ilo = (long long)j * dp[0][m - j + 1]; ilo %= p; dp[1][j] = (dp[1][j] + ilo) % p; } for(int j = 1;j <= m; j++) dp[0][j] = dp[1][j]; } for(int i = 1;i <= m; i++) w = (w + dp[0][i]) % p; cout<<w<<'\n'; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<bits/stdc++.h> using namespace std; int n,m,p,dp[2][10000004],w; int main() { cin>>n>>m>>p; for(int i = 1;i <= m; i++) dp[0][i] = i % p; for(int i = 1;i < n; i++) { long long suma = 0; for(int j = 1;j <= m; j++) { dp[1][j] = 0; suma = (suma + dp[0][m - j + 2] - dp[0][j - 1] + p) % p; dp[1][j] = (dp[1][j] + suma) % p; dp[1][j] = (dp[1][j] + dp[1][j - 1]) % p; long long ilo = (long long)j * dp[0][m - j + 1]; ilo %= p; dp[1][j] = (dp[1][j] + ilo) % p; } for(int j = 1;j <= m; j++) dp[0][j] = dp[1][j]; } for(int i = 1;i <= m; i++) w = (w + dp[0][i]) % p; cout<<w<<'\n'; return 0; } |