NC52853. Strange Prime
描述
输入描述
The input contains zero or more test cases and is terminated by end-of-file. For each test case:
The first line contains an integer n.
The second line contains n integers .
*
*
* The sum of n does not exceed .
输出描述
For each case, output an integer which denotes the number of different ways.
示例1
输入:
2 0 0 3 0 1 2
输出:
999999956 2756
C++11(clang++ 3.9) 解法, 执行用时: 139ms, 内存消耗: 484K, 提交时间: 2019-10-05 22:09:12
#include <bits/stdc++.h> #define LL long long using namespace std; const LL MOD = 1e9 + 7; LL qpow(LL x, LL y){ LL z = 1; while (y > 0){ if (y & 1) z = z * x % MOD; x = x * x % MOD; y /= 2; } return z; } int main(){ int n; while (scanf("%d", &n) != EOF){ LL ans1 = 1, ans2 = 1, P = 1e10 + 19; for (int i = 1; i <= n; i++){ int x; scanf("%d", &x); ans1 = ans1 * (P % MOD - x + MOD) % MOD; ans2 = ans2 * (MOD - x) % MOD; } LL ans = (ans1 - ans2 + MOD) * qpow(P % MOD, MOD - 2) % MOD; printf("%lld\n", ans); } return 0; }
C++14(g++5.4) 解法, 执行用时: 541ms, 内存消耗: 468K, 提交时间: 2020-05-21 18:08:46
#include<iostream> using namespace std; #define int long long int mod=1e9+7; int p=1e10+19; int n; int qpow(int a,int b){ int ans=1; for(;b;b>>=1){ if(b&1) ans=ans*a%mod; a=a*a%mod; } return ans; } signed main() { p%=mod; while(cin>>n){ int ans1=1,ans2=1; for(int i=1;i<=n;i++){ int t; cin>>t; ans1=ans1*(p-t)%mod; ans2=ans2*(mod-t)%mod; } cout<<(ans1-ans2+mod)%mod*qpow(p,mod-2)%mod<<endl; } return 0; }