Fast prime factor decomposition and primality testing & ABC142D

First, the standard integer decomposition is very clear that it is easy to see:

 

 

 Generally, we should be broken down into a number like this we can write:

 1 #include<cstdio>
 2 int p[105],w[105],k;
 3 void factorize(int n)
 4 {
 5     for(int i=2;i*i<=n;i++)
 6         if(n%i==0)
 7         {
 8             p[++k]=i;
 9             while(n%i==0)
10                 n/=i,w[k]++;
11         }
12     if(n!=1)
13         p[++k]=n,w[k]=1;
14 }
15 int main()
16 {
17     int n;
18     scanf("%d",&n);
19     factorize(n);
20     for(int i=1;i<k;i++)
21         printf("%d^%d*",p[i],w[i]);
22     printf("%d^%d",p[k],w[k]);
23 }

Since decomposition is a prime number, and in addition is an odd prime number 2, it is possible to enumerate at each time i, i + = 2

Example: ABC142D (do not have any good questions, just do it because of the recent 2333)

Looking coprime common factor, it is equivalent to the greatest common divisor to find the most prime factor. (This statement ... I think you can understand

 

T has been written before, and he looked for another way, where there are problems before only to find later

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<map>
 4 using namespace std;
 5 #define N 100005
 6 #define ll long long
 7 #define MOD 1000000007
 8 ll x,y;
 9 map<ll,bool> vis;
10 ll p[N];
11 int pn;
12 ll gcd(ll a,ll b)
13 {
14     if(b==0) return a;
15     else return gcd(b,a%b);
16 }
17 int main()
18 {
19     scanf("%lld %lld",&x,&y);
20     ll d=gcd(x,y);
21     int ans=1;
22     if(d%2==0) ans++;
23     while(d%2==0)
24         d/=2;
25     for(ll i=3;i*i<=d;i+=2)//Written i <= d / i can not open or do not open ll ll explosion will take off and T 
26 is          IF (D% I == 0 )
 27          {
 28              ANS ++ ;
 29              the while (I% D == 0 )
 30                  D / = I;
 31 is          }
 32      IF (! D = . 1 ) ANS ++ ;
 33 is      the printf ( " % D \ n- " , ANS);
 34 is      return  0 ;
 35 }

My comment is that place, pay attention to that kind of a BUG

Finally, the use of this wording:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<map>
 4 #include<vector>
 5 #include<cmath>
 6 using namespace std;
 7 #define N 100005
 8 #define ll long long
 9 #define MOD 1000000007
10 ll x,y;
11 map<ll,bool> vis;
12 ll p[N];
13 int pn;
14 vector<ll>st;
15 ll gcd(ll a,ll b)
16 {
17     if(b==0) return a;
18     else return gcd(b,a%b);
19 }
20 bool is_prime(ll x)
21 {
22     if(x==1) return 0;
23     if(x==2||x==3) return 1;
24     if(x%6!=1&&x%6!=5) return 0;
25     int s=sqrt(x);
26     for(int i=5;i<=s;i+=6)
27         if(x%i==0||x%(i+2)==0)
28             return 0;
29     return 1;
30 }
31 int solve(ll n)
32 {
33     int ans=1;
34     if(n==1) return 1;
35     ll i=0;
36     while(i<n)
37     {
38         if(is_prime(n))
39         {
40             st.push_back(n);
41             if(vis[n]==0)ans++;
42             vis[n]=1;
43             return ans;
44         }
45         for(int i=2;i<n;i++)
46         {
47             if(n%i==0)
48             {
49                 st.push_back(i);
50                 if(vis[i]==0)ans++;
51                 vis[i]=1;
52                 n/=i;
53                 break;
54             }
55         }
56     }
57     st.push_back(n);
58     if(vis[n]==0)ans++;
59     vis[n]=1;
60     return ans;
61 }
62 int main()
63 {
64     scanf("%lld %lld",&x,&y);
65     ll d=gcd(x,y);
66     printf("%d\n",solve(d));
67     return 0;
68 }

In fact, almost feeling and above approach, see also able to understand directly, but the Internet is said to be $ n ^ {1/4} $ of complexity, it is still to find out, there is no harm. (This question is in fact do not need to deposit factor of it)

About primes judgment is_prime, or talk about it.

If you do not add this judgment, it becomes a large prime number in $ n $ times, this algorithm will be degraded to $ O (n) $ level.

 

For each $> = $ 5 can be expressed as the number of $ 6x-1 $ (also equivalent to $ 6x + 5 $), $ 6x $, $ 6x + 1 $, $ 6x + 2 $, $ 6x + 3 $ , $ 6x + 4 $, $ 6x + 5 $ of one.

And $ 6x $, $ 6x + 2 = 2 (3x + 1) $, $ 6x + 3 = 3 (x + 1) $, $ 6x + 4 = 2 (3x + 2) $, can not prime.

So we have for a number n, the direct first determine whether it die more than $ 6 $ 5 $ 1 $ $ $, or more than, if not the direct return false

But that is the case is not necessarily a prime number, still have to judge for themselves.

Each number can be the prime factorisation, so long as we use it in addition to determination of whether the front of the prime number divisible by it.

$ 6x + 1 $, $ 6x + 5 $ this number is obviously impossible to do in addition to the $ 2 $ and $ 3 $, so we judge from $ 5 $ begin.

$ 7 $ next divided, as discussed above, the next and one of $ 11 $ 13 $ $.

Some say the Internet, and so on, can be increased to $ 6 $ step to run faster.

I do not know how to prove, but verify moment is right. (How it feels good water ...)

 


 

 

Inexplicably finished the job this blog.

Water it well written.

Trained, trained, trained, what I'm doing.

Guess you like

Origin www.cnblogs.com/lyttt/p/11621727.html