数论

数学--数论--HDU1792A New Change Problem(GCD规律推导)

我是研究僧i 提交于 2019-12-18 22:10:44
A New Change Problem Problem Description Now given two kinds of coins A and B,which satisfy that GCD(A,B)=1.Here you can assume that there are enough coins for both kinds.Please calculate the maximal value that you cannot pay and the total number that you cannot pay. Input The input will consist of a series of pairs of integers A and B, separated by a space, one pair of integers per line. Output For each pair of input integers A and B you should output the the maximal value that you cannot pay and the total number that you cannot pay, and with one line of output for each line in input. Sample

数论--费马小定理求逆元

拜拜、爱过 提交于 2019-12-09 21:41:49
int Fermat_inverse ( int a , int mod ) { int res = 1 ; for ( int i = 1 ; i < mod - 1 ; ++ i ) res * = a ; return res ; } //如果p为大素数,我们可以用快速幂求解,时间复杂度为: long long fast_pow_mod ( long long a , long long b , long long mod ) { long long res = 1 ; while ( b ) { if ( b & 1 ) res = ( res * a ) % mod ; a = ( a * a ) % mod ; b >>= 1 ; } return res ; } long long Fermat_inverse ( long long a , long long mod ) { return fast_pow_mod ( a , mod - 2 , mod ) ; } 来源: CSDN 作者: 张俊浩 链接: https://blog.csdn.net/weixin_43627118/article/details/103464206

ACM数论----卢卡斯定理

