2018 ACM-ICPC Chinese University student Program design Competition online B. goldbach-Millerabine Prime number Determination (large prime)

Source: Internet
Author: User

A number of years ago, when the problem can be written or super happy, although it is a board problem. Always forget to write a blog, memo.

Miller's big Prime, about what Millerabine is, the portal. Learn about: biubiubiu~

B. Goldbach

Topic Portal

See test instructions yourself, directly affixed to the code.

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <cstdlib>6#include <algorithm>7#include <queue>8#include <vector>9#include <stack>Ten using namespacestd; Onetypedef unsignedLong Longll; A Const intmaxn=1e9+Ten; - Const Doubleeps=1e-7; -ll Add_mod (ll a,ll b,ll MoD)//fast multiplication based on the theory of two points of fast power the { -ll ans=0;//due to the large and fast power overflow of the modulus -      while(b)//You must use this method -     { +         if(b&1)//I'm writing a non-recursive version here. -ans= (ans+a)%MoD; +a=a*2%MoD; Ab>>=1; at     } -     returnans; - } -  -ll Pow_mod (ll a,ll n,ll MoD)//Rapid Power Recursive version - { in     if(n>1) -     { toll Tmp=pow_mod (a,n>>1, MoD)%MoD; +tmp=Add_mod (tmp,tmp,mod); -         if(n&1) tmp=Add_mod (tmp,a,mod); the         returntmp; *     } $     returnA;Panax Notoginseng } -  the BOOLMiller_rabbin (ll n,ll a)//Millerabine prime judgment function body + { All d=n-1, s=0, I; the      while(! (d&1))//first calculate the (2^s) *d. +     { -d>>=1; $s++; $     } -ll T=pow_mod (a,d,n);//A^d take a time to judge -     if(t==1|| t==-1)//one or minus a can declare that this could be prime the         return 1; -      for(i=0; i<s; i++)//if not, continue to multiply the S 2 .Wuyi     { the         if(t==n-1)//(n-1) * (n-1)%n=1 This step is to optimize -             return 1; WuT=add_mod (T,t,n);//Quick Ride -     } About     return 0; $ } -  - intIs_prime (ll N) - { All i,tab[4]= {3,4,7, One};//any integer in [1,n] should have been taken +      for(i=0; i<4; i++)//but generally these few numbers are enough to not need too many group tests the     { -         if(n==Tab[i]) $             return 1;//Small Judgment Small optimization ~ the         if(!n%Tab[i]) the             return 0; the         if(N>tab[i] &&!Miller_rabbin (N,tab[i])) the             return 0; -     } in     return 1; the } the ll Max (ll A,ll b) About { the     returnA>b?a:b; the } the intMain () + { -     intT; thescanf"%d",&t);Bayi      while(t--) the     { the ll N; -scanf"%llu",&n); -         if(n==4) theprintf"2 2\n"); the         Else the         { the              for(LL i=2; i<=n/2; ++i) -             { the                 if(Is_prime (i) &&is_prime (ni)) the                 { theprintf"%llu%llu\n", i,n-i);94                      Break; the                 } the             } the         }98     } About}

At that time, my teammates helped me with the D bug, and he ignored me Now ((?_?))

Uncomfortable

2018 ACM-ICPC Chinese University student Program design Competition online B. goldbach-Millerabine Prime number Determination (large prime)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.