常用算法

#常用算法竞赛头文件及宏定义:

1 #include <iostream>
2 #include <algorithm>
3 #include <cmath>
4 #include <cstring>
5 #define IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);//将cin和cout的速率提升 6 using namespace std; 7 typedef long long ll;
#include <bits/stdc++.h>//包含所有常用

求取一个数的因子数目的算法:

 1 ll get(ll x) {
 2     ll sum = 0, sq = sqrt(x);    //开方,降低复杂度
 3     for (ll i = 1; i <= sq; i++) {
 4         if (x % i == 0) {
 5             sum += 2;
 6         }
 7     }
 8     if (sq * sq == x) {
 9         sum--;
10     }
11     return sum;
12 }

 

求最大公约数:

1  int gcd(int x,int y){
2              if(y==0){
3                  return x;
4              } else {
5                  return gcd(y,x%y);
6              }
7          }
8  

 

从大到小排序:

sort(A, A + n,greater<int>());

 

二维数组初始化;

  

vector<vector<int>> dp(N, vector<int>(N, -1));

 

 

posted @ 2020-02-15 16:41  DengSchoo  阅读(109)  评论(0编辑  收藏  举报