leetcode 152 Zhou race

1. The prime arrange
for you to help give from 1 to n number of design arrangement plan, so that all the "prime number" should be placed in "prime index" on the (index start at 1); you need to return the total number of possible solutions.
Let's take a look at "prime": prime number must be greater than 1, and can not be less than the product of its two positive integers to represent.
Since the answer may be very large, so please return the answer mode mod can result after 10 ^ 9 + 7.
1 <= n <= 100
Links: https://leetcode-cn.com/contest/weekly-contest-152/problems/prime-arrangements/

Thinking: This question is investigated prime sieve, so that the subject can be analyzed further demand is the product of the number of non-aligned and aligned prime number of prime numbers.

class Solution {
public:
    int mod=1e9+7;
    vector<bool> st;
    vector<int> primers;
    int numPrimeArrangements(int n) {
        st=vector<bool>(n+1,false);
        int num=get_primers(n);
        long long int ans=1;
        long long int c1=1;
        long long int c2=1;
        for(int i=1;i<=num;++i){
            c1=1ll*c1*i%mod;
        }
        for(int i=1;i<=n-num;++i){
            c2=1ll*c2*i%mod;
        }
        ans=1ll*c1*c2%mod;
        return ans;
    }
    int get_primers(int n){
        for(int i=2;i<=n;++i){
            if(!st[i])primers.push_back(i);
            for(int j=0;j<primers.size()&&primers[j]<=n/i;++j){
                st[primers[j]*i]=true;
                if(i%primers[j]==0)
                    break;
            }
        }
        return primers.size();
    }
};

Guess you like

Origin www.cnblogs.com/clear-love/p/11447818.html