Modulo operation
(ab)mod m=(a mod m)(b mod m) mod m
1. Question statistics
a,b,n=map(int,input().split())
res=a*5+b*2
m=n//res*7
n=n%res
t=0
while n>0:
if t<5:
n-=a
m+=1
else:
n-=b
m+=1
print(m)
Two, fast power
b,p,k=map(int,input().split())
def quick_mi(b,p,k):
n=p
b%=k
ans=1
while n:
if n&1==1:
ans=(ans*b)%k
b=(b*b)%k
n>>=1
return ans
print(quick_mi(b,p,k))
3. RSA decryption
First use the following code to solve p, q
import math
n=1001733993063167141
k=int(math.sqrt(n))
for i in range(2,k+1):
if n%i==0:
print(i,n//i)
891234941 1123984201
The second step is to convert the formula of de%((p-1)(q-1))
n=1001733993063167141
d=212353
p=891234941
q=1123984201
tmp=(p-1)*(q-1)
print(tmp)
for i in range(2,n+1):
now=i*tmp+1
if now%d==0:
print(now//d)
break
1001733991047948000
823816093931522017
The third step is to use the fast power code to solve x=c^e mod n
n=1001733993063167141
e=823816093931522017
c=20190324
def fastpow(a,b,mod):
ans=1
while b:
if b&1:
ans=ans*a%mod
a=a*a%mod
b>>=1
return ans
print(fastpow(c,e,n))
579706994112328949
GCD
def gcd(a,b):
if b==0:
return a
return gcd(b,a%b)
def gcd(a,b):
return a if b==0 else gcd(b,a%b)
from math import gcd
LCM
Least common multiple
LCM=a*b/gcd(a,b)
def lcm(a,b):
return a*b//gcd(a,b)
4. The number of walnuts (least common multiple)
This problem is to find the least common multiple of a, b, c
from math import *
def lcm(a,b):
return a*b//gcd(a,b)
a,b,c=map(int,input().split())
ans=lcm(lcm(a,b),c)
print(ans)
Five, Hankson's fun questions
Passed 80%, the last one timed out, and did not find the python code of ac, if there is, please post it in the comment group!
import math
from math import gcd
def lcm(a,b):
return a*b//gcd(a,b)
n=int(input())
for i in range(n):
a0,a1,b0,b1=map(int,input().split())
ans=0
for x in range(1,int(math.sqrt(b1)+1)):
if b1%x==0:
if gcd(x,a0)==a1 and lcm(x,b0)==b1:
ans+=1
y=b1//x
if x==y:continue
if gcd(y,a0)==a1 and lcm(y,b0)==b1:
ans+=1
print(ans)
6. Find integers
I saw that other people's writing methods were looking for patterns before, which felt very complicated, but it felt like they were always looking for their least common multiple, and then the accumulated step size was very clever!
from math import *
mod=[0,0,1,2,1,4,5,4,1,2,9,0,5,10,11,14,9,0,11,18,9,11,11,15,17,9,23,20,25,16,29,27,25,11,17,4,29,22,37,23,9,1,11,11,33,29,15,5,41,46]
ans=2+mod[2]
k=2
for i in range(3,50):
while True:
if ans%i==mod[i]:
k=lcm(k,int(i))
break
else:ans+=k#一直加他们的最小公倍数,一直到满足符合是其倍数
print(ans)
print(2022040920220409)
prime number
7. Stupid kid
import math
def check(u):
if u<=1:
return False
for i in range(2,int(math.sqrt(u))+1):
if u%i==0:
return False
return True
letter=[0]*26
s=input()
for i in range(len(s)):
if "A"<=s[i]<"Z":
letter[ord(s[i]) - ord('A')] += 1
else:letter[ord(s[i])-ord('a')]+=1
letter.sort()
maxx=letter[-1]
for i in letter:
if i==0:
continue
minn=i
break
# print(maxx,minn)
if check(maxx-minn):
print("Lucky Word")
print(maxx-minn)
else:
print("No Answer")
print("0")
8. Prime numbers
N=10**6
primes=[]
bprime=[False]*N
def getPrimes(n):
global primes
global cnt
bprime[0]=True
bprime[1]=True
for i in range(2,n+1):
if not bprime[i]:
primes.append(i)
cnt+=1
for j in range(i*2,n+1,i):
bprime[j]=True
n=int(input())
cnt=0
getPrimes(n-1)
for p in primes:
print(p,end=' ')
print()
print(cnt)