
---------ACM-数学-------------
数论只会GCD
研二在读
展开
-
1-16数论学习总结
欧几里得算法及扩展1. 欧几里得辗转相除求最大公约数//gcd(a, b)int gcd(int a, int b) { return b ? gcd(b, a % b) : a;}或者//gcd(a, b)int gcd(int a, int b) { int t; while (b) { t = b; b = a % b;原创 2017-01-16 19:17:16 · 486 阅读 · 0 评论 -
hdu 6001 容斥 + dfs
题解不如代码代码:#include <bits/stdc++.h>using namespace std;typedef vector<int> vi;typedef long long ll;const int max_m = 1 << 15;const int max_n = 100001;const ll mod = 1e9 + 7;int n, m;char s[20];l原创 2017-10-11 12:58:50 · 851 阅读 · 0 评论 -
51nod 1238 杜教筛
传送门:51nod 1238题意求G(N)=∑i−1N∑j=1Nlcm(i,j) G(N) = \sum_{i - 1}^N\sum_{j=1}^Nlcm(i, j)题解首先G(N)=∑i=1N∑j=1Nlcm(i,j)=2∑i=1N∑j=1ilcm(i,j)−∑i=1Nlcm(i,i)=2∑i=1Ni∑d|i∑u=1idu[gcd(u,id)=1]−N(N+1)2=2∑i=1Ni∑d|iidϕ原创 2017-08-07 16:37:22 · 842 阅读 · 0 评论 -
51nod 1237 杜教筛
题解参考1238code:#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1000001;const ll mod = 1e9 + 7;const ll inv = (mod + 1) / 2;const int mo = 2333333;bool isPrime[N];ll p原创 2017-08-07 18:51:14 · 473 阅读 · 0 评论 -
51nod 1227
杜教筛#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1000001;const ll mod = 1e9 + 7;const ll inv = (mod + 1) / 2;const ll _6 = (mod + 1) / 6;const int mo = 2333333;bo原创 2017-08-07 18:52:24 · 426 阅读 · 0 评论 -
ZOJ 1489
题解:当n为偶数或者1时显然无解(BSGS过程可证)除此之外2与n互质, 则必存在2^phi(n) = 1, 必有解且最小解应该是phi(n)的最小约数x满足2^x % n = 1;质因子拆分 + 快速幂判断就可CODE: #include #include using namespace std;int FastPowMod(int a, int b, int p){原创 2017-04-22 00:10:20 · 588 阅读 · 0 评论 -
ZOJ 3254
zoj 3254 传送门 : ZOJ 3254题解 : 扩展BSGS + 判断循环节#include #include #include using namespace std;typedef long long ll;typedef unsigned long long ull;const int N = (1 << 16) + 10;ll A, P, D原创 2017-04-20 22:54:28 · 352 阅读 · 0 评论 -
POJ 3243 扩展BSGS
code:#include #include #include using namespace std;typedef long long ll;const int N = (1 << 16) + 10;ll p, b, n;ll FastPowMod(ll a, ll b, ll mod){ ll ret = 1 % mod; while(b){ if(b & 1原创 2017-04-17 19:51:01 · 500 阅读 · 0 评论 -
51 NOD 1135 原根
传送门 : 51 nod 原根 code:#include <iostream>using namespace std;typedef long long ll;ll a[35];int cnt = 0;ll FastPowMod(ll a, ll b, ll p){ ll ret = 1; while(b){ if(b & 1) ret = ret * a原创 2017-04-16 14:35:04 · 474 阅读 · 0 评论 -
poj 2417 baby_step giant_step 高次同余方程
题解链接 : 点击打开链接code : #include #include #include using namespace std;typedef long long ll;const int N = (1 << 16) + 10;struct R{ ll v, id; bool operator < (const R &rhs) const{ ret原创 2017-04-15 16:53:03 · 508 阅读 · 0 评论 -
HDU 5667 矩阵快速幂 + 费马小定理 + 快速幂
题解看完题目第一反应, 矩阵快速幂, 但是乘法无法构造递推想到幂的乘法可以转成指数的加法设f[n] = ap[n],则n > 2时 f[n] = abf[n-1]c f[n-2]=> p[n] = b + p[n - 1] * c + p[n - 2]就可以构造矩阵了且p[1] = 0, p[2] = b构造矩阵 ⎡⎣⎢p[2]00p[1]00b00⎤⎦⎥∗⎡⎣⎢c11100001⎤⎦原创 2017-03-15 22:35:34 · 515 阅读 · 0 评论 -
SPOJ-NUMTRYE 质因子分解 + 欧拉函数应用
传送门:SPOJ -NUMTRYE题解:首先∑ni=1gcd(n,i)=∑d|ndϕ(nd)\sum_{i=1}^ngcd(n, i) = \sum_{d|n}d\phi(\frac{n}{d}) 所以g(n)=∑ni=1n/gcd(n,i)=∑d|nndϕ(nd)=∑d|ndϕ(d)g(n) = \sum_{i=1}^nn/gcd(n, i) = \sum_{d|n}\frac{n}{d}\p原创 2017-04-07 14:45:06 · 660 阅读 · 0 评论 -
UVA 11426 欧拉函数 + 递推
codeimport java.util.*;import static java.lang.System.*;public class Main{ static final int N = 4100000; /**static long c;*/ static long[] phi = new long[N]; static long[] f = new long原创 2017-03-04 18:41:03 · 563 阅读 · 0 评论 -
AOJ 396 矩阵快速幂 + 斐波那契素数
传送门 : AOJ题解 斐波那契素数 : 除了 F[3]和F[4]之后, 质数项Fibonacci为和前面互质 矩阵快速幂优化code#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define mod 10003const int N = 1000000 + 5;int prime[原创 2017-02-23 20:38:57 · 505 阅读 · 0 评论 -
LightOj 1259 Goldbach`s Conjecture 素数筛法
链接 : LOJ 1259题解 一般素数筛法, 注意空间复杂度code#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int N = 10000000;int prime[N / 10 + 5]; bool f[N + 5];/**bool型省空间*/void getPrime原创 2017-02-19 11:45:38 · 470 阅读 · 0 评论 -
Wannafly模拟赛5 F Course
退役后刷的第一道题…题解:n!内某个因子a的指数: 每次轮流除以a, 当前商+剩余贡献即可 例: 9! : 9 / 3 = 3; 3 / 3 = 1 ans = 4对于[ka, ka + a - 1], 阶乘中因子a的指数相同定义cal(i,n)是i!中因子n的指数cal(i, n)是i!中因子n的指数 ans=∑i=1nmcal(i,n)=cal(nm,n)+∑i=1nm−1c原创 2017-11-07 14:33:22 · 947 阅读 · 0 评论