#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"
/*
int GetN() { return int(1e7); }
int GetElement(int i) { return (i * 1ll * i) % 5 + 1; }
int X[10]= {5, 2, 4, 4, 3};
int GetN(){return 5;}
int GetElement(int i){return X[i];}
*/
using namespace std;
typedef long long LL;
const int M = 1000009;
int d[M];
void insert(int w, int x){
for(;x<M;x+=x&-x)
d[x]+=w;
}
int query(int x){
int res=0;
for(;x>0;x-=x&-x)
res+=d[x];
return res;
}
int tab[10];
int tab2[10];
int main() {
int n = GetN();
LL res=0;
int myNode = 0;
myNode = MyNodeId();
int liczbaNodow = 1;
liczbaNodow = NumberOfNodes();
if (n < 10000009){
if(myNode != 0) return 0;
for (int i=0; i<n; i++){
int x = GetElement(i);
res+=query(M)-query(x);
insert(1, x);
}
cout<<res<<"\n";
}
else{
for (int i=myNode*1101000; i<min(n, (1+myNode)*1101000); i++){
int x = GetElement(i);
tab[x]++;
res+=query(M)-query(x);
insert(1, x);
}
if (myNode==0){
for(int i=0; i<6; i++){
PutInt(1, tab[i]);
}
Send(1);
for(int i=1; i<liczbaNodow; i++){
Receive(i);
res+=GetLL(i);
}
cout<<res<<"\n";
}
else if(myNode != liczbaNodow-1){
int m = Receive(myNode-1);
for(int i=0; i<6; i++){
tab2[i] = GetInt(myNode-1);
}
for(int i=0; i<6; i++){
for(int j=i+1; j<6; j++){
res+=LL(tab[i])*tab2[j];
}
}
for(int i=0; i<6; i++)
tab[i]+=tab2[i];
for(int i=0; i<6; i++){
PutInt(myNode+1, tab[i]);
}
Send(myNode+1);
PutLL(0, res);
Send(0);
}
else{
int m = Receive(myNode-1);
for(int i=0; i<6; i++){
tab2[i] = GetInt(myNode-1);
}
for(int i=0; i<6; i++){
for(int j=i+1; j<6; j++){
res+=LL(tab[i])*tab2[j];
}
}
PutLL(0, res);
Send(0);
}
}
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 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 | #include "message.h" #include "teatr.h" #include "bits/stdc++.h" /* int GetN() { return int(1e7); } int GetElement(int i) { return (i * 1ll * i) % 5 + 1; } int X[10]= {5, 2, 4, 4, 3}; int GetN(){return 5;} int GetElement(int i){return X[i];} */ using namespace std; typedef long long LL; const int M = 1000009; int d[M]; void insert(int w, int x){ for(;x<M;x+=x&-x) d[x]+=w; } int query(int x){ int res=0; for(;x>0;x-=x&-x) res+=d[x]; return res; } int tab[10]; int tab2[10]; int main() { int n = GetN(); LL res=0; int myNode = 0; myNode = MyNodeId(); int liczbaNodow = 1; liczbaNodow = NumberOfNodes(); if (n < 10000009){ if(myNode != 0) return 0; for (int i=0; i<n; i++){ int x = GetElement(i); res+=query(M)-query(x); insert(1, x); } cout<<res<<"\n"; } else{ for (int i=myNode*1101000; i<min(n, (1+myNode)*1101000); i++){ int x = GetElement(i); tab[x]++; res+=query(M)-query(x); insert(1, x); } if (myNode==0){ for(int i=0; i<6; i++){ PutInt(1, tab[i]); } Send(1); for(int i=1; i<liczbaNodow; i++){ Receive(i); res+=GetLL(i); } cout<<res<<"\n"; } else if(myNode != liczbaNodow-1){ int m = Receive(myNode-1); for(int i=0; i<6; i++){ tab2[i] = GetInt(myNode-1); } for(int i=0; i<6; i++){ for(int j=i+1; j<6; j++){ res+=LL(tab[i])*tab2[j]; } } for(int i=0; i<6; i++) tab[i]+=tab2[i]; for(int i=0; i<6; i++){ PutInt(myNode+1, tab[i]); } Send(myNode+1); PutLL(0, res); Send(0); } else{ int m = Receive(myNode-1); for(int i=0; i<6; i++){ tab2[i] = GetInt(myNode-1); } for(int i=0; i<6; i++){ for(int j=i+1; j<6; j++){ res+=LL(tab[i])*tab2[j]; } } PutLL(0, res); Send(0); } } return 0; } |
English