[HDU-3487]Play with Chain
F – Play with Chain Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Description YaoYao is fon…
Read More »[ POJ2187]Beauty Contest——旋转卡壳初步
题目传送门—-> Beauty Contest Description Bessie, Farmer John’s prize cow, has just won first place in a bovine beauty contest, earning the title ‘Miss Cow World’. As a result, Bessie will make a tour …
Read More »Splay[转]
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <cstdlib> #include <vector> using namespace std; #define ll ch[x][0] #define rr ch[x][1] #define KT (ch[ch[rt][1]][0]) const int maxn = 300100; int a[maxn], flage[maxn]; int n, m; struct SplayTree{ ///基本数据定义 int ch[maxn][2]; int sz[maxn], pre[maxn]; int rt, tot; ///题意数据定义 int val[maxn], flg[maxn], GCD[maxn][2]; ///Splay树的基本旋转操作函数 void Rotate(int x, int f) { int y = pre[x]; // down(y); down(x);/// ch[y][!f] = ch[x][f]; pre[ch[x][f]] = y; pre[x] = pre[y]; if (pre[x]) ch[pre[y]][ch[pre[y]][1] == y] = x; ch[x][f] = y; pre[y] = x; up(y);/// } void Splay(int x, int goal) { // down(x);/// while (pre[x] != goal) { if (pre[pre[x]] == goal) Rotate(x, ch[pre[x]][0] == x); else { int y = pre[x], z = pre[y]; int f = ( ch[z][0] == y ); if (ch[y][f] == x) Rotate(x, !f), Rotate(x, f); else Rotate(y, f), Rotate(x, f); } } up(x);/// if (goal == 0) rt = x; } void RTO(int k, int goal) { int x = rt; while (sz[ll] + 1 != k) { if (k < sz[ll] + 1) x = ll; else { k -= (sz[ll] + 1); x = rr; } } Splay(x, goal); } ///Splay树的生成函数 void newnode(int &x, int c, int state, int f)/// { x = ++tot; ll = rr = 0; sz[x] = 1; pre[x] = f; val[x] = c; flg[x] = state; GCD[x][state] = c; GCD[x][!state] = -1; } void build(int &x, int l, int r, int f)/// { if (l > r) return ; int m = (l + r) >> 1; newnode(x, a[m], flage[m], f); build(ll, l, m - 1, x); build(rr, m + 1, r, x); up(x); } void Init(int n) { ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0; GCD[0][0] = GCD[0][1] = -1; flg[0] = val[0] = 0;///???? rt = tot = 0; newnode(rt, -1, 0, 0); newnode(ch[rt][1], -1, 0, rt); build(KT, 1, n, ch[rt][1]);/// up(ch[rt][1]); up(rt);/// } ///区间操作相关up(), down() void up(int x)///!!! { sz[x] = sz[ll] + sz[rr] + 1;///!!! GCD[x][flg[x]] = val[x];///!!!! GCD[x][!flg[x]] = -1; for (int i = 0; i < 2; i++) { if (GCD[ll][i] != -1) { if (GCD[x][i] != -1) GCD[x][i] = __gcd(GCD[x][i], GCD[ll][i]); else GCD[x][i] = GCD[ll][i]; } if (GCD[rr][i] != -1) { if (GCD[x][i] != -1) GCD[x][i] = __gcd(GCD[x][i], GCD[rr][i]); else GCD[x][i] = GCD[rr][i]; } } } ///区间查询 void solve_Q(int L, int R, int state) { RTO(L, 0); RTO(R + 2, rt); printf("%d\n", GCD[KT][state]); } ///插入一个节点 void solve_I(int L, int value, int state) { RTO(L + 1, 0); RTO(L + 2, rt); int x; newnode(x, value, state, ch[rt][1]);///!!! KT = x; up(ch[rt][1]); up(rt); } ///删除一个节点 void solve_D(int L) { RTO(L, 0); RTO(L + 2, rt); KT = 0; up(ch[rt][1]); up(rt); } ///更改节点信息 void solve_R(int L) { RTO(L + 1, 0); flg[rt] ^= 1; up(rt); } void solve_M(int L, int value) { RTO(L + 1, 0); val[rt] = value; up(rt); } ///题意相关函数 void solve(int m) { char op; int L, R, state, value; while (m--) { scanf(" %c", &op); if (op == 'Q') { scanf("%d%d%d", &L, &R, &state); solve_Q(L, R, state); } else if (op == 'I') { scanf("%d%d%d", &L, &value, &state); solve_I(L, value, state); } else if (op == 'D') { scanf("%d", &L); solve_D(L); } else if (op == 'R') { scanf("%d", &L); solve_R(L); } else if (op == 'M') { scanf("%d%d", &L, &value); solve_M(L, value); } } } }sp; int main() { while (cin >> n >> m) { for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &flage[i]); sp.Init(n); sp.solve(m); } } |
感觉很不错的样子,转过来看看。
Read More »插值求多项式系数
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 |
#include <iostream> #include <cstring> #include <algorithm> #include <cstring> #include <map> #include <vector> #include <cmath> using namespace std; const int maxm=100; const int mod=1e9+7; const double EPS=1e-8; typedef vector<double> vec; typedef vector<vec> mat; vec gauss_jordan(const mat&A,const vec&b) { int n=A.size(); mat B(n,vec(n+1)); for (int i=0;i<n;i++) { for (int j=0;j<n;j++) B[i][j]=A[i][j]; } for (int i=0;i<n;i++) B[i][n]=b[i]; for (int i=0;i<n;i++) { int pivot=i; for (int j=i;j<n;j++) { if (abs(B[j][i])>abs(B[pivot][i])) pivot=j; } swap(B[i],B[pivot]); if (abs(B[i][i])<EPS) return vec(); for (int j=i+1;j<=n;j++) B[i][j]/=B[i][i]; for (int j=0;j<n;j++) { if (i!=j) { for (int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k]; } } } vec x(n); for (int i=0;i<n;i++) { x[i]=B[i][n]; } return x; } mat cheng(mat m,mat mm) { int n=m.size(); mat res(n,vec(n)); for (int i=0;i<n;i++) { for (int j=0;j<n;j++) { for (int k=0;k<n;k++) { res[i][k]=res[i][k]+m[i][j]*mm[j][k]; } } } return res; } mat qpow(mat m,long long b) { int n=m.size(); mat ret(n,vec(n)); for (int i=0;i<n;i++) { ret[i][i]=1; } while(b) { if (b&1) ret=cheng(ret,m); m=cheng(m,m); b/=2; } return ret; } long long a[maxm]; long long sum[maxm];//插值的公式值 map<int,int> f; long long get(int n) { int crt=1; while(f[crt]) { crt++; } return crt; } const int maxn=50;//项数 mat A(maxn,vec(maxn)); vec b(maxn); int main() { a[1]=1; a[2]=2; f[1]=1; sum[1]=1; sum[2]=3; for (int i=3;i<maxm;i++) { if (i%2) { a[i]=2*a[i-1]; } else { int crt=get(i); a[i]=a[i-1]+crt; } for (int j=1;j<i;j++) { f[a[i]-a[j]]=1; } a[i]%=mod; sum[i]=(a[i]+sum[i-1]) % mod; } for (int i=maxn;i<=maxn+maxn-1;i++) { b[i-maxn]=sum[i]; } for (int i=0;i<maxn;i++) { for (int j=0;j<maxn-1;j++) { A[i][j]=sum[i+j+1]; } A[i][maxn-1]=1; } vec x=gauss_jordan(A,b); for (int i=0;i<maxn;i++) cout<<x[i]<<endl; mat B(maxn,vec(maxn)); for (int i=0;i<maxn-1;i++) { B[0][i]=x[maxn-2-i]; } B[0][maxn-1]=x[maxn-1]; for (int i=1;i<maxn;i++) { for (int j=0;j<maxn;j++) { B[i][j]=0; } if (i<maxn-1) B[i][i-1]=1; else B[i][i]=1; } for (int i=0;i<maxn;i++) { for (int j=0;j<maxn;j++) { cout<<B[i][j]<<" "; } cout<<endl; } for (int i=maxn;i<100;i++) { mat ret=qpow(B,i-maxn+1); double ans=0; for (int j=0;j<maxn-1;j++) { ans+=sum[maxn-1-j]*ret[0][j]; } ans+=ret[0][maxn-1]; cout<<ans<<" "<<sum[i]<<endl; } 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 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
//最大公约数 long long gcd(long long a,long long b) { return b==0?a:gcd(b,a%b); } //大整数相乘取模 long long multi(long long a,long long b,long long mod) { long long ret=0; while(b) { if (b&1) ret=(ret+a) %mod; a=(a<<1)%mod; b=b>>1; } return ret; } //快速幂1 long long quickpow(long long a,long long b,long long mod) { long long ret=1; while(b) { if (b&1) ret=multi(ret,a,mod); a=multi(a,a,mod); b>>=1; } return ret; } //快速幂2 long long quick_pow(long long a,long long b) { long long res=1; while(b>0) { if (b&1) res=(res*a)%p; a=(a*a)%p; b>>=1; } return res; } //欧拉函数 long long eular(long long n) { long long ret=1,i; for (i=2;i*i<=n;i++) { if (n%i==0) { n=n/i; ret*=i-1; while(n%i==0) { n=n/i; ret*=i; } } } if (n>1) ret*=n-1; return ret; } //预处理maxn范围内的欧拉函数 void euler_pre() { for (int i = 0; i <= maxn; ++i) euler[i] = i; for (int i = 2; i <= maxn; ++i) { if (euler[i] == i) { for (int j = i; j <= maxn; j += i) euler[j] = euler[j] / i * (i - 1); } } } //求一个数的所有约数,返回约数个数,约数存在c数组 int getdivision(int n) { int i; int length=0; for(i=1;i*i<n;i++){ if(n%i == 0){ c[length ++] = i; c[length ++] = n/i; } } if(i*i == n) c[length ++] = i; return length; } //矩阵(n×n),p 为模数 struct Mat { long long mat[maxn][maxn]; long long n; Mat operator * (Mat b) { Mat c; c.n=n; memset(c.mat, 0, sizeof(c.mat)); int i, j, k; for(k = 0; k < n; ++k) { for(i = 0; i < n; ++i) { for(j = 0; j < n; ++j) { c.mat[i][j] =(c.mat[i][j]+(mat[i][k] * b.mat[k][j]) %p)%p; } } } return c; } }; //矩阵快速幂 Mat quick_mat(Mat a,long long k,long long p;) { Mat c; c.n=a.n; int i, j; for(i = 0; i < a.n; ++i) for(j = 0; j < a.n; ++j) c.mat[i][j] = (i == j); for(; k; k >>= 1) { if(k&1) c = c*a; a = a*a; } return c; } //高斯消元 typedef vector<double> vec; typedef vector<vec> mat; mat A; vec gauss_jordan(int n) { for (int c=0,r=0;c<n;++c,++r) { int pivot =r; for (int i=r+1;i<=n;++i) { if (abs(A[i][c])>abs(A[pivot][c])) pivot = i; } swap(A[r],A[pivot]); for (int j=n;j>=c;--j) A[r][j]/=A[r][c]; for (int i=0;i<=n;i++) { if (i!=r) { for (int k=c+1;k<=n;k++) { A[i][k]-=A[i][c]*A[r][k]; } } } } vec x(n); for (int i=0;i<n;i++) x[i]=A[i][n]; return x; } //组合数取模,Lucas定理,p为模数 LL C(LL n, LL m) { if(m > n) return 0; LL ans = 1; for(int i=1; i<=m; i++) { LL a = (n + i - m) % p; LL b = i % p; ans = ans * (a * quick_mod(b, p-2) % p) % p; } return ans; } LL Lucas(LL n, LL m) { if(m == 0) return 1; return C(n % p, m % p) * Lucas(n / p, m / p) % p; } //莫比乌斯函数 const int maxn=1000000; bool check[maxn+10]; int prime[maxn+10]; int mu[maxn+10]; void moblus() { memset(check,false,sizeof check); mu[1]=1; int tot=0; for (int i=2;i<=maxn;i++) { if (!check[i]) { prime[tot++]=i; mu[i]=-1; } for (int j=0;j<tot;j++) { if (i*prime[j]>maxn) break; check[i*prime[j]]=true; if (i%prime[j]==0) { mu[i*prime[j]]=0; break; } else { mu[i*prime[j]]=-mu[i]; } } } } //扩展欧几里德定理 typedef long long LL; void extend_Euclid(LL a, LL b, LL &x, LL &y) { if(b == 0) { x = 1; y = 0; return; } extend_Euclid(b, a % b,x, y); LL tmp = x; x = y; y = tmp - a / b * y; } //中国剩余定理 typedef long long LL; LL RemindChina(LL a[],LL m[],int k) { LL M = 1; LL ans = 0; for(int i=0; i<k; i++) M *= m[i]; for(int i=0; i<k; i++) { LL x, y; LL Mi = M / m[i]; extend_Euclid(Mi, m[i], x, y); ans = (ans + Mi * x * a[i]) % M; } if(ans < 0) ans += M; return ans; } |
Homework of History
题目: Homework of History The History teacher gives Nobita a homework, explore the history of 1000 yeas ago.So Nobita and Doraemon go back to 1000 years ago through time machine.But there are something …
Read More »Homework of Politics
题目: Homework of Politics After the Politics class, the teacher gives the student a homework.But Nobita and Takeshi don’t like this homework at all.So they decide to play a game, and the person who los…
Read More »splay?
1 |
可能我写了假的splay<img src="http://zhuyeye.cn/wp-content/plugins/kindeditor-for-wordpress/plugins/emoticons/images/17.gif" alt="" border="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 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn=10+2; int f[maxn],ch[maxn][2],key[maxn],cnt[maxn],Size[maxn]; int root,sz; void Clear(int x) { ch[x][0]=ch[x][1]=f[x]=key[x]=cnt[x]=Size[x]=0; return; } int get(int x) { return ch[f[x]][1]==x; } void update(int x) { if (x) { Size[x]=cnt[x]; if (ch[x][0]) Size[x]+=Size[ch[x][0]]; if (ch[x][1]) Size[x]+=Size[ch[x][1]]; } } void Rotate(int x) { int old=f[x],oldf=f[old],which=get(x); ch[old][which]=ch[x][which^1]; f[ch[old][which]]=old; f[0]=0; f[old]=x; ch[x][which^1]=old; f[x]=oldf; if (oldf) { ch[oldf][ch[oldf][1]==old]=x; } update(old); update(x); } void splay(int x) { for (int fa;(fa=f[x]);Rotate(x)) { if (f[fa]) { Rotate((get(x)==get(fa))?fa:x); } } root=x; } void Insert(int v) { if (root==0) { sz++; ch[sz][0]=ch[sz][1]=f[sz]=0; cnt[sz]=1; key[sz]=v; Size[sz]=1; root=sz; return; } int now=root,fa=0; while (true) { if (key[now]==v) { cnt[now]++; update(now); update(fa); splay(now); break; } fa=now; now=ch[now][key[now]<v]; if (now==0) { sz++; key[sz]=v; cnt[sz]=Size[sz]=1; ch[sz][0]=ch[sz][1]=0; f[sz]=fa; ch[fa][key[fa]<v]=sz; update(fa); splay(sz); break; } } } int Find(int v) { int ans=0,now=root; while(true) { if (v<key[now]) { now=ch[now][0]; } else { ans+=(ch[now][0]?Size[ch[now][0]]:0); if (key[now]==v) { splay(now); return ans+1; } ans+=cnt[now]; now=ch[now][1]; } if (now==0) return -1; } } int findx(int x) { if (x>sz) return -1; int now=root; while (true) { if (ch[now][0]&&Size[ch[now][0]]>=x) { now=ch[now][0]; } else { int temp=(ch[now][0]?Size[ch[now][0]]:0)+cnt[now]; if (x<=temp) return key[now]; x-=temp;now=ch[now][1]; } } } int pre() { int now =ch[root][0]; while(ch[now][1]) now=ch[now][1]; return now; } int next() { int now=ch[root][1]; while(ch[now][0]) now=ch[now][0]; return now; } void del(int x){ int whatever=Find(x); if (cnt[root]>1) {cnt[root]–;return;} //Only One Point if (!ch[root][0]&&!ch[root][1]) {Clear(root);root=0;return;} //Only One Child if (!ch[root][0]){ int oldroot=root;root=ch[root][1];f[root]=0;Clear(oldroot);return; } else if (!ch[root][1]){ int oldroot=root;root=ch[root][0];f[root]=0;Clear(oldroot);return; } //Two Children int leftbig=pre(),oldroot=root; splay(leftbig); f[ch[oldroot][1]]=root; ch[root][1]=ch[oldroot][1]; Clear(oldroot); update(root); return; } int main() { int n; root=0; sz=0; scanf(“%d”,&n); for (int i=0;i<n;i++) { int opt,x; scanf(“%d%d”,&opt,&x); switch (opt) { case 1: { Insert(x); break; } case 2:{ del(x); break; } case 3:{ printf(“%dn”,Find(x)); break; } case 4:{ printf(“%dn”,findx(x)); break; } case 5:{ Insert(x); printf(“%dn”,key[pre()]); del(x); break; } case 6:{ Insert(x); printf(“%dn”,key[next()]); del(x); break; } } } return 0; } |
Hello,World!
Hello,这是本博客的第一篇文章!
Read More »