Euler sieve of number theory, multiplicative inverse element, fast power combat

Southwest University of Science and Technology Question E twin primes (investigation points: Euler sieve, multiplicative inverse element, fast power or extended Euler)

Preface

I just saw the multiplication inverse of the fancier in the morning. The afternoon question happened to be tested, but I still got stuck and didn't write it out. Summary template + template question, Euler sieve template, multiplication inverse element template, fast power template. Being a cv engineer can be ac, but unfortunately I don't deserve it, I have been wawawa.
Do not work hard before the game, wow wow during the game.

A topic description

Insert picture description here
Insert picture description here
Topic link: link .

Two problem-solving ideas

1. Euler sieve (find the number of twin primes m)
2. Multiplicative inverse element (finally used to find the value of the fraction m/n mod 1e9+7, because the fraction cannot be directly modulated) The
multiplicative inverse element formula has been given.
Insert picture description here
3 fast powers (used to solve the mod-2 power problem of Q above).
? ? ? Gone? ? ? Is this all gone?
It seems to be gone, but there is one sentence left. How to calculate the probability that two of n are twin primes?
No one won't know this, right? I won't, by coincidence.

Three problem solution code



#include<bits/stdc++.h>
using namespace std;

const int maxx = 1e7+10;
int prime[maxx+10];//筛素数的数组
int a[maxx+10];//n个数中有几个孪生素数
bool vis[maxx+10];//标记数组,0是i对应是素数,1是非素。

const int mod=1e9+7;//要模的值
typedef long long ll;
void init()       //欧拉筛+判断n中有几个孪生素数的打表。
{
    
    
    vis[0]=vis[1]=1;
    int k=0;
    for(int i=2;i<=maxx;i++){
    
    
    	
        if(vis[i]==0)prime[++k]=i;
        for(int j=1;i*prime[j]<=maxx;j++){
    
    
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
        
    }
    for (ll i = 0; i <= 10000000; ++i) {
    
    
        a[i] = a[i - 1];
        if (!vis[i] && !vis[i - 2]) {
    
    
            ++a[i];
        }
      
    }
    
}


ll ksm(ll a ,ll k)     //快速幂
{
    
    
    ll rec=1;
    while(k)
    {
    
    
        if (k&1)
            rec=((rec%mod)*(a%mod))%mod;
        a=((a%mod)*(a%mod))%mod;
        k>>=1;
    }
    return rec%mod;
}


int main()
{
    
    
    ll n;
    int t;
    cin>>t;
    init();
    
    while(t--)
    {
    
    
        cin>>n;
    	 
    	 
    	
    	  //cout<<bj[n]<<"   ";
    cout<<(a[n])*ksm(n*(n-1)/2,mod-2)%mod<<endl;//我们从1∼N中不放回地抽取两个数,抽到的是孪生素数的概率.
	}
   
    
    return 0;
}

Four summary

As a number theory player, I personally feel that these commonly used templates are clear to my heart. Although templates can be brought into the game, it is better to be skilled in typing them out. After all, cv engineers are not very good.

Five daily encouragement

After all the hard work is still strong, let him wind from the southeast to the northwest.

Guess you like

Origin blog.csdn.net/qq_40826583/article/details/115362033