#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include "futbol.h" #include "message.h" #include <iostream> #include <chrono> #include <unistd.h> using namespace std; const int O = 10000005; struct solution{ solution(){ instance_number = MyNodeId(); P = GetP(); K = GetK(); prepare(); if(instance_number == 0){ handle_input(1,1,N); send(); } else { receive(); } } int N; void send(){ PutInt(instance_number + 1, send1()); PutInt(instance_number + 1, send2()); PutInt(instance_number + 1, N); Send(instance_number + 1); } void receive(){ int w = instance_number - 1; Receive(w); handle_input(GetInt(w),GetInt(w), GetInt(w)); if (instance_number < 99)send(); else { cout << send1() << '\n'; } } int Ae = 1, Be = 0, Ce = 1; int P, K; int instance_number; int start_of_interval; int end_of_interval; int to_send_f1 = 0, to_send_a2 = 0; int send1(){ return to_send_f1; } int send2(){ return to_send_a2; } void mul(int &a, int b){ a = ((long long)a * b)%P; } int power(int a, int b){ int res = 1; while(b){ if(b&1){ mul(res, a); } mul(a,a); b >>= 1; } return res; } int od(int b){ return power(b, P-2); } /*int od(int b){ long long t = 0; long long r = P; long long new_t = 1; long long new_r = b; while(new_r != 0){ long long q = r/new_r; long long a = t; t = new_t; new_t = a - q*t; a = r; r = new_r; new_r = a - q*r; if (t < 0) t += P; } t %= P; if (t < 0) t += P; //cerr <<" result of inv of " << b << " is " << t << ' ' << ((long long)b *t)%P << '\n'; return t; }*/ void add(int &a, int b){ a += b; a %= P; } int A(int j){ Ae = 1; for(int i = 0; i < j; ++i){ mul(Ae, 2); } return Ae; } int B(int j){ Be = 0; Ce = 0; if(start_of_interval >= K){ Ce = 1; } for(int i = 0; i < j; ++i){ mul(Be, 2); add(Be, P-Ce); int w = start_of_interval + i; if(w + 1 > K){ mul(Ce, w+1); mul(Ce, od(w+1-K)); } else if(w + 1 == K){ Ce = 1; } } return Be; } void prepare(){ start_of_interval = O*instance_number; end_of_interval = O*(instance_number + 1); Ae = 1; Be = 0; Ce = 0; if(start_of_interval >= K){ Ce = 1; } for(int i = 0; i < O; ++i){ mul(Ae, 2); } for(int i = 0; i < O; ++i){ mul(Be, 2); add(Be, P-Ce); int w = start_of_interval + i; if(w + 1 > K){ mul(Ce, w+1); mul(Ce, od(w+1-K)); } else if(w + 1 == K){ Ce = 1; } } } void handle_input(int x, int y, int n){ N = n; if(n >= end_of_interval){ mul(x, Ae); to_send_a2 = y; mul(to_send_a2, Ce); mul(y, Be); add(x, y); to_send_f1 = x; } else if(n < start_of_interval){ to_send_f1 = x; } else { mul(x, A(n - start_of_interval)); mul(y, B(n - start_of_interval)); add(x, y); to_send_f1 = x; } } }; /* std::chrono::steady_clock::time_point stb = std::chrono::steady_clock::now(); void print_time(){ std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now(); cout << "Elapsed time in milliseconds : " << chrono::duration_cast<chrono::milliseconds>(end - stb).count() << " ms" << endl; }*/ int main(){ ios_base::sync_with_stdio(0); solution A(); }
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include "futbol.h" #include "message.h" #include <iostream> #include <chrono> #include <unistd.h> using namespace std; const int O = 10000005; struct solution{ solution(){ instance_number = MyNodeId(); P = GetP(); K = GetK(); prepare(); if(instance_number == 0){ handle_input(1,1,N); send(); } else { receive(); } } int N; void send(){ PutInt(instance_number + 1, send1()); PutInt(instance_number + 1, send2()); PutInt(instance_number + 1, N); Send(instance_number + 1); } void receive(){ int w = instance_number - 1; Receive(w); handle_input(GetInt(w),GetInt(w), GetInt(w)); if (instance_number < 99)send(); else { cout << send1() << '\n'; } } int Ae = 1, Be = 0, Ce = 1; int P, K; int instance_number; int start_of_interval; int end_of_interval; int to_send_f1 = 0, to_send_a2 = 0; int send1(){ return to_send_f1; } int send2(){ return to_send_a2; } void mul(int &a, int b){ a = ((long long)a * b)%P; } int power(int a, int b){ int res = 1; while(b){ if(b&1){ mul(res, a); } mul(a,a); b >>= 1; } return res; } int od(int b){ return power(b, P-2); } /*int od(int b){ long long t = 0; long long r = P; long long new_t = 1; long long new_r = b; while(new_r != 0){ long long q = r/new_r; long long a = t; t = new_t; new_t = a - q*t; a = r; r = new_r; new_r = a - q*r; if (t < 0) t += P; } t %= P; if (t < 0) t += P; //cerr <<" result of inv of " << b << " is " << t << ' ' << ((long long)b *t)%P << '\n'; return t; }*/ void add(int &a, int b){ a += b; a %= P; } int A(int j){ Ae = 1; for(int i = 0; i < j; ++i){ mul(Ae, 2); } return Ae; } int B(int j){ Be = 0; Ce = 0; if(start_of_interval >= K){ Ce = 1; } for(int i = 0; i < j; ++i){ mul(Be, 2); add(Be, P-Ce); int w = start_of_interval + i; if(w + 1 > K){ mul(Ce, w+1); mul(Ce, od(w+1-K)); } else if(w + 1 == K){ Ce = 1; } } return Be; } void prepare(){ start_of_interval = O*instance_number; end_of_interval = O*(instance_number + 1); Ae = 1; Be = 0; Ce = 0; if(start_of_interval >= K){ Ce = 1; } for(int i = 0; i < O; ++i){ mul(Ae, 2); } for(int i = 0; i < O; ++i){ mul(Be, 2); add(Be, P-Ce); int w = start_of_interval + i; if(w + 1 > K){ mul(Ce, w+1); mul(Ce, od(w+1-K)); } else if(w + 1 == K){ Ce = 1; } } } void handle_input(int x, int y, int n){ N = n; if(n >= end_of_interval){ mul(x, Ae); to_send_a2 = y; mul(to_send_a2, Ce); mul(y, Be); add(x, y); to_send_f1 = x; } else if(n < start_of_interval){ to_send_f1 = x; } else { mul(x, A(n - start_of_interval)); mul(y, B(n - start_of_interval)); add(x, y); to_send_f1 = x; } } }; /* std::chrono::steady_clock::time_point stb = std::chrono::steady_clock::now(); void print_time(){ std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now(); cout << "Elapsed time in milliseconds : " << chrono::duration_cast<chrono::milliseconds>(end - stb).count() << " ms" << endl; }*/ int main(){ ios_base::sync_with_stdio(0); solution A(); } |