![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数学知识
QingQingDE23
这个作者很懒,什么都没留下…
展开
-
AcWing 894 拆分-Nim游戏 题解 (博弈论)
#include<iostream>#include<cstring>#include<unordered_set>using namespace std;const int N = 110;int f[N];//记录每个数量的石子的sg值int eg(int x){ if(f[x] != -1) return f[x];//如果x的eg值已经被计算过,直接返回 unordered_set<int> S; for(int i原创 2021-09-15 12:12:05 · 95 阅读 · 0 评论 -
AcWing 892 台阶-Nim游戏 题解 (博弈论)
思路:只考虑奇数台阶上的石子,就是一个普通Nim游戏,最后面对的必胜状态是所有台阶中就剩第一级台阶还有一个石子,拿走,对手必输,此时左右奇数台阶上的石子数量异或和是 !=0,所以面对的奇数级台阶上的石子数量异或和为0就是必败状态,不考虑偶数级台阶是因为对手可以从偶数级台阶往奇数级台阶拿石子维持必胜状态,所以如果开局时,奇数级台阶上的石子数量异或和为奇数,则先手必胜,否则必败#include<iostream>using namespace std;int main(){ in原创 2021-09-15 11:05:19 · 97 阅读 · 0 评论 -
AcWing 884. 高斯消元解异或线性方程组 题解 (高斯消元)
#include<iostream>#include<algorithm>using namespace std;const int N = 110;int n, a[N][N];int gauss(){ int r, c; for(c = 0, r = 0; c < n; c ++ ){ int t = r; for(int i = t; i < n; i ++ ){ if(a[i][c]){ t = i; break原创 2021-09-15 09:52:01 · 75 阅读 · 0 评论 -
AcWing 890 能被整除的数 题解 (容斥原理)
#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const int N = 20;int p[N];int main(){ int n, m; cin>>n>>m; for(int i = 0; i < m; i ++ ){ cin>>p[i];//读入质数 }.原创 2021-09-13 17:06:41 · 76 阅读 · 0 评论 -
AcWing 889 满足条件的01序列 题解(卡特兰数/求组合数)
卡特兰数的思路:把求合法01的排列转化为求二维平面上到达(n, n)点合法(y <= x)路径的数量#include<iostream>using namespace std;typedef long long ll;const int mod = 1e9 + 7;int n;int qmi(int a, int k, int p){ int res = 1; while(k){ if(k & 1) res = (ll)res * a % p;原创 2021-09-12 12:42:28 · 66 阅读 · 0 评论 -
AcWing 888 求组合数 IV 题解 (求组合数)
思路:先算出小于a的所有质数,再得出a、b、(a - b)的阶乘中包含的质数的次数,用get(a) - get(b) - get(a - b)即得出组合数中包含的各个质数的次数,然后利用大整数乘法,将这些质数(带次数)乘积算出来,即得结果#include<iostream>#include<algorithm>#include<vector>using namespace std;const int N = 5010;int prime[N], cnt.原创 2021-09-11 22:48:58 · 163 阅读 · 0 评论 -
AcWing 887 求组合数 III 题解 (求组合数)
#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const int mod = 1e9 + 7;int qmi(int a, int k, int p){ int res = 1; while(k){ if(k & 1) res = (ll)res * a % p; a = (ll)a * a % p; k >>= 1; .原创 2021-09-11 21:19:46 · 55 阅读 · 0 评论 -
AcWing 886 求组合数 II 题解 (求组合数)
#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const int N = 1e5 + 10, mod = 1e9 + 7;int heat[N], inheat[N];//分别储存i的阶乘和i的阶乘的逆元int qmi(int a, int k, int p){//快速幂 int res = 1; while(k){ .原创 2021-09-11 12:47:22 · 68 阅读 · 0 评论 -
AcWing 885 求组合数 I 题解 (求组合数)
#include<cstdio>const int N = 2010, mod = 1e9 + 7;int c[N][N];void init(){ for(int a = 0; a < N; a ++ ){ for(int b = 0; b <= a; b ++ ){ if(!b) c[a][b] = 1; else c[a][b] = (c[a - 1][b - 1] + c[a - 1][b]) % mod; //求组合数1的公式(可推:从a原创 2021-09-11 09:29:49 · 50 阅读 · 0 评论 -
AcWing 883 高斯消元解线性方程组 题解(高斯消元)
思路:大一线代矩阵变化解高斯消元#include<iostream>#include<algorithm>#include<cmath>using namespace std;typedef double db;const double eps = 1e-6;const int N = 110;db a[N][N];int n;int gause(){ int c, r; for(c = 0, r = 0; c < n; c ++原创 2021-09-10 23:37:14 · 74 阅读 · 0 评论 -
AcWing 204表达整数的奇怪方式 题解(中国剩余定理)
#include<iostream>using namespace std;typedef long long LL;//数据范围比较大,所以用LL来存储LL exgcd(LL a,LL b,LL &x,LL &y){ if(!b) { x=1,y=0; return a; } LL d=exgcd(b,a%b,y,x); y-=a/b*x; return d;}int main.原创 2021-09-10 18:19:43 · 68 阅读 · 0 评论 -
AcWing 878 线性同余方程 题解 (拓展欧几里得算法)
#include<cstdio>using namespace std;typedef long long ll;int exgcd(int a, int b, int &x, int &y){ if(!b){ x = 1, y = 0; return a; } int d = exgcd(b, a % b, y, x); y -= a / b * x; return d;}int main(){ int n; scanf("%d", &原创 2021-09-09 18:22:23 · 62 阅读 · 0 评论 -
AcWing 877 拓展欧几里得算法 题解(拓展欧几里得)
#include<iostream>#include<cstdio>using namespace std;int exgcd(int a, int b, int &x, int &y){ if(!b){//当达到边界情况 x = 1, y = 0; return a; } int d = exgcd(b, a % b, y , x);//没达到边界情况就继续往下递归 y -= a / b * x; return d;}int .原创 2021-09-09 15:59:46 · 62 阅读 · 0 评论 -
AcWing 876 快速幂求逆元 题解 (快速幂)
#include<cstdio>typedef long long ll;int qmi(int b, int k, int p){ int res = 1; while(k){ if(k & 1) res = (ll)res * b % p; k >>= 1; b = (ll)b * b % p; } return res;}int main(){ int n; scanf("%d", &n); while(n -- )..原创 2021-09-09 12:44:11 · 89 阅读 · 0 评论 -
AcWing 875 快速幂 题解 (快速幂)
#include<cstdio>typedef long long ll;int qmi(int a, int k, int p){//求a的k次幂mod p的值 int res = 1;//用res记录答案 while(k){//当k不为0时就一直循环 if(k & 1) res = (ll)res * a % p;//当k二进制的最后一位为1时,表示k分解的二的次方数中有这一位,此时进行运算 k >>= 1;//k右移一位 a = (ll).原创 2021-09-09 12:06:39 · 92 阅读 · 0 评论 -
AcWing874 筛法求欧拉函数 题解 (筛法求欧拉函数)
思路:欧拉筛+求欧拉函数的公式#include<iostream>using namespace std;typedef long long ll;const int N = 1e6 + 10;int prime[N], cnt;int oulr[N];int n;ll ans;bool st[N];ll get_oulr(int x){ oulr[1] = 1; for(int i = 2; i <= x; i ++ ){ if(!st[i]){/原创 2021-09-08 22:39:02 · 169 阅读 · 0 评论 -
AcWing 873 欧拉函数 题解(欧拉函数)
1∼N 中与 N 互质的数的个数被称为欧拉函数,(互质:和N只有一个公约数为1的数)#include<iostream>using namespace std;int main(){ int n; cin>>n; while(n -- ){ int a; cin>>a; int res = a; for(int i = 2; i <= a / i; i ++ ){ if(a % i == 0){ res = res /原创 2021-09-08 22:00:01 · 66 阅读 · 0 评论 -
AcWing 872 求最大公约数 (欧几里得定理 )
#include<iostream>using namespace std;int gcd(int a, int b){ return b ? gcd(b, a % b) : a;}int main(){ int n; cin>>n; while(n -- ){ int a, b; cin>>a>>b; cout<<gcd(a, b)<<endl; } return 0;}原创 2021-09-08 19:48:35 · 45 阅读 · 0 评论 -
AcWing 871 约数之和 题解
记公式吧#include<iostream>#include<unordered_map>using namespace std;typedef long long ll;const int mod = 1e9 + 7;int main() { int n; cin>>n; unordered_map<int, int> primes; while(n -- ){ int x; cin>>x; for(i原创 2021-09-08 14:09:27 · 91 阅读 · 0 评论 -
AcWing 870 约数个数 题解 (分解质因数)
思路:设n有质因数a1/a2/a2…an,这些底数分别对应的指数为b1/b2/b3…bn,因为每个底数ai取每个值(从0~bi)都会对应一种情况,所以约数之和应该是(b1+1)(b2+1)(b3+1)…(bn+1)#include<iostream>#include<unordered_map>using namespace std;typedef long long ll;const int mod = 1e9 + 7;int main() { int n原创 2021-09-08 13:43:08 · 116 阅读 · 0 评论 -
AcWing 869 试除法求约数 题解 (试除法求约数)
#include<iostream>#include<vector>#include<algorithm>using namespace std;vector<int> get_div(int x){ vector<int> res; for(int i = 1; i <= x / i; i ++ ){ if(x % i == 0){ res.push_back(i); if(i != x / i)原创 2021-09-08 12:13:00 · 73 阅读 · 0 评论 -
欧拉筛/线性筛 模板
#include<iostream>using namespace std;const int N = 1e6 + 10;int prime[N];int cnt;bool st[N];void is_prime(int x){ for(int i = 2; i <= x; i ++ ){ if(!st[i]){ st[i] = true; prime[cnt ++ ] = i; } for(int j = 0; j < cnt &原创 2021-09-08 11:18:55 · 36 阅读 · 0 评论 -
AcWing 867 分解质因数 题解 (试除法分解质因数)
#include<iostream>#include<cstdio>using namespace std;void bt(int x){ for(int i = 2; i <= x / i; i ++ ){ if(x % i == 0){ int s = 0; while(x % i == 0){ s ++ ; x /= i; } cout<<i<<" "<<s<<endl原创 2021-09-07 16:59:01 · 90 阅读 · 0 评论 -
AcWing 866 试除法判断质数 题解 (试除法判断质数)
注意要用i <= x / isqrt函数运行较慢i * i当n接近int的最大值时可能会造成溢出#include<iostream>#include<cstring>using namespace std;bool is_prime(int x){ if( x == 1) return false; for(int i = 2; i <= x / i; i ++ ){ if(x % i == 0) return false; } retu原创 2021-09-06 18:09:20 · 72 阅读 · 0 评论