省赛第二期模拟赛题单
第一题:求余
- 解题思路:语法
- python代码如下
print(2**2023%1000)
第二题:灌水
- 解题思路:bfs,dfs
- python代码如下:
# bfs
import os
import sys
#很简单的一道bfs
data="""
0000100010000001101010101001001100000011
0101111001111101110111100000101010011111
1000010000011101010110000000001011010100
0110101010110000000101100100000101001001
0000011010100000111111001101100010101001
0110000110000000110100000000010010100011
0100110010000110000000100010000101110000
0010011010100110001111001101100110100010
1111000111101000001110010001001011101101
0011110100011000000001101001101110100001
0000000101011000010011111001010011011100
0000100000011001000100101000111011101100
0010110000001000001010100011000010100011
0110110000100011011010011010001101011011
0000100100000001010000101100000000000010
0011001000001000000010011001100101000110
1110101000011000000100011001001100111010
0000100100111000001101001000001010010001
0100010010000110100001100000110111110101
1000001001100010011001111101011001110001
0000000010100101000000111100110010101101
0010110101001100000100000010000010110011
0000011101001001000111011000100111010100
0010001100100000011000101011000000010101
1001111010010110011010101110000000101110
0110011101000010100001000101001001100010
1101000000010010011001000100110010000101
1001100010100010000100000101111111111100
1001011010101100001000000011000110110000
0011000100011000010111101000101110110001
"""
dx=[-1,0,1,0]
dy=[0,1,0,-1]
def bfs(x,y):
global hh,tt
st[x][y]=1
data[x][y]="2"
q[0]=[x,y]
while hh<=tt:
head=q[hh]
hh+=1
#扩展四个方向
for i in range(4):
new_x=dx[i]+head[0]
new_y=dy[i]+head[1]
#判断是否 合法
if new_x>=30 or new_x<0 or new_y>=40 or new_y<0:
continue
if st[new_x][new_y]:
continue
if data[new_x][new_y]!="0":
continue
#满足条件的可以入队
st[new_x][new_y]=1
data[new_x][new_y]="2"
tt+=1
q[tt]=[new_x,new_y]
#处理数据 处理成二维矩阵的形式
data=list(map(list,data.split()))
q=[0]*(30*45)
st=[[0]*45 for i in range(35)]
hh,tt=0,0
bfs(0,0)
ans=0
for i in range(30):
for j in range(40):
if data[i][j]=="2":
ans+=1
print(ans)
# dfs
data = """
0000100010000001101010101001001100000011
0101111001111101110111100000101010011111
1000010000011101010110000000001011010100
0110101010110000000101100100000101001001
0000011010100000111111001101100010101001
0110000110000000110100000000010010100011
0100110010000110000000100010000101110000
0010011010100110001111001101100110100010
1111000111101000001110010001001011101101
0011110100011000000001101001101110100001
0000000101011000010011111001010011011100
0000100000011001000100101000111011101100
0010110000001000001010100011000010100011
0110110000100011011010011010001101011011
0000100100000001010000101100000000000010
0011001000001000000010011001100101000110
1110101000011000000100011001001100111010
0000100100111000001101001000001010010001
0100010010000110100001100000110111110101
1000001001100010011001111101011001110001
0000000010100101000000111100110010101101
0010110101001100000100000010000010110011
0000011101001001000111011000100111010100
0010001100100000011000101011000000010101
1001111010010110011010101110000000101110
0110011101000010100001000101001001100010
1101000000010010011001000100110010000101
1001100010100010000100000101111111111100
1001011010101100001000000011000110110000
0011000100011000010111101000101110110001
"""
count=0
def dfs(x,y):
global count
count+=1
st[x][y]=1
data[x][y]=="2"
for i in range(4):
new_x=x+dx[i]
new_y=y+dy[i]
if new_x>=30 or new_x <0 or new_y>=40 or new_y<0:
continue
if st[new_x][new_y]:
continue
if data[new_x][new_y]!="0":
continue
dfs(new_x,new_y)
dx=[-1,0,1,0]
dy=[0,1,0,-1]
data=list(map(list,data.split()))
st=[[0]*50 for i in range(40)]
dfs(0,0)
print(count)
第三题:区间最大和
- 解题思路:一维前缀和
- python代码如下
import os
import sys
# 一维前缀和
n,k=map(int,input().split())
num_list=list(map(int,input().split()))
num_list.insert(0,0)
for i in range(1,n+1):
num_list[i]+=num_list[i-1]
max_num=0
i=0
while i+k<=n:
max_num=max(max_num,num_list[i+k]-num_list[i])
i+=1
print(max_num)
第四题:循环位移
- 解题思路:语法
- python代码如下
x=input()
print(x[1:]+x[0])
第五题:最多约束
- 解题思路:语法
- python代码如下
import os
import sys
import math
data="""393353 901440 123481 850930 423154 240461
373746 232926 396677 486579 744860 468782
941389 777714 992588 343292 385198 876426
483857 241899 544851 647930 772403 109929
882745 372491 877710 340000 659788 658675
296521 491295 609764 718967 842000 670302
"""
data=data.split("\n")
data=[list(map(int,i.split())) for i in data]
data=[i for j in data for i in j]
def yueshu(x):
ans=0
for i in range(1,int(math.sqrt(x))+1):
if x%i==0:
ans+=1
if x//i!=i:
ans+=1
return ans
max_yueshu=0
max_num=0
for i in data:
if max_yueshu<=yueshu(i):
max_yueshu=yueshu(i)
max_num=i
print(max_num)
第六题:字符显示
- 解题思路:语法
- python代码如下
print(36*30//10)
第七题:最后的元音
- 解题思路:语法
- python代码如下
sets=set(["a","e","i","o","u"])
strings=input()
for i in strings[::-1]:
if i in sets:
print(i)
break
第八题:最大落差
- 解题思路:语法
- python代码如下:
import os
import sys
# 请在此输入您的代码
n=int(input())
num_list=list(map(int,input().split()))
max_luocha=0
i=1
while i<n:
if num_list[i]<num_list[i-1]:
max_luocha=max(max_luocha,num_list[i-1]-num_list[i])
i+=1
print(max_luocha)
第九题:数位和相等
[原题链接](https://www.lanqiao.cn/problems/7936/learning/?problem_list_id=25&page=1)
- 解题思路:语法
- python代码如下
import os
import sys
# 请在此输入您的代码
def transfer(x,jinzhi):
ans=[]
while x:
ans.append(x%jinzhi)
x//=jinzhi
return sum(ans)
count=0
i=1
while True:
if transfer(i,2)==transfer(i,8):
count+=1
if count==23:
print(i)
break
i+=1
第十题:转换次数
- 解题思路:语法
- python代码如下
import os
import sys
# 请在此输入您的代码
def transfer(x):
res=1
for i in str(x):
if i!="0":
res*=int(i)
return res
num=int(input())
while num>=10:
num=transfer(num)
print(num)
第十一题:公约移动
- 解题思路:bfs,dfs
- 如果用dfs做的话,要重新设置一下递归深度,要不然会出现段错误
- python代码如下:
#dfs
import sys
# 获取默认的递归深度
default_recursion_limit = sys.getrecursionlimit()
new_recursion_limit = 100000
sys.setrecursionlimit(new_recursion_limit)
#移动位数dfs 版本
N,M=1005,1005
matrix=[[0]*N for i in range(M)]
st=[[0]*N for i in range(M)]
count=0
dx=[-1,0,1,0]
dy=[0,1,0,-1]
def gcd(a,b):
if b==0:
return a
return gcd(b,a%b)
def dfs(x,y):
#一条路直接搜到底
global count
count+=1
st[x][y]=1
#扩展
for i in range(4):
new_x=x+dx[i]
new_y=y+dy[i]
#开始进行判断
if new_x>n or new_x<1 or new_y>m or new_y<1:
continue
if st[new_x][new_y]:
continue
if gcd(matrix[new_x][new_y],matrix[x][y])<=1:
continue
#剩下来的就是可以进行扩展的
dfs(new_x,new_y)
n,m=map(int,input().strip().split())
for i in range(1,n+1):
temp=list(map(int,input().strip().split()))
for j in range(m):
matrix[i][j+1]=temp[j]
#处理完成
r,c=map(int,input().strip().split())
dfs(r,c)
print(count)
# bfs
n,m=1005,1005
matrix=[[0]*n for i in range(m)]
st=[[0]*n for i in range(m)]
q=[0]*(n*m)
hh,tt=0,0
count=0
dx=[-1,0,1,0]
dy=[0,1,0,-1]
def gcd(a,b):
if b==0:
return a
return gcd(b,a%b)
def bfs(x,y):
global count,hh,tt
#入队之前进行的操作
count+=1
st[x][y]=1
q[hh]=[x,y]
while hh<=tt:
head=q[hh]
hh+=1
#扩展四个方向
for i in range(4):
new_x=head[0]+dx[i]
new_y=head[1]+dy[i]
#判断扩展的方向死否合法
if new_x>n or new_x<1 or new_y>m or new_y<1:
continue
if st[new_x][new_y]:
continue
if gcd(matrix[new_x][new_y],matrix[head[0]][head[1]])<=1:
continue
#以下是可以合法的扩展
#那么就要进行入队列操作
count+=1
st[new_x][new_y]=1
tt+=1
q[tt]=[new_x,new_y]
return count
n,m=map(int,input().split())
for i in range(1,n+1):
temp=list(map(int,input().split()))
for j in range(m):
matrix[i][j+1]=temp[j]
#构建完成,开始bfs
#输入开始位置的坐标
r,c=map(int,input().split())
print(bfs(r,c))