各种自己用的模版(持续更

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<cstring>
  8 #define inf 2147483647
  9 #define For(i,a,b) for(register int i=a;i<=b;i++)
 10 #define p(a) putchar(a)
 11 #define g() getchar()
 12 
 13 using namespace std;
 14 int n,m,s,t;
 15 int x,y,v;
 16 int d[10010];
 17 struct node
 18 {
 19     int n;
 20     int v;
 21     node *next;
 22 }*e[500010];
 23 queue<int>q;
 24 bool vis[10010];
 25 
 26 void in(int &x)
 27 {
 28     int y=1;
 29     char c=g();x=0;
 30     while(c<'0'||c>'9')
 31     {
 32     if(c=='-')
 33     y=-1;
 34     c=g();
 35     }
 36     while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
 37     x*=y;
 38 }
 39 void o(int x)
 40 {
 41     if(x<0)
 42     {
 43         p('-');
 44         x=-x;
 45     }
 46     if(x>9)o(x/10);
 47     p(x%10+'0');
 48 }
 49 
 50 void push(int x,int y,int v)
 51 {
 52     node *p;
 53     p=new node();
 54     p->n=y;
 55     p->v=v;
 56     if(e[x]==NULL) 
 57     e[x]=p;
 58     else
 59     {
 60         p->next=e[x]->next;
 61         e[x]->next=p;
 62     }
 63 }
 64 
 65 void spfa(int x)
 66 {
 67     node *p;
 68     For(i,1,n)
 69     d[i]=inf;
 70     d[x]=0;
 71     q.push(x);
 72     while(q.size()>0)
 73     {
 74         t=q.front();
 75         vis[t]=true;
 76         for(node *i=e[t];i!=NULL;i=i->next)
 77         {
 78             if(d[t]+i->v<d[i->n])
 79             {
 80                 d[i->n]=d[t]+i->v;
 81                 if(!vis[i->n])
 82                 q.push(i->n);
 83             }
 84         }
 85         vis[t]=false;
 86         q.pop();
 87     }
 88 }
 89 
 90 int main()
 91 {
 92     in(n);in(m);in(s);
 93     For(i,1,m)
 94     {
 95         in(x);in(y);in(v);
 96         push(x,y,v);
 97     }
 98     spfa(s);
 99     For(i,1,n)
100     o(d[i]),p(' ');
101      return 0;
102 }
spfa求最短路
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define ls rt<<1
 13 #define rs rt<<1|1
 14 #define lson ls,nl,mid,l,r
 15 #define rson rs,mid+1,nr,l,r
 16 #define N 200010
 17 #define For(i,a,b) for(register long long i=a;i<=b;i++)
 18 #define p(a) putchar(a)
 19 #define g() getchar()
 20 
 21 using namespace std;
 22 long long n,m,s;
 23 long long x,y,v;
 24 bool vis[N];
 25 
 26 struct node{
 27     long long n;
 28     long long v;
 29     node *next;
 30 }*e[N];
 31 
 32 struct Node
 33 {
 34     long long i;
 35     long long d;
 36     bool operator < (const Node&temp)const{
 37         return d>temp.d;
 38     }
 39 }a[N];
 40 
 41 priority_queue<Node>q;
 42 
 43 void in(long long &x){
 44     long long y=1;
 45     char c=g();x=0;
 46     while(c<'0'||c>'9'){
 47         if(c=='-')y=-1;
 48         c=g();
 49     }
 50     while(c<='9'&&c>='0'){
 51         x=(x<<1)+(x<<3)+c-'0';c=g();
 52     }
 53     x*=y;
 54 }
 55 void o(long long x){
 56     if(x<0){
 57         p('-');
 58         x=-x;
 59     }
 60     if(x>9)o(x/10);
 61     p(x%10+'0');
 62 }
 63 
 64 void push(long long x,long long y,long long v){
 65     node *p;
 66     p=new node();
 67     p->n=y;
 68     p->v=v;
 69     if(e[x]==NULL)
 70         e[x]=p;
 71     else{
 72         p->next=e[x]->next;
 73         e[x]->next=p;
 74     }
 75 }
 76 
 77 void hd(){
 78     a[s].d=0;
 79     q.push(a[s]);
 80     while(!q.empty()){
 81         long long t=q.top().i;
 82         q.pop();
 83         if(vis[t])continue;
 84         vis[t]=true;
 85         for(register node *i=e[t];i;i=i->next){
 86             if(a[t].d+i->v<a[i->n].d){
 87                 a[i->n].d=a[t].d+i->v;
 88                 q.push(a[i->n]);
 89             }
 90         }
 91     }
 92 }
 93 
 94 int main(){
 95     in(n);in(m);in(s);
 96 
 97     For(i,1,m){
 98         in(x);in(y);in(v);
 99         push(x,y,v);
100     }
101 
102     For(i,1,n){
103         a[i].i=i;
104         a[i].d=inf;
105     }
106     hd();
107     For(i,1,n){
108         o(a[i].d);
109         p(' ');
110     }
111     return 0;
112 }
dijkstra最短路
  1 // luogu-judger-enable-o2
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<queue>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<ctime>
  8 #include<set>
  9 #include<map>
 10 #include<stack>
 11 #include<cstring>
 12 #define inf 2147483647
 13 #define For(i,a,b) for(register long long i=a;i<=b;i++)
 14 #define p(a) putchar(a)
 15 #define g() getchar()
 16 
 17 using namespace std;
 18 long long T;
 19 long long x,y,v;
 20 long long n,m;
 21 bool flag;
 22 bool vis[2010];
 23 long long d[2010];
 24 int num[2010];
 25 
 26 struct node
 27 {
 28     long long n;
 29     long long v;
 30     node *next;
 31 }*e[400010];
 32 queue<int>q;
 33 
 34 void in(long long &x)
 35 {
 36     long long y=1;
 37     char c=g();x=0;
 38     while(c<'0'||c>'9')
 39     {
 40     if(c=='-')
 41     y=-1;
 42     c=g();
 43     }
 44     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
 45     x*=y;
 46 }
 47 void o(long long x)
 48 {
 49     if(x<0)
 50     {
 51         p('-');
 52         x=-x;
 53     }
 54     if(x>9)o(x/10);
 55     p(x%10+'0');
 56 }
 57 
 58 inline void push(long long x,long long y,long long v)
 59 {
 60     node *p;
 61     p=new node();
 62     p->n=y;
 63     p->v=v;
 64     if(e[x]==NULL)
 65     e[x]=p;
 66     else
 67     {
 68         p->next=e[x]->next;
 69         e[x]->next=p;
 70     }
 71 }
 72 
 73 inline void spfa(long long x){
 74    q.push(x);
 75    vis[x]=true;
 76    num[1]++;
 77    while(!q.empty()){
 78     int t=q.front();q.pop();
 79     vis[t]=true;
 80     for(node *i=e[t];i;i=i->next){
 81 
 82         if(d[i->n]>d[t]+i->v){
 83             d[i->n]=d[t]+i->v;
 84             num[i->n]++;
 85                 if(num[i->n]>=n){
 86                     flag=true;
 87                     return;
 88                 }
 89             if(!vis[i->n]){
 90                 q.push(i->n);
 91                 vis[i->n]=true;
 92             }
 93         }
 94 
 95     }
 96     vis[t]=false;
 97    }
 98 }
 99 
100 int main()
101 {
102     in(T);
103     while(T--)
104     {
105         in(n),in(m);
106 
107         For(i,1,n)
108         e[i]=NULL,vis[i]=false,d[i]=inf,num[i]=0;
109         while(!q.empty())
110             q.pop();  
111         flag=false;
112 
113         For(i,1,m){
114             in(x),in(y),in(v);
115             push(x,y,v);
116             if(v>=0)
117             push(y,x,v);
118         }
119 
120         d[1]=0;
121         spfa(1);
122 
123         if(flag)
124         puts("YE5");
125         else
126         puts("N0");
127     }
128      return 0;
129 }
负环
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register int i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 //by war
16 //2017.11.9
17 using namespace std;
18 int f[100010][20];
19 int n,m;
20 int x,y;
21 void in(int &x)
22 {
23     int y=1;
24     char c=g();x=0;
25     while(c<'0'||c>'9')
26     {
27     if(c=='-')
28     y=-1;
29     c=g();
30     }
31     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
32     x*=y;
33 }
34 void o(int x)
35 {
36     if(x<0)
37     {
38         p('-');
39         x=-x;
40     }
41     if(x>9)o(x/10);
42     p(x%10+'0');
43 }
44 
45 void st()
46 {
47     int ind=log2(n);
48     For(j,1,ind)
49       For(i,1,n-(1<<j)+1)
50         f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
51 }
52 
53 int query(int l,int r)
54 {
55     int ind=log2(r-l+1);
56     return max(f[l][ind],f[r-(1<<ind)+1][ind]);
57 }
58 
59 int main()
60 {
61     in(n),in(m);
62     For(i,1,n)
63     {
64         in(x);
65         f[i][0]=x;
66     }
67     st();
68     For(i,1,m)
69     {
70         in(x),in(y);
71         o(query(x,y)),p('\n');
72     }
73      return 0;
74 }
ST表
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define ls rt<<1
 13 #define rs rt<<1|1
 14 #define lson ls,nl,mid,l,r
 15 #define rson rs,mid+1,nr,l,r
 16 #define N 100010
 17 #define For(i,a,b) for(int i=a;i<=b;i++)
 18 #define p(a) putchar(a)
 19 #define g() getchar()
 20 
 21 using namespace std;
 22 int n,m,x,y,cnt;
 23 int dfn[20010],low[20010];
 24 int del[20010];
 25 
 26 struct node{
 27     int n;
 28     node *next;
 29 }*e[200010];
 30 
 31 void in(int &x){
 32     int y=1;
 33     char c=g();x=0;
 34     while(c<'0'||c>'9'){
 35         if(c=='-')y=-1;
 36         c=g();
 37     }
 38     while(c<='9'&&c>='0'){
 39         x=(x<<1)+(x<<3)+c-'0';c=g();
 40     }
 41     x*=y;
 42 }
 43 void o(int x){
 44     if(x<0){
 45         p('-');
 46         x=-x;
 47     }
 48     if(x>9)o(x/10);
 49     p(x%10+'0');
 50 }
 51 
 52 void push(int x,int y){
 53     node *p;
 54     p=new node();
 55     p->n=y;
 56     if(e[x]==0)
 57         e[x]=p;
 58     else{
 59         p->next=e[x]->next;
 60         e[x]->next=p;
 61     }
 62 }
 63 
 64 void tarjan(int x,int fa){
 65     int child=0;
 66     dfn[x]=low[x]=++cnt;
 67     for(node *i=e[x];i;i=i->next){
 68         if(!dfn[i->n]){
 69             tarjan(i->n,fa);
 70             low[x]=min(low[x],low[i->n]);
 71             if(x==fa)
 72                 child++;
 73             if(low[i->n]>=dfn[x]&&x!=fa)
 74                 del[x]=1;
 75         }
 76         low[x]=min(low[x],dfn[i->n]);
 77     }
 78     if(child>1&&x==fa)
 79         del[x]=1;
 80 }
 81 
 82 int main(){
 83     in(n);in(m);
 84     For(i,1,m){
 85         in(x);in(y);
 86         push(x,y);
 87         push(y,x);
 88     }
 89     For(i,1,n)
 90         if(!dfn[i])
 91             tarjan(i,i);
 92     cnt=0;
 93     For(i,1,n)
 94         if(del[i])++cnt;
 95     o(cnt);p('\n');
 96 
 97     For(i,1,n)
 98         if(del[i])
 99             o(i),p(' ');
100     return 0;
101 }
割点(割顶)
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define For(i,a,b) for(register long long i=a;i<=b;i++)
 13 #define p(a) putchar(a)
 14 #define g() getchar()
 15 #define N 100010
 16 using namespace std;
 17 long long a[N],n,m,k,x,y,f;
 18 
 19 struct seg{
 20     long long mark;
 21     long long sum;
 22 }s[N<<3];
 23 
 24 inline void in(long long &x)
 25 {
 26     long long y=1;
 27     char c=g();x=0;
 28     while(c<'0'||c>'9')
 29     {
 30         if(c=='-')
 31             y=-1;
 32         c=g();
 33     }
 34     while(c<='9'&&c>='0')
 35     {
 36         x=(x<<1)+(x<<3)+c-'0';c=g();
 37     }
 38     x*=y;
 39 }
 40 inline void o(long long x)
 41 {
 42     if(x<0)
 43     {
 44         p('-');
 45         x=-x;
 46     }
 47     if(x>9)o(x/10);
 48     p(x%10+'0');
 49 }
 50 
 51 inline void build(long long rt,long long l,long long r){
 52     if(l==r){
 53         s[rt].sum=a[l];
 54         return;
 55     }
 56     long long mid=(l+r)>>1;
 57     build(rt<<1, l, mid);
 58     build(rt<<1|1, mid+1, r);
 59     s[rt].sum=s[rt<<1].sum+s[rt<<1|1].sum;
 60 }
 61 
 62 inline void pushdown(long long rt,long long l,long long r){
 63     if(s[rt].mark){
 64         long long mid=(l+r)>>1;
 65         s[rt<<1].mark+=s[rt].mark;
 66         s[rt<<1|1].mark+=s[rt].mark;
 67         s[rt<<1].sum+=(mid-l+1)*s[rt].mark;
 68         s[rt<<1|1].sum+=(r-mid)*s[rt].mark;
 69         s[rt].mark=0;
 70     }
 71 }
 72 
 73 inline void update(long long rt,long long nl,long long nr,long long l,long long r,long long k){
 74     if(nr<l||nl>r)return;
 75     if(nl>=l&&nr<=r){
 76         s[rt].mark+=k;
 77         s[rt].sum+=(nr-nl+1)*k;
 78         return;
 79     }
 80     pushdown(rt,nl,nr);
 81     long long mid=(nl+nr)>>1;
 82     update(rt<<1, nl, mid, l, r, k);
 83     update(rt<<1|1, mid+1, nr, l, r, k);
 84     s[rt].sum=s[rt<<1].sum+s[rt<<1|1].sum;
 85 }
 86 
 87 inline long long sum(long long rt,long long nl,long long nr,long long l,long long r){
 88     if(nl>r||nr<l)return 0;
 89     pushdown(rt, nl, nr);
 90     if(nl>=l&&nr<=r)return s[rt].sum;
 91     long long mid=(nl+nr)>>1;
 92     return sum(rt<<1, nl, mid, l, r)+sum(rt<<1|1, mid+1, nr, l, r);
 93 }
 94 
 95 int main()
 96 {
 97     in(n);in(m);
 98     For(i, 1, n)in(a[i]);
 99     build(1,1,n);
100     For(i,1,m){
101         in(f);in(x);in(y);
102         if(f==1){
103             in(k);
104             update(1,1,n,x,y,k);
105         }
106         else{
107             o(sum(1,1,n,x,y));p('\n');
108         }
109     }
110     return 0;
111 }
线段树(区间加,区间求和)
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register long long i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 //by war
16 //2017.11.9
17 using namespace std;
18 long long a[3000010];
19 long long p;
20 long long n;
21 void in(long long &x)
22 {
23     long long y=1;
24     char c=g();x=0;
25     while(c<'0'||c>'9')
26     {
27     if(c=='-')
28     y=-1;
29     c=g();
30     }
31     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
32     x*=y;
33 }
34 void o(long long x)
35 {
36     if(x<0)
37     {
38         p('-');
39         x=-x;
40     }
41     if(x>9)o(x/10);
42     p(x%10+'0');
43 }
44 int main()
45 {
46     in(n),in(p);
47     a[1]=1;
48     o(1),p('\n');
49     For(i,2,n)
50     {
51     a[i]=(p-p/i)*a[p%i]%p;    
52     o(a[i]),p('\n');
53     }
54      return 0;
55 }
乘法逆元(线性)
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define For(i,a,b) for(long long i=a;i<=b;i++)
 13 #define p(a) putchar(a)
 14 #define g() getchar()
 15 #define N 100010
 16 using namespace std;
 17 long long a[N],n,m,x,y,k,f,P;
 18 
 19 struct seg{
 20     long long add;
 21     long long mul=1;
 22     long long sum;
 23 }s[N<<3];
 24 
 25 void in(long long &x){
 26     long long y=1;
 27     char c=g();x=0;
 28     while(c<'0'||c>'9'){
 29         if(c=='-')y=-1;
 30         c=g();
 31     }
 32     while(c<='9'&&c>='0'){
 33         x=(x<<1)+(x<<3)+c-'0';c=g();
 34     }
 35     x*=y;
 36 }
 37 void o(long long x){
 38     if(x<0){
 39         p('-');
 40         x=-x;
 41     }
 42     if(x>9)o(x/10);
 43     p(x%10+'0');
 44 }
 45 
 46 void build(long long rt,long long l,long long r){
 47     if(l==r){
 48         s[rt].sum=a[l];
 49         return;
 50     }
 51     long long mid=(l+r)>>1;
 52     build(rt<<1, l, mid);
 53     build(rt<<1|1, mid+1, r);
 54     s[rt].sum=(s[rt<<1].sum+s[rt<<1|1].sum)%P;
 55 }
 56 
 57 void pushdown(long long rt,long long l,long long r){
 58     if(s[rt].add||s[rt].mul^1){
 59         long long mid=(l+r)>>1;
 60         s[rt<<1].mul=(s[rt<<1].mul*s[rt].mul)%P;
 61         s[rt<<1|1].mul=(s[rt<<1|1].mul*s[rt].mul)%P;
 62         s[rt<<1].add=(s[rt<<1].add*s[rt].mul+s[rt].add)%P;
 63         s[rt<<1|1].add=(s[rt<<1|1].add*s[rt].mul+s[rt].add)%P;
 64         s[rt<<1].sum=(s[rt<<1].sum*s[rt].mul+(mid-l+1)*s[rt].add)%P;
 65         s[rt<<1|1].sum=(s[rt<<1|1].sum*s[rt].mul+(r-mid)*s[rt].add)%P;
 66         s[rt].add=0;
 67         s[rt].mul=1;
 68     }
 69 }
 70 
 71 void update_mul(long long rt,long long nl,long long nr,long long l,long long r,long long k){
 72     if(nl>r||nr<l)return;
 73     pushdown(rt,nl,nr);
 74     if(nl>=l&&nr<=r){
 75         s[rt].mul= (s[rt].mul*k)%P;
 76         s[rt].add=(s[rt].add*k)%P;
 77         s[rt].sum=(s[rt].sum*k)%P;
 78         return;
 79     }
 80     long long mid=(nl+nr)>>1;
 81     update_mul(rt<<1, nl, mid, l, r, k);
 82     update_mul(rt<<1|1, mid+1, nr, l, r, k);
 83     s[rt].sum=(s[rt<<1].sum+s[rt<<1|1].sum)%P;
 84 }
 85 
 86 void update_sum(long long rt,long long nl,long long nr,long long l,long long r,long long k){
 87     if(nl>r||nr<l)return;
 88     pushdown(rt,nl,nr);
 89     if(nl>=l&&nr<=r){
 90         s[rt].add= (s[rt].add+k)%P;
 91         s[rt].sum=(s[rt].sum+(nr-nl+1)*k)%P;
 92         return;
 93     }
 94     long long mid=(nl+nr)>>1;
 95     update_sum(rt<<1, nl, mid, l, r, k);
 96     update_sum(rt<<1|1, mid+1, nr, l, r, k);
 97     s[rt].sum=(s[rt<<1].sum+s[rt<<1|1].sum)%P;
 98 }
 99 
100 long long sum(long long rt,long long nl,long long nr,long long l,long long r) {
101     if(nl>r||nr<l)return 0;
102     pushdown(rt, nl, nr);
103     if(nl>=l&&nr<=r) return s[rt].sum%P;
104     long long mid=(nl+nr)>>1;
105     return (sum(rt<<1, nl, mid, l, r)+sum(rt<<1|1, mid+1, nr, l, r))%P;
106 }
107 
108 int main(){
109     in(n);in(m);in(P);
110     For(i, 1, n)in(a[i]);
111     build(1, 1, n);
112     For(i, 1, m){
113         in(f);in(x);in(y);
114         if(f==1){
115             in(k);
116             update_mul(1,1,n,x,y,k);
117         }
118         if(f==2){
119             in(k);
120             update_sum(1,1,n,x,y,k);
121         }
122         if(f==3){
123             o(sum(1,1,n,x,y));
124             p('\n');
125         }
126     }
127     return 0;
128 }
线段树(区间加,区间乘,区间求和)
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register long long i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 #define MOD1 1000000007
16 #define MOD2 1000000009
17 #define base 31
18 //by war
19 //2017.11.6
20 using namespace std;
21 struct ha
22 {
23     long long o;
24     long long t;
25     bool operator<(const ha&a)const
26     {
27         return o<a.o;
28     }
29 }h[10010];
30 char a[1510];
31 long long n;
32 long long ans;
33 void in(long long &x)
34 {
35     long long y=1;
36     char c=g();x=0;
37     while(c<'0'||c>'9')
38     {
39     if(c=='-')
40     y=-1;
41     c=g();
42     }
43     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
44     x*=y;
45 }
46 void o(long long x)
47 {
48     if(x<0)
49     {
50         p('-');
51         x=-x;
52     }
53     if(x>9)o(x/10);
54     p(x%10+'0');
55 }
56 
57 long long hash(long long mod)
58 {
59     long long ans=0;
60     long long len=strlen(a+1);
61     For(i,1,len)
62     ans=((ans*base)+a[i])%mod;
63     return ans;
64 }
65 
66 int main()
67 {
68     in(n);
69     For(i,1,n)
70     {
71         cin>>(a+1);
72         h[i].o=hash(MOD1);
73         h[i].t=hash(MOD2);
74     }
75     sort(h+1,h+n+1);
76     For(i,2,n)
77     if(h[i].o!=h[i-1].o||h[i].t!=h[i-1].t)
78     ans++;
79     o(ans+1);
80      return 0;
81 }
字符串hash
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int n,m,cnt;
 5 bool vis[10000010];
 6 int prime[1000010];
 7 int x;
 8 void E( )
 9 {
10     vis[1]=true;
11     for(int i=2;i<=n;i++)
12     {
13         if(!vis[i])prime[++cnt]=i;
14         for(int j=1;j<=cnt&&prime[j]*i<=n;j++)
15         {
16             vis[prime[j]*i]=true;
17             if(i%prime[j]==0)break;
18         }
19     }
20 }
21 int main( )
22 {
23     cin>>n>>m;
24     E( );
25     for(int i=1;i<=m;i++)
26     {
27         cin>>x;
28         if(!vis[x])
29             printf("Yes\n");
30         else
31             printf("No\n");
32     }
33     return 0;
34 }
线性筛素数
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register int i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 //by war
16 //2017.11.9
17 using namespace std;
18 int t[500010];
19 int n,m;
20 int flag,x,y,k;
21 int pre;
22 void in(int &x)
23 {
24     int y=1;
25     char c=g();x=0;
26     while(c<'0'||c>'9')
27     {
28     if(c=='-')
29     y=-1;
30     c=g();
31     }
32     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
33     x*=y;
34 }
35 void o(int x)
36 {
37     if(x<0)
38     {
39         p('-');
40         x=-x;
41     }
42     if(x>9)o(x/10);
43     p(x%10+'0');
44 }
45 
46 void modify(int k,int change)
47 {
48     for(;k<=n;k+=(-k)&k)
49     t[k]+=change;
50 }
51 
52 int get(int k)
53 {
54     int cnt=0;
55     for(;k>0;k-=(-k)&k)
56     cnt+=t[k];
57     return cnt;
58 }
59 
60 int main()
61 {
62     in(n),in(m);
63     For(i,1,n)
64     {
65         in(x);
66         modify(i,x-pre);
67         pre=x;
68     }
69     For(i,1,m)
70     {
71         in(flag);
72         if(flag==1)
73         {
74             in(x),in(y),in(k);
75             modify(x,k);
76             modify(y+1,-k);
77         }
78         else
79         {
80             in(x);
81             o(get(x)),p('\n');
82         }
83     }
84      return 0;
85 }
树状数组(区间加,单点求和)
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(int i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 
16 using namespace std;
17 int t[10000100],n,m,cz,x,y,k,f,ans;
18 void in(int &x)
19 {
20     int y=1;
21     char c=g();x=0;
22     while(c<'0'||c>'9')
23     {
24         if(c=='-')
25             y=-1;
26         c=g();
27     }
28     while(c<='9'&&c>='0')
29     {
30         x=(x<<1)+(x<<3)+c-'0';c=g();
31     }
32     x*=y;
33 }
34 void o(int x)
35 {
36     if(x<0)
37     {
38         p('-');
39         x=-x;
40     }
41     if(x>9)o(x/10);
42     p(x%10+'0');
43 }
44 int main()
45 {
46     in(n);in(cz);
47     for(m=1;m<=n;m<<=1);
48     For(i, m+1, m+n)
49     in(t[i]);
50     for(int i=m-1;i;i--)
51         t[i]=t[i<<1]+t[i<<1|1];
52     For(jj,1,cz)
53     {
54         in(f);
55         if(f==1)
56         {
57             in(x);in(k);
58             for(t[x+=m]+=k,x>>=1;x;x>>=1)
59                 t[x]=t[x<<1]+t[x<<1|1];
60         }
61         else
62         {
63             in(x);in(y);
64             ans=0;
65             for(int l=x+m-1,r=y+m+1;l^r^1;l>>=1,r>>=1)
66             {
67                 if(~l&1)ans+=t[l^1];
68                 if(r&1)ans+=t[r^1];
69             }
70             o(ans);p('\n');
71         }
72     }
73     return 0;
74 }
树状数组(单点加,区间求和)
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2019.8.15
using namespace std;
long long a[200010];
long long n,m,p;
long long T;
void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

long long ksm(long long a,long long b){
    long long r=1;
    while(b>0){
        if(b%2==1)
            r=r*a%p;
        a=a*a%p;
        b>>=1;
    }
    return r;
}

long long c(long long n,long long m){
    if(n<m) return 0;
    return a[n]*ksm(a[n-m]*a[m]%p,p-2)%p;
}

long long lucas(long long n,long long m){
    if(!m) return 1;
    return c(n%p,m%p)*lucas(n/p,m/p)%p;
}

int main(){
    in(T);
    a[0]=1;
    while(T--){
        in(n);in(m);in(p);
        For(i,1,n+m)
            a[i]=a[i-1]*i%p;
        o(lucas(n+m,m)%p);p('\n');
    }
     return 0;
}
卢卡斯定理
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <cstring>
 7 #define inf 2147483647
 8 #define N 1000010
 9 #define p(a) putchar(a)
10 #define For(i,a,b) for(long long i=a;i<=b;++i)
11 
12 using namespace std;
13 long long l,r;
14 long long prime[N],f[N],g[N],cnt,ans;
15 bool vis[N];
16 void in(long long &x){
17     long long y=1;char c=getchar();x=0;
18     while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
19     while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
20     x*=y;
21 }
22 void o(long long x){
23     if(x<0){p('-');x=-x;}
24     if(x>9)o(x/10);
25     p(x%10+'0');
26 }
27 
28 void Euler(){
29     For(i,2,r){
30         if(!vis[i]){
31             prime[++cnt]=i;
32             f[i]=g[i]=i+1;
33             vis[i]=1;
34         }
35         for(long long j=1;j<=cnt&&i*prime[j]<=r;j++){
36             vis[i*prime[j]]=1;
37             if(i%prime[j]==0){
38                 g[i*prime[j]]=g[i]*prime[j]+1;
39                 f[i*prime[j]]=f[i]/g[i]*g[i*prime[j]];
40                 break;
41             }
42             else{
43                 g[i*prime[j]]=prime[j]+1;
44                 f[i*prime[j]]=f[i]*f[prime[j]];
45             }
46         }
47     }
48 }
49 int main(){
50     in(l);in(r);
51     Euler();
52     For(i,l,r)
53         ans+=f[i];
54     o(ans);
55     return 0;
56 }
筛法求约数和
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(long long i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 #define mod 1000000007
16 //by war
17 //2017.11.6
18 using namespace std;
19 long long n,k;
20 struct matrix{
21     long long a[110][110];
22     matrix operator*(const matrix&b)const{
23         matrix r;
24         For(i,1,n)
25             For(j,1,n){
26                 r.a[i][j]=0;    
27                 For(k,1,n)
28                 r.a[i][j]=(r.a[i][j]+a[i][k]*b.a[k][j])%mod;
29             }
30         return r;
31     }
32 }a;
33 void in(long long &x)
34 {
35     long long y=1;
36     char c=g();x=0;
37     while(c<'0'||c>'9')
38     {
39     if(c=='-')
40     y=-1;
41     c=g();
42     }
43     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
44     x*=y;
45 }
46 void o(long long x)
47 {
48     if(x<0)
49     {
50         p('-');
51         x=-x;
52     }
53     if(x>9)o(x/10);
54     p(x%10+'0');
55 }
56 
57 matrix ksm(matrix a,long long b){
58     matrix r=a;b--;
59     while(b>0){
60         if(b%2==1) r=r*a;
61         a=a*a;
62         b>>=1;
63     }
64     return r;
65 }
66 
67 int main(){
68     in(n),in(k);
69     For(i,1,n)
70       For(j,1,n)
71         in(a.a[i][j]);
72     matrix r=ksm(a,k);
73     For(i,1,n){
74         For(j,1,n)
75             o(r.a[i][j]),p(' ');
76         p('\n');
77     }
78      return 0;
79 }
矩阵快速幂

 

posted @ 2019-04-16 20:42  WeiAR  阅读(...)  评论(...编辑  收藏