竞赛讨论区 > 牛客练习赛52题A数数Python一直无法通过,求赐教~
头像
痴迷、淡然
发布于 2019-09-17 17:00
+ 关注

牛客练习赛52题A数数Python一直无法通过,求赐教~

牛客练习赛52 题A 数数 Python 一直无法通过,求赐教~

为啥同样的思路,python过不了?
 这是C++版(借鉴 hnust_liushisi
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const ll N=1e7+10;
ll sz[N];
ll qpow(ll a, ll n)//计算a^n % mod
{
    ll re = 1;
    while(n)
    {
        if(n & 1)//判断n的最后一位是否为1
            re = (re * a) % mod;
        n >>= 1;//舍去n的最后一位
        a = (a * a) % mod;//将a平方
    }
    return re % mod;
}
int main()
{
    sz[1]=1;
    for(ll i=2;i<N-7;i++)
        sz[i]=(sz[i-1]*i)%mod;
    ll t,n;
    ll ls,ls1;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&n);
        ls=(n+((n-1)*n)/2);
        ls=ls%mod;
        ls=ls*ls;
        ls=ls%mod;
        ls1=qpow(sz[n],2*n);
        printf("%lld %lld\n",ls,ls1);
    }
    return 0;
}

这是我更改后的 python 版:
mod = 998244353    # 取余数

# 打表
table = [0, 1]
for i in range(2, int(1e7+3)):
    table.append((table[-1] * i) % mod)
    
def apow(a, n):
    res = 1
    while n:
        if n & 1:
            res = (res * a) % mod
        n >>= 1
        a = (a * a) % mod
    return res % mod

T = int(input())
for _ in range(T):
    n = int(input())
    sum_li = (n+((n-1)//2)) % mod
    sum_li = (sum_li * sum_li) % mod
    mul_li = apow(table[n], 2 * n)
    print(sum_li, end=' ')
    print(mul_li)

通过率 0.00%,报超出时间(检查循环或时间复杂度),是因为打表了吗?
至今为止,还未见 python 通过的,感谢大佬赐教~

全部评论

(1) 回帖
加载中...
话题 回帖

等你来战

查看全部

热门推荐