当前位置:网站首页>2022-02-12 daily clock in: problem fine brush
2022-02-12 daily clock in: problem fine brush
2022-07-03 04:53:00 【Can__ er】
2022-02-12 Clock in every day : Problem fine brush
Write it at the front
“ After these things are skilled , Maybe it's as plain as drinking water , But it can bring great happiness to beginners , I always thought , Can you always keep your enthusiasm as a beginner 、 focus , Determines how far you can go when you do something , How good can you do .” This series of articles is written by python To write , There are three sources of topics : Not done before ;Leetcode secondary , Difficult and difficult questions ; Week title ; The classic topic of a topic , All codes have been AC. everyday 1-3 Avenue , Random analysis , I hope rain or shine , As a record of encouraging yourself to brush questions .
204. Count prime
Enumeration no longer gives , The biggest problem is that the correlation between numbers is not taken into account .
I want to learn something here 【 Egyptian sieve ( Eradorse sieve method )】:
- consider x Prime number , Then its multiple 2x,3x… It must not be a prime number .
- Can be directly from x⋅x Start marker , because 2x,3x… These numbers must be in x Previously marked by multiples of other numbers , for example 2 All multiples of ,3 All multiples of .
class Solution:
def countPrimes(self, n: int) -> int:
# from 2 Start checking , It was found that n-1( Less than or equal to... Is detected n 了 )
isprime = [1 for i in range(0,n)]
if len(isprime)<=1:
return 0
isprime[0]=isprime[1]=0
for i in range(2,n):
if isprime[i]:
for j in range(i*i,n,i):
isprime[j]=0
return sum(isprime)
【 Linear sieve 】 Our approach is more advanced , In order to eliminate redundant operations in the Egyptian sieve :
- such as 45 Will be 3 and 5 Both numbers are marked at the same time , We expect O ( n ) O(n) O(n) Complexity , That is, each number is determined only once . according to 《 Basic theorem of arithmetic 》:【 Every composite number can be uniquely written as the product of prime numbers 】. In other words , The product of multiple prime numbers can only form a unique composite number .
- No longer mark x All multiples of , Only the numbers in the prime number set and x The number multiplied by .
- For the problem of multiplying multiple prime numbers , It's not just a compound mark for prime numbers , But for each number , for example 8 = 4 ∗ 2 8=4*2 8=4∗2 in 4 Can't be ignored .
- Mark to... Each time x m o d p r i m e s i = = 0 x \mod primes_i==0 xmodprimesi==0, Because if x You can divide a prime number , So for composite numbers x ∗ p r i m e s i = x / p r i m e s i ∗ p r i m e s i + 1 x * primes_i = x/primes_i * primes_{i+1} x∗primesi=x/primesi∗primesi+1, That is, there must be a larger number behind it so that it can be marked . Another blogger said in his introduction that the purpose is 【 Sift him out with the smallest quality factor 】.
class Solution:
def countPrimes(self, n: int) -> int:
# from 2 Start checking , It was found that n-1( Less than or equal to... Is detected n 了 )
isprime = [1 for i in range(0, n)]
if len(isprime) <= 1:
return 0
isprime[0] = isprime[1] = 0
primes = []
for i in range(2, n):
if isprime[i]:
primes.append(i)
for j in primes:
if j*i < n :
isprime[i*j] = 0
else:
break
# You must join before exiting
# 4 = 2*2
if i % j == 0:
break
return sum(isprime)
Smart Stefanie
The meaning of the question is to give a number S , Find the sum of divisors equals S All the numbers of .
Here's a theorem :
The first contact is difficult to understand , for instance : 360 = 2 3 × 3 2 × 5 360=2^3×3^2×5 360=23×32×5 , The sum of its divisors is ( 1 + 2 1 + 2 2 + 2 3 ) × ( 1 + 3 1 + 3 2 ) × ( 1 + 5 1 ) (1+2^1+2^2+2^3)×(1+3^1+3^2)×(1+5^1) (1+21+22+23)×(1+31+32)×(1+51).
We can enumerate ( Will timeout ) Method to check each number , It can also be decomposed by searching :
- If the current number can be expressed as a prime number that has not been searched and 1 And , Then the product of the number searched before and this prime number conforms to the meaning of the question .
- For every prime number that has not been searched and whose square is less than the current number , Then enumerate all that may conform to the meaning of the topic ai Perform a recursive search .
It's really not written , The code is This blogger's link :
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 100000
int p[N+5],cnt,s,ans,num[N+5];
bool flag[N+5];
void getprime()
{
for(int i=2;i<=N;i++)
{
if(!flag[i]) p[++cnt]=i;
for(int j=1; i*p[j]<=N; j++)
{
flag[i*p[j]]=1;
if(i%p[j]==0) break;
}
}
}
bool isprime(int x)
{
if(x==1) return false;
if(x<=N) return !flag[x];
for(int i=1;p[i]*p[i]<=x;i++)
if(x%p[i]==0) return false;
return true;
}
void dfs(int last,int now,int tot)
{
// now->summ, tot->left, last->pos
if(tot==1){
num[++ans]=now; return; }
if(tot-1>p[last]&&isprime(tot-1))
num[++ans]=now*(tot-1);
for(int i=last+1; p[i]*p[i]<=tot; i++)
for(int tnum=p[i]+1,t=p[i]; tnum<=tot; t*=p[i],tnum+=t)
if(tot%tnum==0)
dfs(i,now*t,tot/tnum);
}
int main()
{
getprime();
while(scanf("%d",&s)!=EOF)
{
ans=0;
dfs(0,1,s);
cout<<ans<<endl;
sort(num+1,num+ans+1);
for(int i=1; i<=ans; i++)
printf("%d%c",num[i],i==ans?'\n':' ');
}
}
边栏推荐
- 2022 P cylinder filling test content and P cylinder filling simulation test questions
- The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
- [luatos sensor] 2 air pressure bmp180
- [BMZCTF-pwn] 18-RCTF-2017-Recho
- Triangular rasterization
- Learn to use the idea breakpoint debugging tool
- Current market situation and development prospect forecast of the global fire boots industry in 2022
- Kept hot standby and haproxy
- [SQL injection point] location and judgment of the injection point
- C Primer Plus Chapter 10, question 14 3 × 5 array
猜你喜欢
Thesis reading_ ICD code_ MSMN
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
Review the configuration of vscode to develop golang
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
I stepped on a foundation pit today
Oracle SQL table data loss
移动端——uniapp开发记录(公共请求request封装)
Thesis reading_ Tsinghua Ernie
Source insight garbled code solution
论文阅读_中文NLP_ELECTRA
随机推荐
MySQL winter vacation self-study 2022 12 (3)
Notes | numpy-10 Iterative array
Shuttle + Alluxio 加速内存Shuffle起飞
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
Preparation for school and professional cognition
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
【SQL注入点】注入点出现位置、判断
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
The reason why the entity class in the database is changed into hump naming
Handler understands the record
Market status and development prospect prediction of global SoC Test Platform Industry in 2022
Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
Number of uniform strings of leetcode simple problem
Leetcode simple problem delete an element to strictly increment the array
[tools run SQL blind note]
Current market situation and development prospect forecast of the global fire boots industry in 2022
Market status and development prospect prediction of global fermentation acid industry in 2022
Day 51 - tree problem
Learn to use the idea breakpoint debugging tool