随笔分类 - 数论
摘要:数论,判断整数N的正约数之和和N之间的大小关系,先素数分解,然后用欧拉函数的一个变形就可以搞定了。注意输出的时候的格式要求,用setw/*time: 0MSresult:AC*/#include"iostream"#include"math.h"#include"iomanip"using namespace std;bool isprime[100];int prime[100];int k=0;struct p{ double x; double i;};void getprime(){ int i=2,j; for(i;i<
阅读全文
摘要:Miller_Rabin大素数探定还有 pollard_rho质因数分解。TL了三次,肯定不是算法不行,应该是存在死循环了。/*poj 1811 prime testresult:ACtime:329MSlanguage:c++memory:168K*/#include"stdio.h"#include"time.h"#include"iostream"using namespace std;int pri[]={2,3,5,7,11,13,17,19,23,29};__int64 gcd(__int64 a,__int64 b){
阅读全文
摘要:/*O(lg(N)*/#include"iostream"using namespace std;long Countk2(long n,long k){ long count = 0; long i = 1; long current = 0,after = 0,before = 0; while((n / i) != 0) { current = (n / i) % 10; before = n / (i * 10); after = n - (n / i) * i; if (current > k) count = count + (before + 1) *
阅读全文
摘要:用归并排序求逆序对,用时391ms,但是用树状数组的话runtime error。#include <stdio.h> #include <string.h> const int N=500000;int a[N], b[N], n; __int64 Calc(int first, int last) { if (first == last-1) return 0; int mid = (first+last)/2, i = first, j = mid, k = first; __int64 res = Calc(first, mid)+Calc(mid, last)
阅读全文
摘要:The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few areF2 = {1/2}F3 = {1/3, 1/2, 2/3}F4 = {1/4, 1/3, 1/2, 2/3, 3/4}F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4,
阅读全文
摘要:判断数N在k进制下是否为回文数,其中k∈[2,16],输出K。wa了一次,原因:在如果不存在k的情况下,不小心在n前面加了个& 输出了n的地址,导致错误。#include"stdio.h"int s[50000];int main(){ //freopen("1078b.txt","w",stdout); int n; while(scanf("%d",&n)!=EOF,n) { int base[16]; int i,k=0; for(i=2;i<=16;i++) { int j=0,tmp
阅读全文
摘要:zoj控制格式真的很严格,PE了一次。#include"iostream"using namespace std;int s[30]={0};int before(int j){ int k=j; for(j;j>=0;j--) if(s[j]){ s[j]--; break; } return k-j+1;}int main(){ int t; cin>>t; while(t--) { int n,p[30],w[30],i; cin>>n; for(i=0;i<n;i++) { cin>>p[i]; if(i==0) s[
阅读全文
摘要:是不是zoj不能添加注释的啊?很奇怪。这是1057的AC代码#include"stdio.h"#include"math.h"int main(){ int n,j; for(j=0;;j++) { scanf("%d",&n); if(n==0) break; if(j) printf("\n"); int a[20],b[20],i; int aa=0,bb=0; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i+
阅读全文
摘要:利用积性函数的优化.这个文章主要介绍了3算法1线性时间筛素数2线性时间求前n个数的欧拉函数值3线性时间求前n个数的约数个数一、首先介绍下积性函数。下面是wiki的条目:在非数论的领域,积性函数指有对于任何a,b都有性质f(ab)=f(a)f(b)的函数。在数论中的积性函数。对于正整数n的一个算术函数f(n),当中f(1)=1且当a,b互质,f(ab)=f(a)f(b),在数论上就称它为积性函数。若某算术函数f(n)符合f(1)=1,且就算a,b不互质,f(ab)=f(a)f(b),称它为完全积性的。例子φ(n)-欧拉φ函数,计算与n互质的正整数之数目μ(n)-默比乌斯函数,关于非平方数的质因子
阅读全文
摘要:假若相遇,则存在 x+ms≡y+ns(mod l)=>(x+ms)-(y+ns)=kl;=>x-y-(n-m)s=kl;(已知x,y,n,m,l)=>kl+(n-m)s=x-y;即求满足 kl+bs=a(已知 l,b,a)最小的s;——————————扩展欧几里德算法-求解不定方程,线性同余方程:解不定方程ax + by = n的步骤如下:(1)计算gcd(a, b). 若gcd(a, b)不能整除n,则方程无整数解;否则,在方程的两边同除以gcd(a, b), 得到新的不定方程a'x + b'y = n',此时gcd(a', b')
阅读全文
摘要:求N个同余方程,用扩展欧几里得。扩展欧几里得算法是用来在已知a,b的情况下求解一组x,y 使得 ax+by = gcd(a,b)原理:gcd(a,b) = gcd(b, a%b)=> ax+by = gcd(a,b) = gcd(b, a%b);=>ax+by=bx+(a%b)y;=>ax+by=bx1+(a-(a/b)*b)y1=ay1+bx1-(a/b)*by1;=>x=y1;y=x1-(a/b)*y1;#include"iostream"using namespace std;__int64 egcd(__int64 a,__int64 b,_
阅读全文
摘要:典型的中国余数定理的应用。设m1,m2,..,mk是k个两两互素的正整数,m=m1*m2*...*mk,Mi=m/mi(i=1,2,..,k)。则同余方程组x≡b1(mod m1)x≡b2(mod m2)......x≡bk(mod mk)有唯一解。x≡M'1M1b1+…+M'kMkbk (modm),其中M'iMi≡1 (modmi),i=1,2,…,k举个最经典的例子,今有物不知其数,三三数之余二 ,五五数之余三 ,七七数之余二,问物几何?除数序列{3,5,7};对应的余数序列{2,3,2}接下来我们需要求一个数k1满足k1 %3==1&&k1%5=
阅读全文
摘要:题意是:求出满足2^x mod n = 1的x。这样的x叫做2模n的次数。我使用余数的性质递推的,应该还有好的算法。#include"stdio.h" int main() { int n; while(scanf("%d",&n)!=EOF) { if(n==1||n%2==0) { printf("2^? mod %d = 1\n",n);continue;} int ans,k=1;long long s=2; ans=s%n; while(ans!=1) { s=ans*2; ans=s%n; k++; } print
阅读全文
摘要:Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits: { 0-9,A-Z,a-z } HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original
阅读全文
摘要:Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!高精度处理,用字符串来存储。#include"iostream"using namespace std;char ans[35661];//10000的时候是35660位数int main(){ int n; while(cin>>n) { int i,digit=1; ans[0]=1; for(i=2;i<=n;i++) { int j,k; for(j=k=0;j<digit;j++) { int temp=ans[j]
阅读全文
摘要:Input第一行是一个整数T,表示测试组数。接下来T行,每行一个整数 n ( 1<= |n| <=10^5 )。 Output以实数形式输出1/n 。如果是循环小数的,只输出第一个循环节。 非循环节部分的长度k=max{abs(分子分母2的因子个数之差),abs(分子分母5的因子个数之差)}然后再输出直到重复为止就好了。#include"iostream" using namespace std; int before(int n) //计算非循环节长度k { int c2=0,c5=0; while(n%2==0) { c2++; n/=2; } while(
阅读全文
摘要:大意是输入a,b;求出有多少对数p,q满足GCD(p,q) =a;LCM(p,q) =b。输出对数。思路是令x=lcm/gcd(如果lcm不能被整除,则输出0);然后再将x素因子分解,判断x有k个互异的素因子,对数为2^k。代码如下:#include"stdio.h"#include"math.h"int main(){int a,b;while(scanf("%d%d",&a,&b)!=EOF){ if(b%a!=0){printf("0\n");continue;}int n=0,ans=1;b
阅读全文
摘要:求各数位之和;运用弃九法可以很快解出,但一开始没想到出入的会是字符串,WA了几次,AC代码如下#include"iostream"#include"string"using namespace std;int main(){ string str; while(cin>>str,str!="0") { int n=0; for(string::size_type i=0;i!=str.size();i++) n+=str[i]-'0'; n%=9; if(n==0) n=9; cout<<n&l
阅读全文
浙公网安备 33010602011771号