当前位置: 首页 > news >正文

P10185 [YDOI R1] Necklace

[YDOI R1] Necklace - 洛谷

因为是方案数求和

我们考虑计算每种珠子单独贡献的方案数有

\sum_{i=1}^{n}2^{sum-a[i]}*\binom{a[i]}{x}*v^{x}

因为有二项式定理

(a+b)^{n}=\sum_{i=0}^{n}\binom{n}{i}*a^{i}*b^{n-i}

构造

\sum_{i=1}^{n}2^{sum-a[i]}*\binom{a[i]}{x}*v^{x}*1^{a[i]-x}

因为n不取0,便有

\sum_{i=1}^{n}2^{sum-a[i]}*((v+1)^{a[i]}-1)

时间复杂度O(nlogs)

modint + qmi code

#include <bits/stdc++.h>#define INF (1ll<<60)
#define eps 1e-6
using namespace std;
typedef long long ll;
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef vector<vector<int> > vii;
typedef vector<vector<vector<int> > > viii;
typedef vector<ll> vl;
typedef vector<vector<ll> > vll;
typedef vector<double> vd;
typedef vector<vector<double> > vdd;
#define time mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());//稳定随机卡牛魔 ull
const int mod=1e9+7;
template<class T>
constexpr T power(T a, LL b) {T res = 1;for (; b; b /= 2, a *= a) {if (b % 2) {res *= a;}}return res;
}template<int P>
struct MInt {int x;constexpr MInt() : x{} {}constexpr MInt(LL x) : x{norm(x % getMod())} {}static int Mod;constexpr static int getMod() {if (P > 0) {return P;} else {return Mod;}}constexpr static void setMod(int Mod_) {Mod = Mod_;}constexpr int norm(int x) const {if (x < 0) {x += getMod();}if (x >= getMod()) {x -= getMod();}return x;}constexpr int val() const {return x;}explicit constexpr operator int() const {return x;}constexpr MInt operator-() const {MInt res;res.x = norm(getMod() - x);return res;}constexpr MInt inv() const {assert(x != 0);return power(*this, getMod() - 2);}constexpr MInt &operator*=(MInt rhs) & {x = 1LL * x * rhs.x % getMod();return *this;}constexpr MInt &operator+=(MInt rhs) & {x = norm(x + rhs.x);return *this;}constexpr MInt &operator-=(MInt rhs) & {x = norm(x - rhs.x);return *this;}constexpr MInt &operator/=(MInt rhs) & {return *this *= rhs.inv();}friend constexpr MInt operator*(MInt lhs, MInt rhs) {MInt res = lhs;res *= rhs;return res;}friend constexpr MInt operator+(MInt lhs, MInt rhs) {MInt res = lhs;res += rhs;return res;}friend constexpr MInt operator-(MInt lhs, MInt rhs) {MInt res = lhs;res -= rhs;return res;}friend constexpr MInt operator/(MInt lhs, MInt rhs) {MInt res = lhs;res /= rhs;return res;}friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {LL v;is >> v;a = MInt(v);return is;}friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {return os << a.val();}friend constexpr bool operator==(MInt lhs, MInt rhs) {return lhs.val() == rhs.val();}friend constexpr bool operator!=(MInt lhs, MInt rhs) {return lhs.val() != rhs.val();}
};template<>
int MInt<0>::Mod = 1e9 + 7;template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();constexpr int P = 1e9 + 7;
using Z = MInt<P>;const int N=2e5+9;Z inv[N],f[N],invf[N];//组合数
void init(){inv[1]=1;for(int i=2;i<N;i++){inv[i]=inv[mod%i]*(mod-mod/i);}f[0]=1,invf[0]=1;for(int i=1;i<N;i++){f[i]=f[i-1]*i;invf[i]=invf[i-1]*inv[i];}
}
Z C(ll n,ll m){if(n<m || m<0){return 0;}return f[n]*invf[m]*invf[n-m];
}
Z qmi(Z a,ll b){Z res=1;while(b){if(b&1){res=res*a;}b>>=1;a=a*a;}return res;
}
int main() {ios::sync_with_stdio(false);cin.tie(nullptr), cout.tie(nullptr);int n;cin>>n;vi a(n+1);ll sum=0;for(int i=1;i<=n;i++){cin>>a[i];sum+=a[i];}vi v(n+1);for(int i=1;i<=n;i++){cin>>v[i];}Z ans=0;for(int i=1;i<=n;i++){ans+=qmi(2,sum-a[i])*(qmi(v[i]+1,a[i])-1);}cout<<ans<<'\n';return 0;
}


http://www.mrgr.cn/news/43754.html

相关文章:

  • Qt开发技巧(十四)文字的分散对齐,设置动态库路径,进度条控件的文本,文件对话框的卡顿,滑块控件的进度颜色,停靠窗体的排列,拖拽事件的坑
  • 2025秋招LLM大模型多模态面试题(九)-- LoRA 面试问题大全:从理论到实践
  • Chromium 搜索引擎功能浅析c++
  • 重生之我们在ES顶端相遇第 20 章 - Mapping 参数设置大全(进阶)
  • 【表达式的值II】
  • 终于有人把多模态大模型讲这么详细了
  • [Python] 轻松入门输出语句与条件语句
  • ElasticSearch 备考 -- Snapshot Restore
  • 《浔川社团官方通报 —— 为何明确 10 月 2 日上线的浔川 AI 翻译 v3.0 再次被告知延迟上线》
  • 教育技术革新:SpringBoot在线教育系统开发
  • 【数学分析笔记】第4章第4节 复合函数求导法则及其应用(3)
  • ffmpeg面向对象——拉流协议匹配机制探索
  • CSS盒子模型
  • 自动驾驶系列—线控系统:驱动自动驾驶的核心技术解读与应用指南
  • 独孤思维:闲得蛋疼才去做副业
  • C++面试速通宝典——10
  • ICM20948 DMP代码详解(64)
  • 降低大模型幻觉的5种方案
  • IDEA基础开发配置以及和git的联动
  • Leetcode—200. 岛屿数量【中等】