寵の児 提交于 2019-12-07 12:45:02
1.拓展欧几里得 + 卢卡斯 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<cmath> using namespace std; typedef long long LL; const int maxn = 1000000 + 10; LL n,k; LL fac[maxn],mod; LL ex_gcd(LL a,LL b,LL &x,LL &y){//拓展欧几里得 if(b==0){x = 1;y = 0;return a;} LL ans = ex_gcd(b,a%b,x,y); LL temp = x; x = y; y = temp - (a/b)*y; return ans; } LL inv(LL a){//求逆元 LL x,y; int ans = ex_gcd(a,mod,x,y); if(ans!=1)return -1; if(x<0)x = (x%mod+mod)%mod; return x; } void solve(){ fac[0] = 1; for(int i = 1;i<=maxn-1;i++){ fac[i] = (fac[i-1]*i)%mod; } } LL comb(LL n,LL k

codeforces 359 C (数论)

こ雲淡風輕ζ 提交于 2019-12-06 15:06:23
题意:给定质数x,有n个1/(x^ai) (1<=i<=n)相加,分母为x^(a1 + a2 + a3 + …… + an),分子为T。求分子,分母最大公因数。 思路:设分母为:dwn,x^p的个数为mp[p]。分子为 x^(a1 + a2 …… + an - ai)相加。因为an最大,所以分子第n项最小。提出该项dwn / (x^an) == x((分母次数) - an),设总次数为(分母次数 - div)如果该项系数(个数)% x == 0,则mp[div - 1+=mp[div] / x;div--; #include <bits/stdc++.h> #include <unordered_map> using namespace std; #define N 500010 #define maxn (N + 10) #define Max(a,b) (a > b ? a : b) #define Min(a,b) (a < b ? a : b) typedef long long ll; const ll INF = (1e9) + 10; const ll mod = (1e9) + 7; unordered_map <int,int> mp; ll f_pow(ll x,ll p){ ll ans = 1; while(p){ if(p % 2 == 1){ ans *

拓展欧几里得算法(数论)

家住魔仙堡 提交于 2019-12-04 20:44:51
拓展欧几里得 是用来计算已经知道 a 和 b 的时候,让你去求 a*x+b*y=GCD(a,b) 的一组解 (x,y) 。(首先根据一些相关数论证明,这个解是一定存在的) GCD(a,b) 应该都知道吧,求a和b的最小公约数。 因为GCD(a,b)=GCD(b,a%b),所以a*x+b*y=GCD(b,a%b),也就意味着a*x+b*y=b*x-(a%b)*y,进而可以得出x*a+y*b=y*a+(x-y*a/b)*b; 最后推到最后是当b为0的时候,a为最小公约数,根据第一个式子,a*x+b*y=a; (但是此时的x和y是进过多次替换过的,并不是最开始的那个x,y了) 但是我们可以递归回我们需要的那个最开始的x,y。 这就是拓展欧几里得。 代码实现: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int x,y; int ex_gcd(int a,int b) { int ret,tem; if(!b) { x=1;y=0; return a; } ret=ex_gcd(b,a%b); tem=x; x=y; y=tem-a/b*y; return ret; } int main() { int a,b,z; scanf("%d%d"

数论复习

不打扰是莪最后的温柔 提交于 2019-12-04 08:49:23
质数: 试除法 bool is_prime(int n) { if(n < 2) return false; for(int i = 2;i <= sqrt(n);i++) if(n%i == 0) return false; return true; } 来源: https://www.cnblogs.com/kwei/p/11851053.html

AtCoder - 2286 (数论——唯一分解定理)

烂漫一生 提交于 2019-12-04 06:38:25
题意 求n!的因子数%1e9+7。 思路 由唯一分解定理,一个数可以拆成素数幂之积,即2^a * 3^b *……,n!=2*3*……*n,所以计算每个素因子在这些数中出现的总次数(直接对2~n素因子分解即可),再用唯一分解定理公式,因子数=(a+1)*(b+1)*……。 代码 #include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f #define ll long long const int N=200005; const int mod=1e9+7; const double eps=1e-8; const double PI = acos(-1.0); #define lowbit(x) (x&(-x)) int a[N]; int main() { std::ios::sync_with_stdio(false); ll n; cin>>n; for(int i=2;i<=n;i++) { int ii=i; for(int j=2;j<=i;j++) { while(ii%j==0) { a[j]++; ii/=j; } } } ll ans=1; for(int i=2;i<=n;i++) { // cout<<a[i]<<endl; ans=(ans*(a[i]+1))%mod; }

数学笔记 - 数论 - Min_25筛

孤街醉人 提交于 2019-12-03 20:24:20
终于知道发明者的正确的名字了,是 Min_25 ,这个筛法速度为亚线性的 \(O(\frac{n^{\frac{3}{4}}}{\log x})\) ,用于求解具有下面性质的积性函数的前缀和: 在 \(p\) 处是简单的低次多项式 在 \(p^c\) 处可以快速求值 来源: https://www.cnblogs.com/KisekiPurin2019/p/11806890.html

数学笔记 - 数论

两盒软妹~` 提交于 2019-12-03 17:14:08
由于某些众所周知的原因,都是列出来icpc里面的常jian用guo的简单数论知识,应该都不会进行证明,甚至会不严谨,比如对0和1这样的数的特殊判定,都是根据实际情况特别处理。毕竟目前只讲究应用而不关心理论的优美。 因数 假如存在一个整数 \(k\) ,使得 \(n=kd\) ,则称 \(d\) 整除 \(n\) ,或者 \(d\) 是 \(n\) 的一个因数。 不过一般题目中指的都是正因数,有些奇葩题里面会有负的因数,负数在模意义下的运算有点奇怪。 质数 恰好有2个正因数的正整数。 合数 有大于2个正因数的正整数。 众所周知,1既不是质数也不是合数,不过在很多情况下它拥有与质数类似的表现(比如各种数论函数),但是由于它和算术基本定理有些瓜葛,还是单列出来方便。 所以0是质数还是合数呢?应该质数合数这些概念是对正整数才适用的吧。反正都是特例特殊判断就可以了。(0在有些函数中表现得奇怪,比如阶乘,应该是充当一个单位元的角色,而不是简单的全部认为函数值是0) 质数定理 \(x\) 以内(一般认为“ \(x\) 以内”都是指 \([1,x]\) )的质数的个数,记为 \(\pi(x)\) ,质数定理给出这个函数的一个很好的估计 \(\pi(x)~\frac{x}{\ln(x)}\) ,这个 \(\ln\) 很明显就是指自然对数了。 常见的,1e5以内的质数个数不超过9.6e3

数论题目小记

女生的网名这么多〃 提交于 2019-12-03 16:31:39
素数个数 【题目描述】求n到n+m内的素数个数 【解题报告】 “数论题目有时复杂度看着很大,实际上并没有那么大” 详见代码 #include <bits/stdc++.h> #define ll long long using namespace std; const int N = 1e6 + 3; ll n, m, pr[N], tot, s[N]; bool vis[N]; int main() { int T; scanf("%d", &T); while (T--) { scanf("%lld%lld", &n, &m); memset(vis, 0, sizeof vis); tot = m + 1; for (ll i = 2; i <= 1e6 && i <= n + m; i++) for (ll j = (n / i) * i; j <= n + m; j += i) //保证跳到的都是合数 if (j >= n && j != i && !vis[j - n]) { vis[j - n] = 1; tot--; //素数的个数等于总数-合数 } printf("%lld\n", tot); } return 0; } 来源: https://www.cnblogs.com/phemiku/p/11802954.html