全部评论
取余的话很简单啊,不是数论入门题吗有理数取余
做完了不会求模 我佛了
乘法逆元:(a/b)%mod=a*(b^(mod-2))%mod mod为素数
我感觉这个特殊的取模规则是把分子 (num) 加上 1e9+7 的整数倍直到能被分母 (den) 整除为止,然后返回整除时的商?类似这样的 M = 10**9+7
step = 0
while step * M + num % den != 0:
step += 1
print((step * M + num) // den)
n = 2
p = 1
q = 0
mod = 1000000007
jie = []
ni = []
def power(a,n,mod):
p = 1
while n>0:
if n%2==1:
p = p*a
p = p%mod
n = n//2
a = a*a%mod
return p%mod
def C(n,k):
return jie[n]*ni[k]%mod*ni[n-k]%mod
jie.append(1)
ni.append(1)
for i in range(1,10):
sg = jie[i-1]*i%mod
jie.append(sg)
nio = power(sg, mod-2, mod)
ni.append(nio)
sum =0
for i in range(p,n-q+1):
sum = (sum + C(n,i)*(i%mod)%mod)%mod
#print(sum)
mu = 0
for i in range(p,n-q+1):
mu = (mu + C(n,i))%mod
#print(mu)
ans = sum * power(mu, mod-2, mod)%mod
print(ans) AC代码
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int getOne(int num) {
int ret = 0;
int tmp;
while (num) {
ret++;
tmp = num - 1;
num = num & tmp;
}
return ret;
}
int main() {
int n, p, q;
cin >> n >> p >> q;
long long sum = 0;
int pp = n - q;
int count = 0;
//if (p == n-q)
// return
for (int i = 0; i < pow(2, n); i++) {
int ones = getOne(i);
cout << "ones:" << ones << endl;
if (ones >= p && ones <=pp) {
count++;
sum += ones;
}
}
cout << "sum:" << sum << " count:" << count << endl;
cout << 1000000007 << endl;
cout << sum * 1000000007 / count << endl;
} 这个取模真是服,卡在取模上了
我只会第一道题
题目是啥呀?
为啥我跟你的题不一样,我没有这道题
from fractions import Fraction
n,p,q = map(int,input().split())
mod = 10**9 + 7
# 计算组合
def C(n):
N=n
c = [[0] * (N+1) for _ in range(N+1)]
c[0][0] = 1
for i in range(1,N+1):
c[i][0] = 1
for j in range(1,N+1):
c[i][j] = c[i-1][j-1] + c[i-1][j]
return c
c = C(n)
P_pos = 0
P_neg = 0
for i in range(min(p,q),n+1):
if i >= p:
P_pos += c[n][i]
if i >= q:
P_neg += c[n][i]
total = 2**n
n_chan = (P_pos * P_neg) // (total)
# 求期望
exp = 0
for j in range(p,n+1):
exp += Fraction(j * (P_neg * c[n][j])) / Fraction(total*n_chan)
num = exp.numerator
den = exp.denominator
print(int((mod+num)//den)) 这个取模好恶心,我认为要根据分子分母取模,结果只ac0.1
谁知道怎么取模😂
直接求模肯定会超时,不知道为什么不选小一点的数
20%
同不会取模
请问取模是怎么算的呢? 并不是直接result % 1e9+7, 这算哪门子取模啊?😂😂
自信敲代码 0ac🤣
暴力取模,30%+超时
10%
求答案
相关推荐

点赞 评论 收藏
分享