蓝桥杯-数的潜能

问题描述

  将一个数N分为多个正整数之和,即N=a1+a2+a3+…+ak,定义M=a1*a2*a3*…*ak为N的潜能。

  给定N,求它的潜能M。

  由于M可能过大,只需求M对5218取模的余数。

输入格式

  输入共一行,为一个正整数N。

输出格式

  输出共一行,为N的潜能M对5218取模的余数。

样例输入

10

样例输出

36

数据规模和约定

  1<=N<10^18

初步思路:

初步思路是采用回溯模板,类比组合总和问题,得出1-N所有满足和为N的总和,分别求乘积取最大值,错误就错误在忽略了潜能是允许有重复的数字的,所以自然是不正确的,故而选择百度大佬思路,得到了规律:

(1)元素不会超过4,因为4=2+2,又可以转化为2的问题,而5=2+3,5<2*3,所以5总能分解成2和3。
(2)尽可能多分解出3,然后分解出2,不要分出1。
考虑任意一个数,除以3之后的结果有以下3种:
(1)能被3除断,那么就分解为3+3+...+3的情况即可。例如9=3+3+3。
(2)被3除余1,分解为3+3+...+3+2+2或者3+3+...+3+4的情况,例如10=3+3+2+2
(3)被3除余2,分解为3+3+...+3+2的情况,例如11=3+3+3+2

老实讲这个思路根本不可能在比赛当时想到,数学功底有限,笑死,数学竞赛白学,按照规律,写出代码,然而超时60分及格:

def mypow(k):
    res = 1
    for i in range(k):
        res = res*3
        res = res%5218
    return int(res)
 
n = int(input())
if n <= 4:
    result = n
if n%3 == 0 and n!=3:
    result = mypow(n//3)%5218
if n%3 == 1 and n!=1:
    result = 4*mypow((n//3)-1)%5218
if n%3 == 2 and n!=2 and n!=4:
    result = 2*mypow(n//3)%5218
print("%d"%result)

进阶思路:

数字的规模大了,运算时间就会大大的增加,因为求幂函数是通过循环来求得,像10**18这种烂球数就要进行相当大规模的数字相乘,所以想着采取什么方法能够进行快速的求幂运算,这里想到了内置函数math.pow(a,n),可惜数字规模过大会报错,然后20分不及格:

import math
N=int(input())
def fun(k):
    return math.pow(3,k)%5218
def calc(N):
    if N<=4:
        return N
    if N%3==0 and N!=3:
        return fun(N//3)
    if N%3==1 and N!=1:
        return 4*fun((N-4)//3)
    if N%3==2 and N!=1 and N!=2:
        return 2*fun((N-2)//3)
print(calc(N)%5218)

最终思路:

没有办法,百度python快速幂的方法,还真找到了思路:

原理可参考:https://blog.csdn.net/qq_19782019/article/details/85621386

"""
这里给出的是快速幂取模的函数模板,求的是a^n%m
"""
def fastPow(a,n,m):
    result=1
    while(n):
        if (n&1):result=result*a%m
        a=a*a%m
        n>>=1
    return result

于是轻松AC:

N=int(input())
def fastPow(a,n,m):
    result=1
    while(n):
        if (n&1):result=result*a%m
        a=a*a%m
        n>>=1
    return result
def calc(N):
    if N<=4:
        return N
    if N%3==0 and N!=3:
        return fastPow(3,N//3,5218)
    if N%3==1 and N!=1:
        return 4*fastPow(3,(N-4)//3,5218)
    if N%3==2 and N!=1 and N!=2:
        return 2*fastPow(3,(N-2)//3,5218)
print(calc(N)%5218)

你可能感兴趣的:(蓝桥杯,职场和发展)