#include<cstdio> #include<algorithm> #include<cstring> #define MAXN 200005 #define P 16777216 #define rint register int #define LL long long usingnamespace std;
int n, sum1, sum2, sum3, sum4, y[MAXN], l[MAXN], r[MAXN], t[MAXN];
voidadd(int x, int y) { for (; x <= n; x += (x & -x)) t[x] = (t[x] + y) % P; }
intask(int x) { int ans = 0; for (; x; x -= (x & -x)) ans = (ans + t[x]) % P; return ans; }
intcomb(int x, int y) { LL up = 1, down = 1; for (rint i = 0; i < y; ++i) up *= (x - i), down *= (i + 1); return up / down % P; }
intmain() { scanf("%d", &n); for (rint i = 1; i <= n; ++i) scanf("%d", &y[i]); for (rint i = 1; i <= n; ++i) { l[i] = ask(y[i]); r[i] = y[i] - l[i] - 1; add(y[i], 1); } for (rint i = 1; i <= n; ++i) { sum1 = (sum1 + comb(n - r[i] - i, 3)) % P; } memset(t, 0, sizeof(t)); for (rint i = 1; i <= n; ++i) { sum2 = (sum2 + 1LL * (n - r[i] - i) * ask(y[i]) % P) % P; add(y[i], l[i]); } memset(t, 0, sizeof(t)); for (rint i = n; i >= 1; --i) { sum3 = (sum3 + 1LL * (n - r[i] - i) * (ask(y[i]) - comb(r[i], 2) + P) % P) % P; add(y[i], y[i] - 1); } memset(t, 0, sizeof(t)); for (rint i = 1; i <= n; ++i) { sum4 = (sum4 + 1LL * (n - r[i] - i) * (1LL * (i - 1) * l[i] - comb(l[i], 2) - ask(y[i])) % P + P) % P; add(y[i], i); } printf("%d\n", (sum2 + sum3 + sum4 - sum1 + P) % P); }
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<map> #include<set> #define MAXN 200005 #define P #define INF 0x3f3f3f3f #define rint register int #define LL long long #define LD long double usingnamespace std;
intksm(int x, int y) { int ans=1; while(y) { if(y&1) ans=1LL*ans*x%p; y>>=1; x=1LL*x*x%p; } return ans; }
intbsgs(int a, int b) { int siz=sqrt(p)+0.5, base=1; int t=b; mp[t]=0; for(rint i=1; i<=siz; ++i) { t=1LL*t*a%p; mp[t]=i; base=1LL*base*a%p; } t=1; for(rint i=1; i<=siz; ++i) { t=1LL*t*base%p; if(mp.count(t)) { int x=i*siz-mp[t]; return x; } } return-1; }
intexgcd(int a, int b, LL& x, LL& y) { if(!b) { x=1, y=0; return a; } int d=exgcd(b, a%b, x, y); int temp=x; x=y, y=temp-a/b*y; return d; }
intsolve(int a, int b, int c) { LL x, y; int d=exgcd(a, b, x, y); //printf("%d %d %d %d!!!\n", a, b, c, d); if(c%d) return-1; x*=c/d; return (x%(b/d)+(b/d))%(b/d); }