#include"message.h" #include"futbol.h" #include<iostream> #include<vector> using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef vector< ii > vii; typedef vector< pair < ii, int > > viii; typedef vector< vector < ii > > vvii; typedef pair < pair < int, int >, int > iii; typedef pair < pair < int, int >, pair < int, int > > iiii; typedef unsigned long long ull; typedef long long ll; typedef unsigned long long ull; typedef vector< ll > vll; typedef long double ld; #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define st first #define nd second #define rep(i,n) for(int i=0; i<(n); ++i) #define rall(c) (c).rbegin(), (c).rend() #define FOR(i, a, b) for (int (i)=(a); (i)<(b); (i)++) #define FOR2(i, a, b) for (int (i)=(a); (i)>=(b); (i)--) template<typename T> inline void setmin(T &x, T y) { if (y < x) x = y; } template<typename T> inline void setmax(T &x, T y) { if (y > x) x = y; } template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; } const int MAX = 1e6 + 7; const int T = 1 << 20; const int INF = 1e9 + 7; const ll BIG_INF = 1e18 + 5; ld e = 2.7182818284590452353602874713526624; //ld PI = acos(-1); ld eps = 1e-19; ll n , k , p; ll wynik; ll wynik_pop = 1; ll ile_przed; ll max_odw = 1; ll max_gora = 1; ll pop_max_odw = 1; ll pop_max_gora = 1; ll koniec = 0; const int MM = 10000000; ll tab[MM]; ll fast( ll a , ll x , ll mod ){ ll ret = 1; ll pom = a; while( x ){ if( x & 1 ){ ret = (ret * pom) % mod; } pom = (pom * pom) % mod; x >>= 1; } return ret; } void compute(){ ll curr = 1; ll silnia = 1; ll pom = n - ile_przed; for( ll i = ile_przed ; i < min( k , ile_przed + MM) ; ++i){ silnia = (silnia * (i + 1) ) % p; curr = ( curr * pom ) % p; tab[ i % MM] = curr; pom--; if( k == i + 1 ){ koniec = 1; break; } } silnia = fast( silnia , p-2 , p); max_odw = silnia; max_gora = curr; for( ll i = min( k , ile_przed + MM) - 1 ; i >= ile_przed ; --i){ wynik = ( wynik + tab[ i % MM ] * silnia ) % p; silnia = ( silnia * (i+1) ) % p; } } int main(){ boost; ile_przed = MyNodeId() * MM; n = GetN(); k = GetK(); p = GetP(); if( k == 0 ){ if( MyNodeId() == 0 ){ cout << 1 << endl; return 0; } return 0; } compute(); ll pierwsza = 1; if( MyNodeId() > 0 ){ Receive( MyNodeId() - 1); pierwsza = GetLL( MyNodeId() - 1 ); } if( pierwsza == 1 ){ if( MyNodeId() > 0 ){ pop_max_odw = GetLL( MyNodeId() - 1); pop_max_gora = GetLL( MyNodeId() - 1); wynik_pop = GetLL( MyNodeId() - 1); } wynik = ( ( ( wynik * pop_max_gora ) % p ) * pop_max_odw ) % p; wynik = ( wynik + wynik_pop ) % p; max_odw = ( max_odw * pop_max_odw ) % p; max_gora = ( max_gora * pop_max_gora ) % p; if( koniec ){ cout << wynik << endl; if( MyNodeId() != 99 ){ PutLL( MyNodeId() + 1 , 0 ); Send( MyNodeId() + 1 ); } } else{ PutLL( MyNodeId() + 1 , 1 ); PutLL( MyNodeId() + 1 , max_odw ); PutLL( MyNodeId() + 1 , max_gora ); PutLL( MyNodeId() + 1 , wynik ); Send( MyNodeId() + 1 ); } } else{ if( MyNodeId() < 99 ){ PutLL( MyNodeId() + 1 , 0 ); Send( MyNodeId() + 1 ); } } }
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include"message.h" #include"futbol.h" #include<iostream> #include<vector> using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef vector< ii > vii; typedef vector< pair < ii, int > > viii; typedef vector< vector < ii > > vvii; typedef pair < pair < int, int >, int > iii; typedef pair < pair < int, int >, pair < int, int > > iiii; typedef unsigned long long ull; typedef long long ll; typedef unsigned long long ull; typedef vector< ll > vll; typedef long double ld; #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define st first #define nd second #define rep(i,n) for(int i=0; i<(n); ++i) #define rall(c) (c).rbegin(), (c).rend() #define FOR(i, a, b) for (int (i)=(a); (i)<(b); (i)++) #define FOR2(i, a, b) for (int (i)=(a); (i)>=(b); (i)--) template<typename T> inline void setmin(T &x, T y) { if (y < x) x = y; } template<typename T> inline void setmax(T &x, T y) { if (y > x) x = y; } template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; } const int MAX = 1e6 + 7; const int T = 1 << 20; const int INF = 1e9 + 7; const ll BIG_INF = 1e18 + 5; ld e = 2.7182818284590452353602874713526624; //ld PI = acos(-1); ld eps = 1e-19; ll n , k , p; ll wynik; ll wynik_pop = 1; ll ile_przed; ll max_odw = 1; ll max_gora = 1; ll pop_max_odw = 1; ll pop_max_gora = 1; ll koniec = 0; const int MM = 10000000; ll tab[MM]; ll fast( ll a , ll x , ll mod ){ ll ret = 1; ll pom = a; while( x ){ if( x & 1 ){ ret = (ret * pom) % mod; } pom = (pom * pom) % mod; x >>= 1; } return ret; } void compute(){ ll curr = 1; ll silnia = 1; ll pom = n - ile_przed; for( ll i = ile_przed ; i < min( k , ile_przed + MM) ; ++i){ silnia = (silnia * (i + 1) ) % p; curr = ( curr * pom ) % p; tab[ i % MM] = curr; pom--; if( k == i + 1 ){ koniec = 1; break; } } silnia = fast( silnia , p-2 , p); max_odw = silnia; max_gora = curr; for( ll i = min( k , ile_przed + MM) - 1 ; i >= ile_przed ; --i){ wynik = ( wynik + tab[ i % MM ] * silnia ) % p; silnia = ( silnia * (i+1) ) % p; } } int main(){ boost; ile_przed = MyNodeId() * MM; n = GetN(); k = GetK(); p = GetP(); if( k == 0 ){ if( MyNodeId() == 0 ){ cout << 1 << endl; return 0; } return 0; } compute(); ll pierwsza = 1; if( MyNodeId() > 0 ){ Receive( MyNodeId() - 1); pierwsza = GetLL( MyNodeId() - 1 ); } if( pierwsza == 1 ){ if( MyNodeId() > 0 ){ pop_max_odw = GetLL( MyNodeId() - 1); pop_max_gora = GetLL( MyNodeId() - 1); wynik_pop = GetLL( MyNodeId() - 1); } wynik = ( ( ( wynik * pop_max_gora ) % p ) * pop_max_odw ) % p; wynik = ( wynik + wynik_pop ) % p; max_odw = ( max_odw * pop_max_odw ) % p; max_gora = ( max_gora * pop_max_gora ) % p; if( koniec ){ cout << wynik << endl; if( MyNodeId() != 99 ){ PutLL( MyNodeId() + 1 , 0 ); Send( MyNodeId() + 1 ); } } else{ PutLL( MyNodeId() + 1 , 1 ); PutLL( MyNodeId() + 1 , max_odw ); PutLL( MyNodeId() + 1 , max_gora ); PutLL( MyNodeId() + 1 , wynik ); Send( MyNodeId() + 1 ); } } else{ if( MyNodeId() < 99 ){ PutLL( MyNodeId() + 1 , 0 ); Send( MyNodeId() + 1 ); } } } |