#include<iostream> #include<vector> using namespace std; long long n,m,p; vector<vector<long long> > lookup; vector<long long> previ; vector<long long> nexti; vector<long long> zeros; void create_lookup(int u, int d){ long long id = 0; for(int i=1; i<=m; i++){ for(int j=m-1; j>=0; j--){ if(j-(i-1)>=0){ if(d<=j&&u>=j-(i-1)){ lookup.back().push_back(id); } id++; } } } } void create_states(){ for(int i=1; i<=m; i++){ for(int j=m-1; j>=0; j--){ if(j-(i-1)>=0){ lookup.push_back(vector<long long>()); previ.push_back(1); nexti.push_back(1); zeros.push_back(0); create_lookup(j, j-(i-1)); }else{ break; } } } } void paint(){ for(int i=0; i<n-1; i++){ previ = nexti; nexti = zeros; for(int j=0; j<lookup.size(); j++){ for(auto s: lookup[j]){ nexti[s] = (nexti[s] + previ[j])%p; } } } } void give_answer(){ long long sum = 0; for(int i=0; i<nexti.size(); i++){ sum = (sum+nexti[i])%p; } cout << sum; } int main(){ cin >> n >> m>> p; if(n==1){ long long sum = 0; for(int i=1; i<=m; i++){ sum = (sum + i )%p; } cout << sum; }else{ create_states(); paint(); give_answer(); } }
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #include<iostream> #include<vector> using namespace std; long long n,m,p; vector<vector<long long> > lookup; vector<long long> previ; vector<long long> nexti; vector<long long> zeros; void create_lookup(int u, int d){ long long id = 0; for(int i=1; i<=m; i++){ for(int j=m-1; j>=0; j--){ if(j-(i-1)>=0){ if(d<=j&&u>=j-(i-1)){ lookup.back().push_back(id); } id++; } } } } void create_states(){ for(int i=1; i<=m; i++){ for(int j=m-1; j>=0; j--){ if(j-(i-1)>=0){ lookup.push_back(vector<long long>()); previ.push_back(1); nexti.push_back(1); zeros.push_back(0); create_lookup(j, j-(i-1)); }else{ break; } } } } void paint(){ for(int i=0; i<n-1; i++){ previ = nexti; nexti = zeros; for(int j=0; j<lookup.size(); j++){ for(auto s: lookup[j]){ nexti[s] = (nexti[s] + previ[j])%p; } } } } void give_answer(){ long long sum = 0; for(int i=0; i<nexti.size(); i++){ sum = (sum+nexti[i])%p; } cout << sum; } int main(){ cin >> n >> m>> p; if(n==1){ long long sum = 0; for(int i=1; i<=m; i++){ sum = (sum + i )%p; } cout << sum; }else{ create_states(); paint(); give_answer(); } } |