随笔分类 - 
        
            数论
        
    
        
            
    HDU 4704
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4704求(2^n)%mod的方法#include #include #include #include #include #include using namespace std ;//(2^n)%mod=(2^(...
         阅读全文
 
            
         
        
            
    HDU 4861
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4861结论题:p是奇素数,1^n+2^n+...+(p-1)^n=0(mod p),其中p-1不整除n#include #include #include #include #include #include #i...
         阅读全文
 
            
         
        
            
    XMU 1246
    
            
摘要:http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1246求区间内素数个数,经典问题,区间长度10^6,数的取值最多能到10^12(此题范围稍小)用筛法搞出[2,根号b]范围内的素数,用这些素数再去筛[a,b]一个吐血的trick,1不是素数#incl...
         阅读全文
 
            
         
        
            
    乘法逆元
    
            
摘要:d模f的乘法逆元int Extend_Eulid(int d,int f){ int x1,x2,x3,y1,y2,y3 ; x1=1,x2=0,x3=f,y1=0,y2=1,y3=d ; while(y3 && y3!=1) { int q=x3/y3 ; int t1,t2,t3 ; t1=x1-q*y1,t2=x2-q*y2,t3=x3-q*y3 ; x1=y1,x2=y2,x3=y3 ; y1=t1,y2=t2,y3=t3 ; } if(!y3)return -1 ; ret...
         阅读全文
 
            
         
        
            
    HDU 1452
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1452原来真心没见过这种题,不会做,非常帅gcd(a,b)==1 && s(a,b)==s(a)*s(b)满足这种条件的s叫做积性函数,本题求的因子和就是一个积性函数接着有一个结论if(prime[p])s(p^n)=1+p^1+p^2+p^n=(p^(n+1)-1)/(p-1)s(2004^n)=s(2^(2n))*s(3^n)*s(167^n)其中,167和22关于29同余所以,s(2004^n)=s(2^(2n))*s(3^n)*s(2^n)a=s(2^(2n))=(2^(2n+1)-1
         阅读全文
 
            
         
        
            
    hdu 1098
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1098假设x=m时,65|f(m),即65|5*m^13+13*m^5+k*a*m计算f(m+1)=(5*m^13+13*m^5+k*a*m)+65*(m^12+6*m^11+22*m^10+55*m^9+99*m^8+132*m^7+132*m^6+99*m^5+56*m^4+24*m^3+8*m^2+2*m)+(18+k*a)式子的前两部分显然能被65整除,此时如果65|(18+k*a),那么65|f(m+1)。同时观察到f(1)=18+k*a,所以如果65|f(1),则65|f(m+1),此时对于所有x
         阅读全文
 
            
         
        
            
    HDU 2973 YAPTCHA
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2973威尔逊定理,详情参见数论四大定理。从这道题应用的层面讲,我们只需要知道若p为素数,则p可整除(p-1)!+1。有上面的结论这道题就解决了,3k+7为素数则答案加1,为非素数则不变。(代数式简单变化一下很容易看出来,此处不赘述)为了防止超时,提前把答案打表打出来就好。输入输出挂,果断刷到第一。View Code #include <iostream>using namespace std ;bool prime[3000008] ;int ans[1000001] ;inline bool s
         阅读全文
 
            
         
        
            
    HDU 1395 2^x mod n = 1
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1395枚举x复杂度可以处理成O(n),由于我是个sb,所以给弄成O(nlogn)了。这里用的方法基于欧拉定理。欧拉定理表明,若n,a为正整数,且n,a互质,(a,n) = 1,则a^φ(n) ≡ 1 (mod n)。我们处理的当然是(2,n)=1的情况,枚举欧拉函数的因子即可View Code #include <stdio.h>__int64 qpow(int a,__int64 b,int r){ __int64 ans=1,buff=a; while(b) { if(b&...
         阅读全文
 
            
         
        
            
    HDU 1905 Pseudoprime numbers
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1905水题,套个米勒拉宾的模板就秒了View Code #include <stdio.h>__int64 qpow(int a,int b,int r)//快速幂 { __int64 ans=1,buff=a; while(b) { if(b&1)ans=(ans*buff)%r; buff=(buff*buff)%r; b>>=1; } return ans;}bool Miller_Rabbin(int n,int a)//米勒拉宾...
         阅读全文
 
            
         
        
            
    HDU 2161 Primes
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2161素数判断,这题数据范围小的可怜,普通的筛法肯定没问题,不过为了练习我还是拿米勒拉宾测试写的。ps:为2b的自己流了一地泪View Code #include <iostream>using namespace std ;__int64 qpow(int a,int b,int r){ __int64 ans=1,buff=a; while(b) { if(b&1)ans=(ans*buff)%r; buff=(buff*buff)%r; b>>...
         阅读全文
 
            
         
        
            
    HDU 2138 How many prime numbers
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2138米勒拉宾素数测试,解大规模素数问题。View Code #include <iostream>using namespace std ;__int64 qpow(int a,int b,int r){ __int64 ans=1,buff=a; while(b) { if(b&1)ans=(ans*buff)%r; buff=(buff*buff)%r; b>>=1; } return ans;}bool Miller_Rabbin...
         阅读全文
 
            
         
        
            
    HDU 3501 Calculation 2
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=3501继续欧拉函数,关键点是求小于n且与n互质的数的和。公式是Eular(n)*n/2,简略证明如下:原问题等价于一个数n,如果a与它互质,那么n-a也与它互质;反证法,假设n与a互质,n与n-a不互质设n和n-a的最大公约数为m,则n=p*m,n-a=q*m所以a=(p-q)*m,推导出a和n有公约数m,与假设矛盾View Code #include <iostream>#include <cstdlib>#include <cstring>#include <st
         阅读全文
 
            
         
        
            
    HDU 1787 GCD Again
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1787依然是欧拉函数的模板题,求有多少个小于等于N且不与N互质的数,减去N本身再减去和N互质的就好了,小于N且和N互质的数的个数就用欧拉函数算了View Code #include <iostream>#include <cstdlib>#include <cstring>#include <string>#include <stack>#include <queue>#include <map>#include <al
         阅读全文
 
            
         
        
            
    HDU 2824 The Euler function
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2824同上一题,裸欧拉函数View Code #include <iostream>#include <cstdlib>#include <cstring>#include <string>#include <stack>#include <queue>#include <map>#include <algorithm>using namespace std;const int MAX=3000001;int ph
         阅读全文
 
            
         
        
            
    HDU 1286 找新朋友
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1286裸欧拉函数,欧拉函数是求比n小的数中有多少个与n互质View Code #include <iostream>#include <cstdlib>#include <cstring>#include <string>#include <stack>#include <queue>#include <map>#include <algorithm>using namespace std;const int MA
         阅读全文
 
            
         
        
            
    HDU 1212 Big Number
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1212大整数取模问题(a+b) mod n=((a mod n)+(b mod n)) mod nView Code #include <stdio.h>#include <string.h>int main(){ char s[1100]; int n; int len,ans,i; while(~scanf("%s%d",s,&n)) { len=strlen(s); ans=0; for(i=0;i<len;i++) ...
         阅读全文
 
            
         
        
            
    HDU 2710 Max Factor
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2710找出拥有最大素因子的数,基本的素数筛选View Code #include <stdio.h>int prime[110000];int main(){ int t,n; int i,j; int max,maxnum; for(i=2;i*i<=100001;i++) if(!prime[i]) for(j=i;j*i<=100001;j++) prime[i*j]=1; while(~scanf("%d"...
         阅读全文
 
            
         
        
            
    BJTU 1188 素数筛选
    
            
摘要:http://acm.bjtu.edu.cn/problem/detail?pid=1188百万级素数筛选,果断埃斯托拉尼筛法View Code #include <stdio.h>int prime[1000001];int ans[1000001]={0,0,1};int main(){ int i,j,n; for(i=2;i*i<=1000000;i++) if(!prime[i]) for(j=i;j*i<=1000000;j++) prime[j*i]=1; for(i=3;i<=1000000;i...
         阅读全文
 
            
         
        
            
    HDU 1397 Goldbach's Conjecture
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1397和2136有共通之处,学习了构造素数表的方法,在这里初次使用。按因子从小到大的顺序筛选,去掉2的倍数,去掉3的倍数......最后剩下的就是素数了。View Code #include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>int prime[40000];int main(){ int n; for(int i=2;i*i<32768;i++) { if(!p
         阅读全文
 
            
         
        
            
    HDU 2136 Largest prime factor
    
            
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2136求n的最大素因子在素数表中的位置。如代码循环,因为如果在前面被计算过则肯定不是素数。View Code #include <stdio.h>#include <string.h> #include <stdlib.h>#include <math.h>int rank[1100000]; int main() { int n,cnt=1; for(int i=2;i<1000001;i++) { if(rank[i])continue; for(in
         阅读全文
 
            
         
    
 
    
 
 | 
                 
             
         |