zoukankan      html  css  js  c++  java
  • 数论模板


     
    1. //求gcd(a, b)   
    2. LL gcd(LL a, LL b)  
    3. {  
    4.     return b ? gcd(b, a%b) : a;  
    5. }  

     
    1. //求整数x和y,使得ax+by=d, 且|x|+|y|最小。其中d=gcd(a,b)   
    2. void gcd(LL a, LL b, LL& d, LL& x, LL& y)  
    3. {  
    4.     if(!b)  
    5.     {  
    6.         d = a;  
    7.         x = 1;  
    8.         y = 0;  
    9.     }  
    10.     else  
    11.     {  
    12.         gcd(b, a%b, d, y, x);  
    13.         y -= x * (a/b);  
    14.     }  
    15. }  

     
    1. //计算模n下a的逆。如果不存在逆, 返回-1   
    2. LL inv(LL a, LL n)  
    3. {  
    4.     LL d, x, y;  
    5.     gcd(a, n, d, x, y);  
    6.     return d == 1 ? (x+n)%n : -1;  
    7. }  


     


     
    1. //筛素数   
    2. void sieve(int n)  
    3. {  
    4.     int m = sqrt(n+0.5);  
    5.     memset(vis, 0, sizeof(vis));  
    6.     vis[0] = vis[1] = 1;  
    7.     for(int i = 2; i <= m; i++)  
    8.         if(!vis[i])  
    9.             for(int j = i*i; j <= n; j += i)  

                    vis[j] = 1;  

    1. }  
    2. int get_primes(int n)  
    3. {  
    4.     sieve(n);  
    5.     int c = 0;  
    6.     for(int i = 2; i <= n; i++)  
    7.         if(!vis[i])  
    8.             prime[c++] = i;  
    9.     return c;  
    10. }  

     
    1. //返回a^p mod n 快速幂  
    2. LL pow_mod(LL a, LL p, LL n)  
    3. {  
    4.     LL ans = 1;  
    5.     while(p)  
    6.     {  
    7.         if(p&1)  
    8.         {  
    9.             ans *= a;  
    10.             ans %= n;  
    11.         }  
    12.         a *= a;  
    13.         a %= n;  
    14.         p >>= 1;  
    15.     }  
    16.     return ans;  
    17. }  

     
    1. //欧拉phi函数   
    2. int euler_phi(int n)  
    3. {  
    4.     int m = sqrt(n+0.5);  
    5.     int ans = n;  
    6.     for(int i = 2; i <= m; i++)  
    7.     {  
    8.         if(n % i == 0)  
    9.         {  
    10.             ans = ans / i * (i-1);  
    11.             while(n % i == 0)  
    12.                 n /= i;  
    13.         }  
    14.     }  
    15.     if(n > 1)  
    16.         ans = ans / n * (n-1);  
    17.     return ans;  
    18. }  

     
    1. //用类似筛法的方式计算phi(1), phi(2), ..., phi(n)  
    2. LL phi[maxn];  
    3. void phi_table(int n)  
    4. {  
    5.     for(int i = 2; i <= n; i++)  
    6.         phi[i] = 0;  
    7.     phi[1] = 1;  
    8.     for(int i = 2; i <= n; i++)  
    9.         if(!phi[i])  
    10.             for(int j = i; j <= n; j += i)  
    11.             {  
    12.                 if(!phi[j])  
    13.                     phi[j] = j;   
    14.                 phi[j] = phi[j] / i * (i-1);  
    15.             }  
    16. }  

    1. void phi()  
    2. {  
    3.     for(int i=1; i<N; i++)  p[i] = i;  
    4.     for(int i=2; i<N; i+=2) p[i] >>= 1;  
    5.     for(int i=3; i<N; i+=2)  
    6.     {  
    7.         if(p[i] == i)  
    8.         {  
    9.             for(int j=i; j<N; j+=i)  
    10.                 p[j] = p[j] - p[j] / i;  
    11.         }  
    12.     }  
    13. }  




     
    1. //中国剩余定理   
    2. LL china(int n, int* a, int* m)  
    3. {  
    4.     LL M = 1, d, y, x = 0;  
    5.     for(int i = 0; i n; i++)  
    6.         M *= m[i];  
    7.     for(int i = 0; i n; i++)  
    8.     {  
    9.         LL w = M /m[i];  
    10.         gcd(m[i], w, d, d, y);  
    11.         x = (x + y*w*a[i]) % M;  
    12.     }  
    13.     return (x+M)%M;  
    14. }  


    二分法求等比数列 1+q^1+q^2+...+q^n的和

     
    1. LL db(LL p, LL x)  
    2. {  
    3.     if(!x)  
    4.         return 1;  
    5.     if(x&1)  
    6.         return (db(p, x/2)*(1+pow_mod(p, x/2+1)))%mod;  
    7.     else  
    8.         return ((db(p, x/2-1)*(1+pow_mod(p, x/2+1)))+pow_mod(p, x/2))%mod;  
    9. }  


     梅森素数指数表

     
    1. int kiss[]={0,2,3,5,7,13,17,19,31,61,89,  
    2.             107,127,521,607,1279,2203,2281,3217,4253,4423,  
    3.             9689,9941,11213,19937,21701,23209,44497,86243,110503,132049,  
    4.             216091,756839,859433,1257787,1398269,2976221,3021377,6972593};  


     同构数表

     
      1. char number1[2001]=  
      2. "0302695456948792438016548848805106486276062082716415913252360  
      3. 9790500938385405426324719893931802209823600162545177681029159  
      4. 3965045066578090330527721983852863418796455114247485363072354  
      5. 5704904450912521423427595549184397398445871252869481982692702  
      6. 9255264834903206526851272202961318699947776535481291519857640  
      7. 4229681830917734452777232007376038258831727292795636574190144  
      8. 4523595431910306357249617898820317578776106213770808096781137  
      9. 4931911766563031490205784352509572880668464121069252802275061  
      10. 2985116162063840067789794024490238751112586895345495148882006  
      11. 7866770234100283954928297028644727362521753544319791185506815  
      12. 7264858804852673871684804002188529473022223344541221328464844  
      13. 1535937936631336044589403287234784019473575603613462120086753  
      14. 7334691331433871735088021260028575298538664393102232655345477  
      15. 6845029957025561658143370236502074744856814787872902092412582  
      16. 9053012491246688683515876774998917686787157281349408792768945  
      17. 2979709777230540335661882819870221063055796723980661119019774  
      18. 4642421025136748701117131278125400133690086034889084364023875  
      19. 7659368219796261819178335204927041993248752378258671482789053  
      20. 4489744014261231703569954841949944461060814620725403655999827  
      21. 1588356035049327795540741961849280952093753026852390937562839  
      22. 1485716123673519706092242423987770075749557872715597674134589  
      23. 9753769551586271888794151630756966881635215504889827170437850  
      24. 8028434084412644126821848514157729916034497017892335796684991  
      25. 4473895660019325458276780006183298544262328272575561107331606  
      26. 9701586498422229125548572987933714786632317240551575610235254  
      27. 3994999345608083801190741530060056055744818709692785099775918  
      28. 0500754164285277081620113502468060581632761716767652609375280  
      29. 5684421448619396049983447280672190667041724009423446619781242  
      30. 6690787535944616698508064636137166384049029219341881909581659  
      31. 5244778618461409128782984384317032481734288865727376631465191  
      32. 0498802944796081467376050395719689371467180137561905546299681  
      33. 4764263903953007319108169802938509890062166509580863811000557  
      34. 423423230896109004106619977392256259918212890625",number2[2001]=  
      35. "9697304543051207561983451151194893513723937917283584086747639  
      36. 0209499061614594573675280106068197790176399837454822318970840  
      37. 6034954933421909669472278016147136581203544885752514636927645  
      38. 4295095549087478576572404450815602601554128747130518017307297  
      39. 0744735165096793473148727797038681300052223464518708480142359  
      40. 5770318169082265547222767992623961741168272707204363425809855  
      41. 5476404568089693642750382101179682421223893786229191903218862  
      42. 5068088233436968509794215647490427119331535878930747197724938  
      43. 7014883837936159932210205975509761248887413104654504851117993  
      44. 2133229765899716045071702971355272637478246455680208814493184  
      45. 2735141195147326128315195997811470526977776655458778671535155  
      46. 8464062063368663955410596712765215980526424396386537879913246  
      47. 2665308668566128264911978739971424701461335606897767344654522  
      48. 3154970042974438341856629763497925255143185212127097907587417  
      49. 0946987508753311316484123225001082313212842718650591207231054  
      50. 7020290222769459664338117180129778936944203276019338880980225  
      51. 5357578974863251298882868721874599866309913965110915635976124  
      52. 2340631780203738180821664795072958006751247621741328517210946  
      53. 5510255985738768296430045158050055538939185379274596344000172  
      54. 8411643964950672204459258038150719047906246973147609062437160  
      55. 8514283876326480293907757576012229924250442127284402325865410  
      56. 0246230448413728111205848369243033118364784495110172829562149  
      57. 1971565915587355873178151485842270083965502982107664203315008  
      58. 5526104339980674541723219993816701455737671727424438892668393  
      59. 0298413501577770874451427012066285213367682759448424389764745  
      60. 6005000654391916198809258469939943944255181290307214900224081  
      61. 9499245835714722918379886497531939418367238283232347390624719  
      62. 4315578551380603950016552719327809332958275990576553380218757  
      63. 3309212464055383301491935363862833615950970780658118090418340  
      64. 4755221381538590871217015615682967518265711134272623368534808  
      65. 9501197055203918532623949604280310628532819862438094453700318  
      66. 5235736096046992680891830197061490109937833490419136188999442  
      67. 576576769103890995893380022607743740081787109376";  
  • 相关阅读:
    mysqldump 导出数据库为DBname的表名为Tname的表结构 导出数据库的所有表的表结构
    mysqldump 备份某张表 Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions,
    nfs missing codepage or helper program, or other error
    date 增加一个小时 减少一个小时
    mysqldump 备份单个数据库
    mysql删除账户
    怎么删除某个用户的所有帖子?
    mongodb删除重复数据
    ReSharper2018破解详细方法
    激活windows和office
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4472515.html
Copyright © 2011-2022 走看看