[SCU 4532] interesting (数论+技巧+SPFA转移DP)
来源:互联网 发布:mac电脑兼容双系统吗 编辑:程序博客网 时间:2024/06/04 06:07
SCU - 4532
a1∗b1+a2∗b2...+an∗bn=B
给定a1...an ,Bmax 、Bmin
问有多少个B,Bmin<=B<=Bmax ,
使得b1...bn 存在非负整数解
首先问题可以转化为在区间
如果能解决这个,原问题的用
接下来有一个很关键的步骤,也是这题的神来之笔
之所以说神,是因为目前我还没深刻理解这么做的道理 (2016.7.14)
将等式两边
这样使得
于是题目就转化为了求每个等价类中最小的那个数
因为知道了最小的数
然后把每个等价类的答案累加起来即为结果
设
初始条件
转移方程为
由于这个 dp过程可能有环,所以采用 SPFA的方式进行转移
#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <iostream>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <cctype>#include <map>#include <set>#include <queue>using namespace std;typedef pair<int,int> Pii;typedef long long LL;typedef unsigned long long ULL;typedef double DBL;typedef long double LDBL;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define Sqr(a) ((a)*(a))const int maxn=20, maxa=5e5+10;const LL INF=0x3f3f3f3f3f3f3f3f;int N;LL bmin, bmax;LL A[maxn];LL dp[maxa];bool inq[maxa];LL Get(LL,LL,int);int main(){ #ifdef LOCAL freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout); #endif int T; scanf("%d", &T); for(int ck=1; ck<=T; ck++) { scanf("%d%lld%lld", &N, &bmin, &bmax); for(int i=1; i<=N; i++) scanf("%lld\n", &A[i]); sort(A+1, A+1+N); if(A[1]==0) { puts("0"); continue; } MST(dp,0x3f); CLR(inq); dp[0]=0; inq[0]=1; queue<LL> que; que.push(0); while(que.size()) { LL u=que.front(); que.pop(); for(int i=2; i<=N; i++) { LL v = (u+A[i])%A[1]; if(dp[v] > dp[u]+A[i]) { dp[v] = dp[u]+A[i]; if(!inq[v]) { inq[v]=1; que.push(v); } } } inq[u]=0; }// for(int i=0; i<A[1]; i++) printf("%lld ", dp[i]); puts(""); LL ans=0; for(int i=0; i<A[1]; i++) { ans += Get(bmax,dp[i],A[1]) - Get(bmin-1,dp[i],A[1]); } cout << ans << '\n'; } return 0;}LL Get(LL R, LL L, int r){ if(R<L) return 0; return (R-L)/r+1;}
0 0
- [SCU 4532] interesting (数论+技巧+SPFA转移DP)
- SCU 4371 Interesting matrix
- SCU 4426 Counting_3 (dp)
- SCU 4511 任务(dp)
- UVA 12664 Interesting Calculator(spfa)
- CSU 1336 Interesting Calculator(spfa)
- URAL 2070 Interesting Numbers(数论)
- ural 2070 Interesting Numbers(数论基础)
- SCU 4437Carries(数论)
- [SCU 4498] RunningPhoton's Nightmare (BFS预处理+SPFA)
- SCU 1114-数字三角(dp)
- [SCU 4501] DNA序列 (状压DP)
- [SCU 4511] 任务 (状态优化DP)
- [SCU 4514] Simple dp (XJBLG法)
- SCU 4510 TaoSama与煎饼(dp)
- SCU 4512 Goozy的积木(dp)
- SCU 4515 又见背包(dp)
- TOJ 4603 Interesting Calculator -- SPFA
- Java File类,IO流,字节输入流InputStream,FileInputStream学习使用
- webuploader 常用设置
- TTEFS 文档密级标识安全管理控件
- python学习——文档测试
- 同步发电机 有功功率 无功功率调节
- [SCU 4532] interesting (数论+技巧+SPFA转移DP)
- 我的第一篇csdn博客
- 关于Node节点上一些坐标的转换的运用
- 容斥原理 和 欧拉函数
- 2016.07.10学习内容
- POJ 3164 Command Network
- NYOJ不高兴的小明
- 我的c学习:连加
- dijkstra单源最短路径