zoukankan      html  css  js  c++  java
  • 算法回顾板子

    欧拉函数

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 int phi(int x){
     6     int ans = x;
     7     for(int i = 2; i*i <= x; i++){
     8         if(x%i == 0){
     9             ans = ans - ans/i;
    10             while(x%i == 0)
    11                 x /= i;
    12         }
    13     }
    14     if(x > 1)
    15         ans = ans - ans/x;
    16     return ans;
    17 }
    18 
    19 int n;
    20 int main(){
    21     cin >> n;
    22     cout << phi(n) << endl;
    23     return 0;
    24 }

    矩阵快速幂

     1 #include <iostream>
     2 #define mod 1000000000
     3 #define N 100
     4 #define ll long long int
     5 using namespace std;
     6 struct Node{
     7     int vis[N][N];
     8 }node;
     9 
    10 Node mul(Node a, Node b){
    11     Node c;
    12     for(int i = 0; i < 2; i++){
    13         for(int k = 0; k < 2; k++){
    14             for(int j = 0; j < 2; j++){
    15                 c.vis[i][j] = (c.vis[i][j] + a.vis[i][k]*b.vis[k][j])%mod;
    16             }
    17         }
    18     }
    19     return c;    
    20 }
    21 
    22 Node quick_pow(Node a, ll b){
    23     Node c;
    24     for(int i = 0; i < 2; i++)
    25         c.vis[i][i] = 1;
    26     while(b){
    27         if(b&1)
    28             c = mul(a, c);
    29         a = mul(a, a);
    30         b >>= 1;
    31     }
    32     return c;
    33 }
    34 
    35 
    36 
    37 int main(){
    38     Node ans;
    39     ans.vis[0][0] = 1; ans.vis[0][1] = 1;
    40     ans.vis[1][0] = 1; ans.vis[1][1] = 0;
    41     Node cnt = quick_pow(ans, 20);
    42     for(int i = 0; i < 2; i++){
    43         for(int j = 0 ; j < 2; j++){
    44             cout << cnt.vis[i][j] << " ";
    45         }
    46         cout << endl;
    47     }
    48     cout << endl;
    49     return 0;
    50 }

    快速幂

     1 #include <iostream>
     2 #define mod 1000000000
     3 #define N 100000
     4 #define ll long long int
     5 using namespace std;
     6 
     7 ll quick_pow(ll a, ll b){
     8     ll ans = 1;
     9     while(b){
    10         if(b&1)
    11             ans = (ans*a)%mod;
    12         a = (a*a)%mod;
    13         b >>= 1;
    14     }
    15     return ans;
    16 }
    17 
    18 int main(){
    19     ll n, m;
    20     cin >> n >> m;
    21     cout << quick_pow(n,m)<<endl;
    22     return 0;
    23 }

     kmp

     1 #include <bits/stdc++.h>
     2 #define N 100000
     3 using namespace std;
     4 int Next[N];
     5 
     6 void getNext(string s){
     7     int j = -1;
     8     int i = 0;
     9     int len = s.length();
    10     Next[0] =-1;
    11     while(i < len){
    12         if(j == -1 || s[i] == s[j]){
    13             i++;
    14             j++;
    15             Next[i] = j;
    16         }else
    17             j = Next[j];
    18     }
    19 }
    20 
    21 int kmp(string s, string ss){
    22     getNext(ss);
    23     int i = 0, j = 0;
    24     int slen = s.length();
    25     int sslen = ss.length();
    26     while( i < slen && j < sslen){
    27         if(j == -1 || s[i] == ss[j]){
    28             i ++; j++;
    29         }else
    30             j = Next[j];
    31     }
    32     if(j == sslen){
    33         return i - j + 1;
    34     }
    35     return -1;
    36 }
    37 
    38 string s, ss;
    39 int main(){
    40     cin >> s >> ss;
    41     int an = kmp(s, ss);
    42     if(an == -1){
    43         cout << "NO" << endl;
    44     }else{
    45         cout << "YES" <<" " << an <<  endl;
    46     }
    47     return 0;
    48 }

    树状数组

     1 #include <bits/stdc++.h>
     2 #define N 50005
     3 #define mem(a) memset(a,0,sizeof(a))
     4 using namespace std;
     5 struct Node{
     6     int a,b;
     7 }node[N];
     8 int tree[N];
     9 bool cmp(Node x,Node y){
    10     return x.a>y.a;
    11 }
    12 int lowbit(int x){ return x&(-x);}
    13 
    14 int update(int x){
    15     while(x<N){
    16         tree[x]++;
    17         x+=lowbit(x);
    18     }
    19 }
    20 int sum(int x){
    21     int sum=0;
    22     while(x){
    23         sum+=tree[x];
    24         x-=lowbit(x);
    25     }
    26     return sum;
    27 }
    28 int main() {
    29     int n;
    30     cin>>n;
    31     mem(tree);
    32     mem(node);
    33     for(int i=1;i<=n;i++){
    34         cin>>node[i].a;
    35         node[i].b=i;
    36     }
    37     sort(node+1,node+1+n,cmp);
    38     int cnt=0;
    39     for(int i=1;i<=n;i++){
    40         cnt+=sum(node[i].b);
    41         update(node[i].b);
    42     }
    43     cout<<cnt<<endl;
    44     return 0;
    45 }

    字典树

     1 #include <bits/stdc++.h>
     2 #define N 100000
     3 using namespace std;
     4 int tree[N][26];
     5 int val[N];
     6 string s;
     7 int pos = 1;
     8 
     9 void insert(string s){
    10     int root = 0;
    11     for(int i = 0; s[i]; i++){
    12         int an = s[i] - 'a';
    13         if(!tree[root][an]){
    14             tree[root][an] = pos++;
    15         }
    16         root = tree[root][an];
    17     }
    18     val[root] = 1;
    19 }
    20 
    21 bool find(string s){
    22     int root = 0;
    23     for(int i = 0; s[i]; i++){
    24         int an = s[i] - 'a';
    25         if(!tree[root][an])
    26             return 0;
    27         root = tree[root][an];
    28     }
    29     return 1;
    30 }
    31 
    32 int main(){
    33     cin >> s;
    34     insert(s);
    35     int n;
    36     cin >> n;
    37     for(int i = 0; i < n; i++){
    38         cin >> s;
    39         if(find(s))
    40             cout<<"Yes"<<endl;
    41         else
    42             cout<<"No"<<endl;
    43 
    44     }
    45     return 0;
    46 }

    最小生成树

     1 #include <iostream>
     2 #include <algorithm>
     3 #define N 50005
     4 using namespace std;
     5 
     6 int n, m;
     7 struct Node{
     8     int x, y, val;
     9     friend bool operator <(const Node &a, const Node &b){
    10         return a.val < b.val;
    11     }
    12 }node[N];
    13 
    14 int fa[N];
    15 int sum = 0;
    16 int find(int x){
    17     return fa[x] == x? x: fa[x] = find(fa[x]);
    18 }
    19 int cnt = 0;
    20 void kruskal(){
    21     for(int i = 0; i <= n; i++)
    22         fa[i] = i;
    23     for(int i = 0; i < m; i++){
    24         int ax = find(node[i].x);
    25         int ay = find(node[i].y);
    26         if(ax != ay){
    27             fa[ax] = ay;
    28             sum += node[i].val;
    29             cnt++;
    30         }
    31         if(cnt == n-1)
    32             break;
    33     }
    34 }
    35 
    36 
    37 int main(){    
    38     cin >> n >> m;
    39     for(int i = 0; i < m; i++){
    40         cin >> node[i].x >> node[i].y >> node[i].val;
    41     }
    42     sort(node, node+m);
    43     kruskal();
    44     cout << sum << endl;
    45     return 0;
    46 }

    加一个prim板子

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <vector>
     4 #include <queue>
     5 #define N 1005
     6 using namespace std;
     7 const int inf = 0x3f3f3f3f;
     8 int n, m;
     9 int an = inf;
    10 struct Node{
    11     int to, val;
    12     friend bool operator <(const Node &a, const Node &b){
    13         return a.val > b.val;
    14     }
    15 };
    16 int vis[N];
    17 vector<Node> v[N];
    18 priority_queue<Node> q;
    19 void prim(){
    20     int cnt = 2;
    21     while(!q.empty()){
    22         Node ans = q.top();
    23         q.pop();
    24         if(vis[ans.to])
    25             continue;
    26         cnt ++;
    27         an += ans.val;
    28         vis[ans.to] = 1;
    29         if(cnt == n)
    30             break;
    31         for(int i = 0; i < v[ans.to].size(); i++){
    32             Node p = v[ans.to][i];
    33             if(vis[p.to])
    34                 continue;
    35             q.push({p.to, p.val});
    36         }
    37     }
    38 }
    39 
    40 int main(){
    41     cin >> n >> m;
    42     int xx,yy;
    43     int x, y, z;
    44     for(int i = 0; i < m; i++){
    45         cin >> x >> y >> z;
    46         v[x].push_back({y,z});
    47         v[y].push_back({x,z});
    48         if(z < an) xx = x, yy = y, an = z;
    49     }
    50     vis[xx] = vis[yy] = 1;
    51     for(int i = 0; i < v[xx].size(); i++)
    52         q.push({v[xx][i].to, v[xx][i].val});
    53     for(int i = 0; i < v[yy].size(); i++)
    54         q.push({v[yy][i].to, v[yy][i].val});
    55     prim();
    56     cout << an <<endl;
    57     return 0;
    58 }

    最短路

     1 #include <bits/stdc++.h>
     2 #define N 1000
     3 #define inf 0x3f3f3f3f
     4 using namespace std;
     5 int n, m;
     6 struct Node{
     7     int to, value;
     8     friend bool operator<(const Node &a , const Node &b)
     9     {
    10         return a.value > b.value;   // ascending sort
    11     }
    12 };
    13 vector<Node> v[N];
    14 int vis[N];
    15 int val[N];
    16 int ans = 0;
    17 
    18 void short_path(int x){
    19     priority_queue<Node> q;
    20     q.push({x,0});
    21     while(!q.empty()){
    22         Node node = q.top();
    23          q.pop();
    24          if(vis[node.to] == 1)
    25             continue;
    26         vis[node.to] = 1;
    27         val[node.to] = min(val[node.to], node.value);
    28         ans ++;
    29         if(ans == n) break;
    30 
    31         for(int i = 0; i < v[node.to].size(); i ++){
    32             Node an = v[node.to][i];
    33             if(vis[an.to] == 0){
    34                 q.push({an.to, val[node.to]+an.value});
    35             }
    36         }
    37     }
    38 }
    39 
    40 int main(){
    41     int x, y, z;
    42     while(cin >> n >> m && n != 0 && m != 0){
    43         memset(vis, 0, sizeof(vis));
    44         memset(val, inf, sizeof(val));
    45         ans = 0;
    46         for(int i = 0; i < 1000; i++)
    47             v[i].clear();
    48         for(int i = 0; i < m; i ++){
    49             cin >> x >> y >> z;
    50             v[x].push_back({y, z});
    51             v[y].push_back({x, z});
    52         }
    53         short_path(1);
    54         cout<< val[n] << endl;
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    UVa 10088 (Pick定理) Trees on My Island
    LA 3295 (计数 容斥原理) Counting Triangles
    LA 5846 (计数) Neon Sign
    java是什么?软帝学院告诉你学Java能做什么?Java有什么特性?
    【软帝学院】一套好的java基础教学视频需要哪些有哪些内容
    推荐五个java基础学习网站,小白必备
    学习java设计模式有用吗?懂这六个原则,编程更轻松
    Java是什么?只需5分钟,了解java必须要知道的知识点
    软帝学院:自学java到底难不难?做好这几步,少走3年弯路
    软帝学院:java开发程序很难吗?学会这十步,5分钟搞定一个程序
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/10544513.html
Copyright © 2011-2022 走看看