当前位置:网站首页>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':' ');
}
}
边栏推荐
- Number of 1 in binary (simple difficulty)
- sql语句模糊查询遇到的问题
- @RequestMapping
- Learning practice: comprehensive application of cycle and branch structure (I)
- Introduction to message queuing (MQ)
- Notes | numpy-09 Broadcast
- Why does I start with =1? How does this code work?
- Crazy scientist
- Market status and development prospect prediction of the global forward fluorescent microscope industry in 2022
- I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time
猜你喜欢

Pyqt control part (II)

Web security - CSRF (token)

Thesis reading_ Tsinghua Ernie
![[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa](/img/a9/92059db74ccde03b84c69dfce35b37.jpg)
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa

stm32逆向入门

The reason why the entity class in the database is changed into hump naming

The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping

Leetcode simple question: check whether two string arrays are equal

论文阅读_中文NLP_ELECTRA

Triangular rasterization
随机推荐
Introduction to message queuing (MQ)
雇佣收银员(差分约束)
逆袭大学生的职业规划
Learn to use the idea breakpoint debugging tool
Analysis of proxy usage of ES6 new feature
The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
Preparation for school and professional cognition
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
《牛客刷verilog》Part II Verilog进阶挑战
Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
Sdl2 + OpenGL glsl practice (Continued)
Games101 Lesson 9 shading 3 Notes
[set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
Number of 1 in binary (simple difficulty)
stm32逆向入门
Summary of training competition (Lao Li's collection of questions)
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
Thesis reading_ Tsinghua Ernie