加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

NJUST1722(大数分解的应用)

发布时间:2020-12-14 04:02:02 所属栏目:大数据 来源:网络整理
导读:? NJUST1722(大数分解的应用) 分类:?数论 2013-08-01 20:40 ? 125人阅读 ? 评论(0) ? 收藏 ? 举报 题目:所有的平方差 题意: n = a1*a1 - b1*b1,n = a2*a2 - b2*b2,...,n = am*am - bm*bm 其中对于任意的1=k1,k2=m,ak1 != ak2 或者 bk1 != bk2,并且ak1,a
?

NJUST1722(大数分解的应用)

分类:?数论 ? 125人阅读? 评论(0)? 收藏? 举报

题目:所有的平方差


题意:n = a1*a1 - b1*b1,n = a2*a2 - b2*b2,...,n = am*am - bm*bm

其中对于任意的1<=k1,k2<=m,ak1 != ak2 或者 bk1 != bk2,并且ak1,ak2,bk1,bk2>=0

那么我们需要求的是n^(a1*a1+b1*b1+a2*a2+b2*b2+...+am*am+bm*bm)%大质数10000000019的值。


本题注意一点,由于10000000019很大,就是最后求出来的ak与ak在相乘的时候注意一下,比如ak大于10^10时,直接相乘

就会爆LL,我们这里有技巧,就是把乘法变为加法,二分加法,也就是下面的multi函数。


通过本题学到了一招,就是对于求x*x%MOD怎么处理,x与MOD大于10^10。


[cpp]? view plain copy
  1. #include?<stdio.h>??
  2. #include?<stdlib.h>??
  3. #include?<string.h>??
  4. #include?<algorithm>??
  5. #include?<iostream>??
  6. #include?<math.h>??
  7. ??
  8. using?namespace?std;??
  9. typedef?unsigned?long?long?LL;??
  10. ??
  11. const?LL?Times=10;??
  12. const?LL?N=555;??
  13. const?LL?MOD=10000000018;??
  14. LL?ct,cnt,c,n;??
  15. LL?fac[N],num[N];??
  16. LL?arr[N];??
  17. LL?gcd(LL?a,LL?b)??
  18. {??
  19. ????return?b??gcd(b,a%b):a;??
  20. }??
  21. LL?multi(LL?a,LL?b,LL?m)??
  22. {??
  23. ????LL?ans=0;??
  24. while(b)??
  25. ????{??
  26. ????????if(b&1)??
  27. ????????{??
  28. ????????????ans=(ans+a)%m;??
  29. ????????????b--;??
  30. ????????}??
  31. ????????b>>=1;??
  32. ????????a=(a+a)%m;??
  33. ????}??
  34. return?ans;??
  35. LL?quick_mod(LL?a,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????LL?ans=1;??
  36. ????a%=m;??
  37. ????while(b)??
  38. ????{??
  39. ????????if(b&1)??
  40. ????????{??
  41. ????????????ans=multi(ans,a,m);??
  42. ????????????b--;??
  43. ????????}??
  44. ????????b>>=1;??
  45. ????????a=multi(a,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????}??
  46. return?ans;??
  47. }??
  48. bool?Miller_Rabin(LL?n)??
  49. if(n==2)?return?true;??
  50. if(n<2||!(n&1))?false;??
  51. ????LL?a,m=n-1,x,y;??
  52. ????int?k=0;??
  53. while((m&1)==0)??
  54. ????????k++;??
  55. ????????m>>=1;??
  56. for(LL?i=0;?i<Times;?i++)??
  57. ????????a=rand()%(n-1)+1;??
  58. ????????x=quick_mod(a,m,n);??
  59. for(LL?j=0;?j<k;?j++)??
  60. ????????????y=multi(x,n);??
  61. ????????????if(y==1&&x!=1&&x!=n-1)?false;??
  62. ????????????x=y;??
  63. if(y!=1)?true;??
  64. LL?Pollard_rho(LL?n,LL?c)??
  65. ????LL?x,y,d,i=1,k=2;??
  66. ????y=x=rand()%(n-1)+1;??
  67. while(true)??
  68. ????????i++;??
  69. ????????x=(multi(x,n)+c)%n;??
  70. ????????d=gcd((y-x+n)%n,153); background-color:inherit; font-weight:bold">if(1<d&&d<n)?return?d;??
  71. if(y==x)?return?n;??
  72. if(i==k)??
  73. ????????????y=x;??
  74. ????????????k<<=1;??
  75. void?find(LL?n,LL?c)??
  76. if(n==1)?return;??
  77. if(Miller_Rabin(n))??
  78. ????????fac[ct++]=n;??
  79. return?;??
  80. ????LL?p=n;??
  81. ????LL?k=c;??
  82. while(p>=n)?p=Pollard_rho(p,c--);??
  83. ????find(p,k);??
  84. ????find(n/p,k);??
  85. void?dfs(LL?dept,LL?product=1)??
  86. if(dept==cnt)??
  87. if(product<=(LL)sqrt(1.0*n)&&(product+n/product)%2==0&&(n/product-product)%2==0)??
  88. ????????????arr[c++]=product;??
  89. return;??
  90. for(LL?i=0;?i<=num[dept];?i++)??
  91. ????????dfs(dept+1,product);??
  92. ????????product*=fac[dept];??
  93. int?main()??
  94. ????LL?t,tt=1;??
  95. ????LL?ans,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????cin>>t;??
  96. while(t--)??
  97. ????????cin>>n;??
  98. ????????printf("Case?%d:?",tt++);??
  99. if(n==1)??
  100. ????????????puts("1");??
  101. continue;??
  102. ????????ct=0;??
  103. ????????c=0;??
  104. ????????find(n,120);??
  105. ????????sort(fac,fac+ct);??
  106. ????????num[0]=1;??
  107. ????????LL?k=1;??
  108. for(LL?i=1;?i<ct;?i++)??
  109. if(fac[i]==fac[i-1])??
  110. ????????????????++num[k-1];??
  111. else??
  112. ????????????{??
  113. ????????????????num[k]=1;??
  114. ????????????????fac[k++]=fac[i];??
  115. ????????????}??
  116. ????????cnt=k;??
  117. ????????dfs(0,1);??
  118. ????????sort(arr,arr+c);??
  119. ????????ans=0;??
  120. for(LL?i=0;?i<c;?i++)??
  121. ????????????x=(n/arr[i]+arr[i])/2;??
  122. ????????????y=(n/arr[i]-arr[i])/2;??
  123. ????????????ans+=multi(x,MOD)+multi(y,MOD);??
  124. ????????????ans%=MOD;??
  125. if(c>0)?cout<<quick_mod(n,ans,MOD+1)<<endl;??
  126. else????puts("-1");??
  127. return?0;??
  128. } ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读