写在前面
- 包含本次模拟赛的10道题题解
- 能过样例,应该可以AC
- 若有错误,欢迎评论区指出
- 有疑问可私信我哈🫰🏻
从2023开始暴力枚举每一个数,直到找到正确答案
- start = 2022
-
- def check(num) :
- t = str(bin(num))
- if t[-6:] == '000000' : return True
- return False
-
- while 1 :
-
- start += 1
- if check(start) : print(start) ; break
-
使用Python自带的datetime库统计一共多少天
- import datetime
-
- start = datetime.date(1949,10,1)
- end = datetime.date(2022,1,1)
- diff = datetime.timedelta(1)
-
- ans = 0
-
- while start < end :
- ans += 1
- start += diff
-
- print(ans)
从10开始枚举直到找到正确答案
-
- start = 10
-
- def check(num) :
- t = int(str(num),16)
-
- if t % num == 0 : print(num,t) ; return True
- return False
-
- while 1 :
- if check(start) : print(start) ; break
- start += 1
动态规划,dp[i][j]代表走到第i行,第j列的最大值是多少,它是由左边的前一个元素dp[i][j-1]和上边的dp[i-1][j] 两者之间的最大值+原数组第i行,第j列的值相加 更新而来的。
-
- s = "174094882455171152761423221685761892795431233411387427793198\
- 650286024865090061389344606618496378829135984076361542097372\
- 601657541200146071777733599818266038012509478351201640618984\
- 143988087783837107349651099683484992553337438088068198972282\
- 890781586124258626539246182119762952003918195325258677229419\
- 698255491250839396799769357665825441616335532825361862146291\
- 503649293440596342887581257444442930778730382520372975343211\
- 325351222640703400531067500454956482168314849207060705673849\
- 265774579830223671554026061117300483012903885770893074783710\
- 083450145620356667677191627276513995926532444279237315785832\
- 411595106453089134746365281031552217482363035280722591085079\
- 053410485925413958279617719034175332412908745680774313630190\
- 429314820559328748143552689295945058801322270313370955837837\
- 939182801848609300876356583948397645861551964542532682663945\
- 625356614462682551015176002433628234343684739800880514363921\
- 982340231989891351425389287014819359798014755509282450440511\
- 590838726938103384801541373585690893606978941566666714061214\
- 952341523168827712604946036245881214982452998386986623826275\
- 782780208928205527678781609589000725521486468983551558405472\
- 149903035076783644195574734088152324666290493119955560594634\
- 905391288186024902215444250421277955403412298227858394469856\
- 607272647132163832860126054679347881638761723785858733108109\
- 249157334220127702410373959720286708183036202841837581704881\
- 367895556630088230650972282944827258473951902831431040790814\
- 079538232104075905120989173307660289899942087873076421916033\
- 622143260549608274076012938515668898707915863945382394851328\
- 164677964192631597026176253407553188801750590935427267220117\
- 591817866992665840378311257621611574856498432538327068011953\
- 631534031790352912617015229051836886166704989498756486878095\
- 690013558017746707412183571476823027885971347137127534455141"
-
- Map = [[0] * 60 for i in range(30)]
-
- for i in range(30) :
- for j in range(60) :
- Map[i][j] = int(s[i*60+j])
-
- dp = [[0] * 60 for i in range(30)]
-
- for i in range(30) :
- for j in range(60) :
- if i == 0 and j == 0 : dp[i][j] = Map[0][0]
- elif j == 0 : dp[i][j] = dp[i-1][j] + Map[i][j]
- elif i == 0 : dp[i][j] = dp[i][j-1] + Map[i][j]
- else : dp[i][j] = max(dp[i-1][j],dp[i][j-1]) + Map[i][j]
-
- print(dp[29][59])
将小于2022的质数都找出来(质数筛),之后枚举质数列表的每一项prime,看2022-prime是否在列表中
-
- def find(num) :
-
- prime = []
- flag = [True] * (num+1)
-
- for i in range(2,num+1) :
- if flag[i] : prime.append(i)
- j = 0
- while prime[j] <= num // i :
- flag[prime[j] * i] = False
- if i % prime[j] == 0 : break
- j += 1
-
- return prime
-
- prime = find(2022)
- ans = 0
-
- for i in prime :
- if 2022 - i in prime : ans += 1
-
- print(ans >> 1)
签到题 略
-
- t,c,s = map(int,input().split())
- print(s*t//c - t)
开一个集合s,读入每一个单词,若该单词不在集合中,则输出并加入集合中。若在集合中则跳过。
-
- N = int(input())
- s = set()
- for _ in range(N) :
- num = input()
- if num not in s :
- print(num)
- s.add(num)
-
将原字符串颠倒,看从第一个字符开始到第几个字符,以这两个字符为边界的字符串是回文串(至少为1,找最大长度) ,则剩下的字符(不属于回文串的,颠倒后+原字符串 就是答案,有点绕,看不懂可以私信我)
-
- s = input()
- leng = len(s)
-
- res = 0
- s = s[::-1]
-
- def check(s) :
- return s == s[::-1]
-
- if check(s) : res = leng
- else :
- for i in range(1,leng) :
- if check(s[:i]) : res = i
-
- t = leng - res
- s = s[::-1]
- tmp = s[:t]
- print(s + tmp[::-1])
枚举每一个不在边界上的点,看以它为中心,能贡献几个X图形,相加起来得到的和就是答案
-
- n,m = map(int,input().split())
- Map = []
- for i in range(n) : Map.append(list(input()))
-
- def bound(x,y) :
-
- return 0 <= x < n and 0 <= y < m
-
- def deep_check(t,a,b,c,d) :
-
- return t == a and t == b and t == c and t == d
-
- def check(x,y) :
-
- l = 0
- while 1 :
- l += 1
- ax = x + (-1) * l ; ay = y + (-1) * l
- bx = x + (-1) * l ; by = y + 1 * l
- cx = x + 1 * l ; cy = y + 1 * l
- dx = x + 1 * l ; dy = y + (-1) * l
- if bound(ax,ay) and bound(bx,by) and bound(cx,cy) and bound(dx,dy) : pass
- else : return l - 1
- if not deep_check(Map[x][y],Map[ax][ay],Map[bx][by],Map[cx][cy],Map[dx][dy]): return l - 1
-
- ans = 0
- for i in range(1,n-1) :
- for j in range(1,m-1) :
- ans += check(i,j)
-
- print(ans)
归并排序求逆序对的模版题,只不过这里不是求逆序对的数量了,答案等于每一对逆序对中较大的那个数(即在左边的数)的和(仔细思考下为什么呢?🤔) 如果没有了解过归并排序,建议先学一下哈
-
- N = int(input())
- lst = list(map(int,input().split()))
-
- if N < 2 : print(0) ; exit()
- ans = 0
-
- def mergeSort(nums,l,r,tmp) :
- if l >= r : return
- mid = l + r >> 1
- mergeSort(nums,l,mid,tmp)
- mergeSort(nums,mid+1,r,tmp)
- merge(nums,l,mid,r,tmp)
-
- def merge(nums,l,mid,r,tmp) :
- global ans
-
- i, j = l, mid + 1
- while i <= mid and j <= r :
- if nums[i] <= nums[j] :
- tmp.append(nums[i])
- i += 1
- else :
- ans += nums[i]
- tmp.append(nums[j])
- j += 1
- while i <= mid : tmp.append(nums[i]) ; i += 1
- while j <= r : tmp.append(nums[j]) ; j += 1
-
- for k in range(len(tmp)) : nums[l+k] = tmp[k]
- tmp.clear()
-
- mergeSort(lst,0,N-1,[])
- print(ans)