神秘钥匙
神秘钥匙
https://ac.nowcoder.com/acm/problem/20701
神秘钥匙
看到题就得以下公式:
ans = 1*C(n,1) + 2*C(n,2) + 3*C(n,3) + ... + n*C(n,n) = n*2^(n-1) 计算过程: S1 = 0*C(n,0) + 1*C(n,1) + 2*C(n,2) + ... + n*C(n,n) S2 = n*C(n,0) + (n-1)*C(n,1) + (n-2)*C(n,2) + ... + 0*C(n,n) S = S1 + S2 = n*[C(n,0)+C(n,1)+...+C(n,n)] = n*2^n ∵ S1 = S2 (利用 C(n,m) = C(n,n-m)) ∴ 2*S1 = n*2^n S1 = n*2^(n-1)
代码:
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; const int mod = 1000000007; ll qpow(ll a, ll n){ ll ans = 1; while(n){ if(n&1) ans = (a*ans) % mod; a = a*a % mod; n >>= 1; } return ans; } int main() { ll n; scanf("%lld",&n); ll ans = n*qpow(2,(n-1))%mod; printf("%lld\n",ans); return 0; }