蓝桥杯 第六十一天 数学与简单DP

买不到的数目

朴素写法

a,b=map(int,input().split())
vis=[False for i in range(1020000)]
vis[a]=True
vis[b]=True
cur=0
for i in range(min(a,b),1000000):
    if vis[i]:
        if not vis[i+a]:
            vis[i+a]=True
        if not vis[i+b]:
            vis[i+b]=True
    else:
        cur=i
print(cur)

牛逼写法

a,b=map(int,input().split())
print(a*b-a-b)

蚂蚁感冒

n=int(input())
a=list(map(int,input().split()))
ill=a[0]
a.sort(key=lambda x:abs(x))
index=a.index(ill)
ans=1

if ill>0:
    flag=False
    for i in a[index+1:]:
        if i<0:
            ans+=1
            flag=True
    if flag:
        for i in a[:index]:
            if i>0:
                ans+=1
else:
    flag=False
    for i in a[:index]:
        if i>0:
            ans+=1
            flag=True
    if flag:
        for i in a[index+1:]:
            if i<0:
                ans+=1
print(ans)

饮料换购

n=int(input())
ans=n
while n>=3:
    y=n//3
    ans+=y
    n-=y*3
    n+=y
print(ans)

01背包

n,m=map(int,input().split())
dp=[[0 for i in range(m+1)]for j in range(n+1)]
v=[0]
w=[0]
for i in range(n):
    x,y=map(int,input().split())
    v.append(x)
    w.append(y)
for i in range(1,n+1):
    for j in range(m+1):
        dp[i][j]=dp[i-1][j]
        if j>=v[i]:
            dp[i][j]=max(dp[i][j],dp[i-1][j-v[i]]+w[i])
print(dp[n][m])

摘花生

t=int(input())
for _ in range(t):
    n,m=map(int,input().split())
    dp=[[0 for i in range(m+1)]]
    for i in range(n):
        dp.append([0]+list(map(int,input().split())))
    for i in range(1,n+1):
        for j in range(1,m+1):
            dp[i][j]+=max(dp[i-1][j],dp[i][j-1])
    print(dp[n][m])

最长上升子序列

n=int(input())
a=[-1<<31]+list(map(int,input().split()))
dp=[0 for i in range(n+1)]
for i in range(1,n+1):
    for j in range(i):
        if a[i]>a[j]:
            dp[i]=max(dp[i],dp[j]+1)
print(max(dp))

地宫取宝

n,m,k=map(int,input().split())
adj=[[1 for i in range(m+1)]]
maxv=-1
for i in range(n):
    adj.append([0]+list(map(int,input().split())))
    for j in range(m+1):
        adj[i+1][j]+=1
    maxv=max(maxv,max(adj[i+1]))
N=14
dp=[[[[0 for i in range(maxv+1)]for j in range(k+1)]for w in range(m+1)]for q in range(n+1)]
dp[1][1][1][adj[1][1]]=1
dp[1][1][0][0]=1
M=1000000007
for i in range(1,n+1):
    for j in range(1,m+1):
        if i==1 and j==1:
            continue
        curvalue=adj[i][j]
        for u in range(k+1):
            for v in range(maxv+1):
                x1=dp[i][j-1][u][v]
                dp[i][j][u][v]=(dp[i][j][u][v]+x1)%M
                x2=dp[i-1][j][u][v]
                dp[i][j][u][v]=(dp[i][j][u][v]+x2)%M
            if u>0:
                for v in range(curvalue):
                    x1=dp[i][j-1][u-1][v]
                    dp[i][j][u][curvalue]=(dp[i][j][u][curvalue]+x1)%M
                    x2=dp[i-1][j][u-1][v]
                    dp[i][j][u][curvalue]=(dp[i][j][u][curvalue]+x2)%M
ans=0
for i in range(maxv+1):
    ans=(ans+dp[n][m][k][i])%M
print(ans)

波动数列

立即退,放弃做题

n,s,a,b=map(int,input().split())
dp=[[0 for i in range(n)]for j in range(n+1)]
dp[0][0]=1
for i in range(1,n):
    for j in range(n):
        x=dp[i-1][(j-a*i)%n]
        y=dp[i-1][(j+b*i)%n]
        dp[i][j]=(x+y)%100000007
print(dp[n-1][s%n])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值