NC217858. 心跳调试
描述
Heartache Debug
输入描述
第一行,一个正整数。
第二行,个正整数
。
输出描述
一行,一个非负整数,表示答案对取模的结果。
您可能不知道如何对一个有理数取模。具体地,若您得到的答案为,那么您应当输出
,其中
为
在模
意义下的逆元(数据保证存在)。
示例1
输入:
3 1 1 1
输出:
500000004
示例2
输入:
3 2 1 3
输出:
250000002
C++(clang++11) 解法, 执行用时: 530ms, 内存消耗: 16032K, 提交时间: 2021-03-20 04:15:53
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e6+5,mod=1e9+7; ll n,s[N],a[N],ans=1; ll ksm(ll di,ll mi) {ll res=1; for(;mi;mi>>=1,di=di*di%mod) if(mi&1) res=res*di%mod; return res;} int main() { int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; s[i]=(s[i-1]+a[i])%mod; } ll res=1; for(int i=2;i<n;i++){ res=res*a[i]%mod*ksm(s[n]-s[i-1]+mod,mod-2)%mod; } cout<<res; }
pypy3(pypy3.6.1) 解法, 执行用时: 1741ms, 内存消耗: 147568K, 提交时间: 2021-03-22 23:28:12
gt=lambda:list(map(int,input().split())) M = 1000000007 gt(); p = 1; tot = 0 for w in gt()[:0:-1]: tot = (tot+w)%M p = w*p%M*pow(tot,M-2,M)%M print(p)