Mocha and Stars

题意

给定 \(n,m\) ,问符合下定条件的数列有多少个:

  • 对于\(a_i(1\le i\le n)\),\(a_i\in [l_i,r_i]\cap \mathbb{Z}\)
  • \(\sum_{i=1}^ma_i\le m\)
  • \(\gcd(a_1,a_2,...a_n)=1\)

答案对 \(998\ 244\ 353\) 取模。

题解

倘若没有第二个条件,和这题就差不多了。第二个问题显然是个多重背包问题,所以我们把那题的代码稍微改一下,就能得出正确答案了。

每次要解决的子问题是这样的一个和原问题几乎一致的问题:

给定 \(n,m\) ,问符合下定条件的数列有多少个:

  • 对于\(a_i(1\le i\le n)\),\(a_i\in [l_i,r_i]\cap \mathbb{Z}\)
  • \(\sum_{i=1}^ma_i\le m\)

其中,\(\gcd(a_1,a_2,...,a_n)=d\), \(d\) 是我们枚举的 \(\gcd\) 。容易发现,把这里的所有数全都除以 \(d\) ,就变成了这么个问题:

给定 \(n,m\) ,问符合下定条件的数列有多少个:

  • 对于\(a_i(1\le i\le n)\),\(a_i\in [\lceil l_i/d\rceil,\lfloor r_i/d \rfloor]\cap \mathbb{Z}\)
  • \(\sum_{i=1}^ma_i\le m/d\)

当成一个普通的前缀和优化的多重背包问题即可。

AC代码

#include <bits/stdc++.h>

#define IO ios::sync_with_stdio(NULL)
#define sc(z) scanf("%lld", &(z))
#define _ff(i, a, b) for (ll i = a; i <= b; ++i)
#define _rr(i, a, b) for (ll i = b; i >= a; --i)
#define _f(i, a, b) for (ll i = a; i < b; ++i)
#define _r(i, a, b) for (ll i = b - 1; i >= a; --i)
#define mkp make_pair
#define endl "\n"
#define pii pair<int,int>
#define fi first
#define se second
#define lowbit(x) x&(-x)
#define pb push_back using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld; const int N = 50 + 5;
const ll M = 1e5 + 5;
const ll mod = 998244353;
const int inf = 1e9;
const double eps = 1e-9;
const double PI = acos(-1.0);
const pii NIL = {0,0}; int n, m;
ll l[N], r[N], dp[M], sum[M], f[M]; ll deal(ll d) {
ll lim=m/d;
f[0]=1;_ff(i,1,lim)f[i]=0;
_ff(i,1,n){
ll L=(l[i]+d-1)/d,R=r[i]/d;
if(L>R)return 0;
sum[0]=f[0];
_ff(j,1,lim)sum[j]=(f[j]+sum[j-1])%mod;
_ff(j,0,lim){
if(j>R)f[j]=(sum[j-L]-sum[j-R-1]+mod)%mod;
else if(j>=L)f[j]=sum[j-L];
else f[j]=0;
}
}
ll ans=0;
_ff(i,1,lim)ans=(ans+f[i])%mod;
return ans;
} void solve() {
cin>>n>>m;
_ff(i,1,n)cin>>l[i]>>r[i];
_rr(d,1,M){
dp[d]=deal(d);
for(int i=d+d;i<=M;i+=d)dp[d]=(dp[d]-dp[i]+mod)%mod;
}
cout<<dp[1]<<endl;
} int main() {
// IO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // !ONLINE_JUDGE // pre();ll T; cin>>T;
// _f(i,0,T) {
// // cout<<"Case "<<i+1<<": ";
// solve();
// } solve(); return 0;
}

最新文章

  1. 我的CodeF水A题之路
  2. SQL性能优化常见措施(Lock wait timeout exceeded)
  3. 如何捕捉并分析SIGSEGV的现场
  4. LoadRunner AJAX TruClient协议Tips and Tricks
  5. str和repr的
  6. PAT 基础编程题 4-11 求自定类型元素序列的中位数(希尔排序)
  7. 常用字符串API
  8. Effective Java2读书笔记-类和接口(一)
  9. POJ 3169 Layout (图论-差分约束)
  10. linux创建vg、lv
  11. php 时间问题
  12. U型理论
  13. 使用 dep 配置 golang 开发环境
  14. 自学Python3.3-函数分类(内置函数补充)
  15. Asp.net MVC5 学习笔记
  16. php协程
  17. FastReport问题整理(http://129.sqdj.gov.cn/?p=77)
  18. 阿里支付宝java接口
  19. 如何在Ubuntu-14.04上安装g++-6.3 ?
  20. MyBatis传入参数

热门文章

  1. [2015年NOIP提高组] 跳石头
  2. Python后端基础知识总结
  3. make vscode portable together with its extensions
  4. usb 2.0 request
  5. (Fiddler)Fiddler 的相关操作
  6. Tomcat异常处理机制
  7. EBS关于LPN的API【OM】
  8. ctp认证权限
  9. linux系统:共享库问题之如version `ZLIB_1.2.9‘ not found
  10. Kubernetes学习笔记(二)