h1j1k1

C++キョウプロコピペ

Feb 14th, 2021 (edited)
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5. #define rep(i, n) for (int i = 0; i < (int)(n); i++)
  6. #define all(v) v.begin(), v.end()
  7. #define ll int64_t
  8. #define _GLIBCXX_DEBUG
  9. const int dx[4] = {1, 0, -1, 0};
  10. const int dy[4] = {0, 1, 0, -1};
  11. //拡張ユークリッドの互除法
  12. template< typename T >
  13. T extgcd(T a, T b, T &x, T &y) {
  14.   T d = a;
  15.   if(b != 0) {
  16.     d = extgcd(b, a % b, y, x);
  17.     y -= (a / b) * x;
  18.   } else {
  19.     x = 1;
  20.     y = 0;
  21.   }
  22.   return d;
  23. }
  24. //べき乗
  25. ll pow(ll x,ll n,ll mod){
  26.   ll res=1;
  27.   while(n>0){
  28.     if(n&1) res=res*x%mod;
  29.     x=x*x%mod;
  30.     n>>=1;
  31.   }
  32.   return res;
  33. }
  34. //約数列挙(√N)
  35. vector< int64_t > divisor(int64_t n) {
  36.   vector< int64_t > ret;
  37.   for(int64_t i = 1; i * i <= n; i++) {
  38.     if(n % i == 0) {
  39.       ret.push_back(i);
  40.       if(i * i != n) ret.push_back(n / i);
  41.     }
  42.   }
  43.   sort(begin(ret), end(ret));
  44.   return (ret);
  45. }
  46. //素因数分解(√N)
  47. map< int64_t, int > prime_factor(int64_t n) {
  48.   map< int64_t, int > ret;
  49.   for(int64_t i = 2; i * i <= n; i++) {
  50.     while(n % i == 0) {
  51.       ret[i]++;
  52.       n /= i;
  53.     }
  54.   }
  55.   if(n != 1) ret[n] = 1;
  56.   return ret;
  57. }
  58. //素数テーブル(NloglogN)
  59. vector< bool > prime_table(int n) {
  60.   vector< bool > prime(n + 1, true);
  61.   if(n >= 0) prime[0] = false;
  62.   if(n >= 1) prime[1] = false;
  63.   for(int i = 2; i * i <= n; i++) {
  64.     if(!prime[i]) continue;
  65.     for(int j = i + i; j <= n; j += i) {
  66.       prime[j] = false;
  67.     }
  68.   }
  69.   return prime;
  70. }
  71. //二項係数(K)
  72. template< typename T >
  73. T binomial(int64_t N, int64_t K) {
  74.   if(K < 0 || N < K) return 0;
  75.   T ret = 1;
  76.   for(T i = 1; i <= K; ++i) {
  77.     ret *= N--;
  78.     ret /= i;
  79.   }
  80.   return ret;
  81. }
  82. //二項係数テーブル(N^2)
  83. template< typename T >
  84. vector< vector< T > > binomial_table(int N) {
  85.   vector< vector< T > > mat(N + 1, vector< T >(N + 1));
  86.   for(int i = 0; i <= N; i++) {
  87.     for(int j = 0; j <= i; j++) {
  88.       if(j == 0 || j == i) mat[i][j] = 1;
  89.       else mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
  90.     }
  91.   }
  92.   return mat;
  93. }
  94. //Gragh template
  95. template< typename T >
  96. struct edge {
  97.   int src, to;
  98.   T cost;
  99.  
  100.   edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  101.  
  102.   edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  103.  
  104.   edge &operator=(const int &x) {
  105.     to = x;
  106.     return *this;
  107.   }
  108.  
  109.   operator int() const { return to; }
  110. };
  111.  
  112. template< typename T >
  113. using Edges = vector< edge< T > >;
  114. template< typename T >
  115. using WeightedGraph = vector< Edges< T > >;
  116. using UnWeightedGraph = vector< vector< int > >;
  117. template< typename T >
  118. using Matrix = vector< vector< T > >;
  119. //unionfind
  120. struct UnionFind {
  121.   vector< int > data;
  122.  
  123.   UnionFind(int sz) {
  124.     data.assign(sz, -1);
  125.   }
  126.  
  127.   bool unite(int x, int y) {
  128.     x = find(x), y = find(y);
  129.     if(x == y) return (false);
  130.     if(data[x] > data[y]) swap(x, y);
  131.     data[x] += data[y];
  132.     data[y] = x;
  133.     return (true);
  134.   }
  135.  
  136.   int find(int k) {
  137.     if(data[k] < 0) return (k);
  138.     return (data[k] = find(data[k]));
  139.   }
  140.  
  141.   int size(int k) {
  142.     return (-data[find(k)]);
  143.   }
  144. };
  145. int AAA=0;
  146. //DIJKSTRA(ElogV)最短路
  147. template< typename T >
  148. vector< T > dijkstra(WeightedGraph< T > &g, int s) {
  149.   const auto INF = numeric_limits< T >::max();
  150.   vector< T > dist(g.size(), INF);
  151.  
  152.   using Pi = pair< T, int >;
  153.   priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  154.   dist[s] = 0;
  155.   que.emplace(dist[s], s);
  156.   while(!que.empty()) {
  157.     T cost;
  158.     int idx;
  159.     tie(cost, idx) = que.top();
  160.     que.pop();
  161.     if(dist[idx] < cost) continue;
  162.     for(auto &e : g[idx]) {
  163.       auto next_cost = cost + e.cost;
  164.       if(dist[e.to] <= next_cost) continue;
  165.       dist[e.to] = next_cost;
  166.       AAA++;
  167.       que.emplace(dist[e.to], e.to);
  168.     }
  169.   }
  170.   return dist;
  171. }
  172. //prim最小全域木
  173. template< typename T >
  174. T prim(WeightedGraph< T > &g) {
  175.   using Pi = pair< T, int >;
  176.  
  177.   T total = 0;
  178.   vector< bool > used(g.size(), false);
  179.   priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  180.   que.emplace(0, 0);
  181.   while(!que.empty()) {
  182.     auto p = que.top();
  183.     que.pop();
  184.     if(used[p.second]) continue;
  185.     used[p.second] = true;
  186.     total += p.first;
  187.     for(auto &e : g[p.second]) {
  188.       que.emplace(e.cost, e.to);
  189.     }
  190.   }
  191.   return total;
  192. }
  193. //bellman_ford(VE)単一始点最短路
  194. template< typename T >
  195. vector< T > bellman_ford(Edges< T > &edges, int V, int s) {
  196.   const auto INF = numeric_limits< T >::max();
  197.   vector< T > dist(V, INF);
  198.   dist[s] = 0;
  199.   for(int i = 0; i < V - 1; i++) {
  200.     for(auto &e : edges) {
  201.       if(dist[e.src] == INF) continue;
  202.       dist[e.to] = min(dist[e.to], dist[e.src] + e.cost);
  203.     }
  204.   }
  205.   for(auto &e : edges) {
  206.     if(dist[e.src] == INF) continue;
  207.     if(dist[e.src] + e.cost < dist[e.to]) return vector< T >();
  208.   }
  209.   return dist;
  210. }
  211. //memo cout << fixed << setprecision(桁数);//
  212. int read(){
  213.     int x=0; char c;
  214.     while(((c=getchar())>'9' || c<'0')&&c!='-');
  215.     const int f=(c=='-')&&(c=getchar());
  216.     while(x=x*10-48+c,(c=getchar())>='0'&&c<='9');
  217.     return f?-x:x;
  218. }
  219. int main(void){
  220.     printf("Hello World!");
  221. }
Add Comment
Please, Sign In to add comment