There are two approaches to this problem:
1: Constructs the matrix, according to the Fibonacci sequence sequence analogy, obtains the matrix after the direct solution out according to the FI T (because the FI is less than 19960515 so the process will not go "%", so can be directly divided by the coefficient, not
Using the inverse element), and then using the matrix to calculate the FJ
|fn+1 | |-1|n * |f1|
|FN | |-1| * |f0|
2: Find a regular push nature:
F0 = 1
F1 = 1t
F2 = 1t + 1
F3 = 2t + 1
F4 = 3t + 2
F5 = 5t + 3
Then the induction fn = fn-1*t + Fn-2 fn is the Fibonacci sequence nth
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace std;const int mod = 19960515;struct matrix{int map[3][3];void init (BOOL flag) {memset (map,0,sizeof (map)); if (flag) for ( int i = 0;i < 3;++i) Map[i][i] = 1;}} Ori,pou; Matrix Mul (Matrix X,matrix y) {matrix ret;ret.init (0); for (int i = 0;i < 3;++i) {for (int j = 0;j < 3;++j) {for (int k = 0 K < 3;++k) {Ret.map[i][j] = (ret.map[i][j]+ (long Long) x.map[i][k]*y.map[k][j])%mod;}}} return ret;} Matrix Pow (Matrix X,int cnt) {matrix ret;ret.init (1), while (CNT) {if (CNT & 1) ret = Mul (ret,x); x = Mul (x,x); cnt>>= 1;} return ret;} int main () {freopen ("gibonacci.in", "R", stdin), Freopen ("Gibonacci.out", "w", stdout); int t,xi,i,j,t;scanf ("%d",& T); Ori.map[0][0] = 1,ori.map[0][1] = 1;ori.map[1][0] = 1,ori.map[1][1] = 0;while (t--) {scanf ("%d%d%d", &i,&xi, &J); Matrix ret;ret.init (0); ret = Pow (ori,i), if ((xi-ret.map[1][1])% ret.map[1][0]! = 0) {printf (" -1\n"); continue;} t = (xi-ret.map[1][1])/ret.map[1][0]; Matrix k;k.init (0); k.map[0][0] = t;k.map[1][0] = 1;ret = Pow (ori,j), ret = Mul (ret,k);p rintf ("%d\n", Ret.map[1][0]);} return 0;}
is a bomb search, but this one-time items need to record status and backtracking.
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring>using namespace std;const int inf = 1<<30;int ans = inf;int hash[1010][30][100],t,n,m,h,s;const int dx[] = { -1,1,0,0};const int dy[] = {0,0,-1,1}; #define Unlegal (x, y) x<0 | | X>=n | | y<0 | | Y>=mchar map[20][20];void dfs (int pos,int hp,int kill) {if (HP < 0) return;if (Kill > Ans) return;if (hash[pos][hp][ Kill] = = T) Return;hash[pos][hp][kill] = T;int x = pos/m,y = pos%m;for (int i = 0;i < 4;++i) {int _x = X+dx[i], _y = y+dy[ I];if (Unlegal (_x,_y)) continue;if (map[_x][_y] = = ' # ') continue;if (map[_x][_y] = = '. ' && Hash[_x*m+_y][hp][kill ]!=t) DFS (_x*m+_y,hp,kill), if (map[_x][_y] = = ' m ') {map[_x][_y] = '. '; DFS (_x*m+_y,hp-1,kill+1); map[_x][_y] = ' m ';} if (map[_x][_y] = = ' C ' && hash[_x*m+_y][hp+5][kill]!=t) {map[_x][_y] = '. '; DFS (_x*m+_y,hp+5,kill); map[_x][_y] = ' C ';} if (map[_x][_y] = = ' E ') {ans = min (ans,kill); return;}}} int main () {freopen ("tower.in", "R", stdin); Freopen ("ToweR.out "," w ", stdout), while (scanf ("%d%d%d ", &h,&n,&m)!=eof) {++t;ans = inf;for (int i = 0;i < N;++i) {scanf (" %s ", Map[i]); for (int j = 0;j < M;++j) {if (map[i][j] = = ' s ') s = i*m+j,map[i][j] = '. ';}} DFS (s,h,0), if (ans = = inf) puts ("Poor Warrior"), Else printf ("%d\n", ans); return 0;}
This question first analyzes the topic, meaning that the referee is required to be between two points and the level is between two people, then for each person can be a referee, then assume that the person at I this point, I before the X personal level is lower than him, then there is i-x-1 personal level higher than he, back the same, After I have Y personal level lower than him, then there is n-y-i personal higher than him, then the answer is (n-y-i) * (x) + (i-x-1) *y, how to maintain a period of a smaller number of R number? It's easy to think of a tree-like array.
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace std;const int N = 200010; #define CLR (A, B) memset (A,b,sizeof (a)) #define LOWBIT (i) I&-ilong long t,n,a[n],x[n],y[n]; struct Bit{int c[n];void init () {clr (c,0);} void update (int x) {for (int i = x;i <= n;i + lowbit (i)) {C[i] + = 1;}} int query (int x) {int sum = 0;for (int i = x;i >= 1;i-= Lowbit (i)) {sum + = C[i];} return sum;}} F,b;int Main () {freopen ("inhouse.in", "R", stdin), Freopen ("Inhouse.out", "w", stdout), scanf ("%i64d", &t), while ( t--) {f.init (), B.init (), CLR (a,0), CLR (x,0), CLR (y,0), scanf ("%d", &n), for (int i = 1;i <= n;++i) scanf ("%i64d", &a[i]); for (int i = 1;i <= n;++i) {X[i] = F.query (A[i]); F.update (A[i]);} for (int i = N;i >= 1;--i) {Y[i] = B.query (A[i]); B.update (A[i]);} Long Long ans = 0;for (int i = 1;i <= n;++i) {ans = ans+ (i-x[i]-1) *y[i]+ (N-i-y[i]) *x[i];} Cout<<ans<<endl;} return 0;}
NOIP Hzwer Second-xgtao-