#include <iostream> using namespace std; typedef unsigned long long I; int n,m; I p; #define D(x) #define MAX 10000010 unsigned w[MAX]; unsigned s[MAX],z[MAX], zz[MAX]; void print(int n) { cout << "n: " << n << "------------------\n"; cout << "s: "; for(int i=1;i<=m;i++) { cout << s[i] << " "; } cout << "\n"; cout << "z: "; for(int i=1;i<=m;i++) { cout << z[i] << " "; } cout << "\n"; cout << "zz: "; for(int i=1;i<=m;i++) { cout << zz[i] << " "; } cout << "\n"; cout << "w: "; for(int i=1;i<=m;i++) { cout << w[i] << " "; } cout << "\n"; } void f(int n) { for(int i=1;i<=m;i++) w[i] = m-i+1; D(print(n)); for(int nn =1;nn<n;nn++) { for(int i=1;i<=m;i++) s[i] = I(s[i-1] + w[i]) % p; for(int i=m;i> 0;i--) z[i] = I(z[i+1] + w[i]) % p; for(int i=m;i> 0;i--) zz[i] = I(zz[i+1] + z[i]) % p; for(int i=1;i<=m;i++) w[m-i+1] = (I(i)*s[i] + (p - zz[m-i+2])) % p; D(print(n)); } } int main() { cin >> n >> m >> p; f(n); I res = 0; for(int i=1;i<=m;i++) res = (res + w[i]) % p; cout << res << "\n"; }
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <iostream> using namespace std; typedef unsigned long long I; int n,m; I p; #define D(x) #define MAX 10000010 unsigned w[MAX]; unsigned s[MAX],z[MAX], zz[MAX]; void print(int n) { cout << "n: " << n << "------------------\n"; cout << "s: "; for(int i=1;i<=m;i++) { cout << s[i] << " "; } cout << "\n"; cout << "z: "; for(int i=1;i<=m;i++) { cout << z[i] << " "; } cout << "\n"; cout << "zz: "; for(int i=1;i<=m;i++) { cout << zz[i] << " "; } cout << "\n"; cout << "w: "; for(int i=1;i<=m;i++) { cout << w[i] << " "; } cout << "\n"; } void f(int n) { for(int i=1;i<=m;i++) w[i] = m-i+1; D(print(n)); for(int nn =1;nn<n;nn++) { for(int i=1;i<=m;i++) s[i] = I(s[i-1] + w[i]) % p; for(int i=m;i> 0;i--) z[i] = I(z[i+1] + w[i]) % p; for(int i=m;i> 0;i--) zz[i] = I(zz[i+1] + z[i]) % p; for(int i=1;i<=m;i++) w[m-i+1] = (I(i)*s[i] + (p - zz[m-i+2])) % p; D(print(n)); } } int main() { cin >> n >> m >> p; f(n); I res = 0; for(int i=1;i<=m;i++) res = (res + w[i]) % p; cout << res << "\n"; } |