Python练习(洛谷)

P1226:快速幂

参考代码:

def ksm(a,b,mod):
    ans=1
    while b:
        if b&1==1:
            ans=(ans*a)%mod
        b>>=1
        a=(a*a)%mod
    return ans%mod
if __name__== '__main__':
  a,b,c=map(int,input().split())
  t=ksm(a,b,c)
  print('%d^%d mod %d=%d'%(a,b,c,t))

P1082:同余方程

参考代码:

x=0
y=0
def exgcd(a,b):
    global x,y
    if(b==0):
        x=1
        y=0
        return a
    exgcd(b,a%b)
    t=x
    x=y
    y=t-int(a/b)*y
if __name__== '__main__':
    a,b=map(int,input().split())
    exgcd(a,b)
    while x<0:
        x+=b
    print(x)
	

P3902:递增

参考代码:

stack=[]
a=[]       
n=0 
def lower(x,l,r):
    while l<=r:
        mid=int((l+r)/2)
        if(x<stack[mid]):
            r=mid-1
        else:
            l=mid+1
    return l

def lis():
    top=1
    stack.append(-1)
    stack.append(a[0])
    for i in range(1,n):
        if(stack[top]<a[i]):
            top+=1
            stack.append(a[i])
        else:
            c=lower(a[i],1,top)
            stack[c]=a[i]
    print(n-top)
if __name__ == '__main__':
    n=int(input())
    a=list(map(int,input().split()))
    lis()
    

P2152:大数gcd

参考代码:

不知道为什么辗转相除会出现re

import fractions as f
if __name__ == '__main__':
    a = int(input())
    b = int(input())
    print(f.gcd(a, b))

你可能感兴趣的:(Python)