Sums gym100753M

同余最短路模板,然而这个东西貌似也可以做去年D1T2

首先我们选择一个模数作为基准,然后剩下的这样连边:

对于一个面值为 x 的硬币 ,当前在 u 这个点(感性理解一下吧)

  1. u + x < Mod

    这种情况直接从u向 u + x 连一条长度为0的边,表示我们在 0 * M + (u + x) 的时候就已经可以凑出了

  2. u + x > Mod

    这种情况下可以 u 向 ( u + x ) mod Mod 连一条长度为1的边,表示通过x的硬币至少在 1 * M + ( u + x ) 凑出。

直接dijk 复杂度会炸(但是可以用zkw线段树优化成 nlogm,就爆过去了)

然后如果Mod取硬币面值的最大值,那么这就是个01bfs,但是复杂度也是 O(n + M) 然而M是 n^2级别的

我们发现每个点只会考虑一次,所以我们可以用bitset优化这个bfs。具体而言,如果我们把 $ a[i] $ 存到一个bitset中,我们需要的转移是 $ trans $ 集体往左移动u次且循环移位后的trans。

学到了Bitset优化搜索这个套路233 但是由于需要提取1,还是得手写。。

这样优化就是 $ n + \frac{M}{w} $ 了

为了偷懒还是用类似dijk的bfs吧。。反正复杂度没问题

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
using namespace std;
//#define int long long
typedef long long ll;
#define MAXN 50006
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define inf 0x3f3f3f3f
#define cmx( a , b ) a = max( a , b )
#define cmn( a , b ) a = min( a , b )
void read( int& x ) {
scanf("%d",&x);
}
void read( ll& x ) {
scanf("%lld",&x);
}
int n , q;
int A[MAXN] , mx; struct Bitset
{
unsigned a[1600];
void reset()
{
memset(a,0,sizeof(a));
}
Bitset()
{
reset();
}
void flip(int x)
{
a[x>>5]^=1<<(x&31);
}
void set(int x)
{
a[x>>5]|=1<<(x&31);
}
void reset(int x)
{
a[x>>5]&=~(1<<(x&31));
}
int test(int x)
{
return (a[x>>5]>>(x&31))&1;
}
Bitset operator ~()const
{
Bitset ret;
for(int i=0;i<1600;i++)ret.a[i]=~a[i];
return ret;
}
Bitset operator &(const Bitset &b)const
{
Bitset ret;
for(int i=0;i<1600;i++)ret.a[i]=a[i]&b.a[i];
return ret;
}
Bitset operator |(const Bitset &b)const
{
Bitset ret;
for(int i=0;i<1600;i++)ret.a[i]=a[i]|b.a[i];
return ret;
}
Bitset operator ^(const Bitset &b)const
{
Bitset ret;
for(int i=0;i<1600;i++)ret.a[i]=a[i]^b.a[i];
return ret;
}
Bitset operator <<(const int t)const
{
Bitset ret;
unsigned last=0;
int high=t>>5,low=t&31;
for(int i=0;i+high<1600;i++)
{
ret.a[i+high]=last|(a[i]<<low);
if(low)last=a[i]>>(32-low);
}
return ret;
}
Bitset operator >>(const int t)const
{
Bitset ret;
unsigned last=0;
int high=t>>5,low=t&31;
for(int i=1600-1;i>=high;i--)
{
ret.a[i-high]=last|(a[i]>>low);
if(low)last=a[i]<<(32-low);
}
return ret;
}
vector<int> ones()const
{
vector<int> ret;
for(int i=0;i<1600;i++)
{
unsigned tmp=a[i];
while(tmp)
{
short t=__builtin_ctz(tmp);
ret.pb((i<<5)|t);
tmp^=1u<<t;
}
}
return ret;
}
}use,trans;
int dis[MAXN];
priority_queue< pii , vector<pii> , greater<pii> > Q;
vector<int> cur;
void bfs( ) {
memset( dis , 0x3f , sizeof dis );
Q.push( mp( 0 , 0 ) ); dis[0] = 0;
while( !Q.empty() ) {
int u = Q.top().se; Q.pop( );
cur = ( ( ( trans << u ) | ( trans >> ( mx - u ) ) ) & use ).ones( );
for( auto& v : cur ) {
if( v < u )
dis[v] = dis[u] + 1 , Q.push( mp( dis[v] , v ) ) , use.reset( v );
else
dis[v] = dis[u] , Q.push( mp( dis[v] , v ) ) , use.reset( v );
}
}
} int main() {
read( n );
for( int i = 1 ; i <= n ; ++ i )
read( A[i] ) , mx = max( mx , A[i] );
for( int i = 1 ; i <= n ; ++ i ) trans.set( A[i] );
for( int i = 0 ; i <= mx ; ++ i ) use.set( i );
bfs();
read( q );
int x;
while( q --> 0 ) {
read( x );
int a = x % mx , b = x / mx;
puts( ( dis[a] == 0x3f3f3f3f || dis[a] > b ) ? "NIE" : "TAK" );
}
} /*
* Things you should pay attention to
* inf is big enough?
* out of bounds?
* long long ?
*/

Sums gym100753M的更多相关文章

  1. 正睿OI国庆DAY2:图论专题

    正睿OI国庆DAY2:图论专题 dfs/例题 判断无向图之间是否存在至少三条点不相交的简单路径 一个想法是最大流(后来说可以做,但是是多项式时间做法 旁边GavinZheng神仙在谈最小生成树 陈主力 ...

  2. [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  3. UVA-11997 K Smallest Sums

    UVA - 11997 K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & ...

  4. POJ3187Backward Digit Sums[杨辉三角]

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6350   Accepted: 36 ...

  5. Leetcode Find K Pairs with smallest sums

    本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...

  6. SPOJ TSUM Triple Sums(FFT + 容斥)

    题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...

  7. ural 2065. Different Sums

    2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...

  8. codeforces 477A A. Dreamoon and Sums(数学)

    题目链接: A. Dreamoon and Sums time limit per test 1.5 seconds memory limit per test 256 megabytes input ...

  9. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

随机推荐

  1. Golang通脉之方法

    方法和接收者 Go语言中的方法(Method)是一种作用于特定类型变量的函数.这种特定类型变量叫做接收者(Receiver).接收者的概念就类似于其他语言中的this或者 self. Go 语言中同时 ...

  2. 【c++ Prime 学习笔记】目录索引

    第1章 开始 第Ⅰ部分 C++基础 第2章 变量和基本类型 第3章 字符串.向量和数组 第4章 表达式 第5章 语句 第6章 函数 第7章 类 第 Ⅱ 部分 C++标准库 第8章 IO库 第9章 顺序 ...

  3. spring源码分析(二)- 容器基础

    1.基本用法 用过Spring的都知道,bean是Spring中最基础也是最核心的.首先看一个简单的例子. 一个类和一个配置文件 package bean; public class MyBean { ...

  4. Noip模拟39 2021.8.14

    T1 打地鼠 都切掉了的简单题 1 #include<bits/stdc++.h> 2 #define int long long 3 using namespace std; 4 con ...

  5. Spring Security:Servlet 过滤器(三)

    3)Servlet 过滤器 Spring Security 过滤器链是一个非常复杂且灵活的引擎.Spring Security 的 Servlet 支持基于 Servlet 过滤器,因此通常首先了解过 ...

  6. 小白自制Linux开发板 十. NES游戏玩起来

    本篇基于我们制作的Debian文件系统而展开,而且我们这会玩一些高级的操作方式--用我们的小电脑进行程序编译.   所以本篇操作全部都在我们个的开发板上完成.   1. 开发环境搭建 首先安装gcc, ...

  7. 如何选择普通索引和唯一索引《死磕MySQL系列 五》

    系列文章 一.原来一条select语句在MySQL是这样执行的<死磕MySQL系列 一> 二.一生挚友redo log.binlog<死磕MySQL系列 二> 三.MySQL强 ...

  8. (一)Mongodb学习之 Centos 7 单机部署

    学习参考:https://www.runoob.com/mongodb/mongodb-tutorial.html 一.部署环境 1.系统:Centos 7 2.mongodb: mongodb-li ...

  9. zabbix部署文档

    环境:zabbix server centos 7 1611最小化安装 172.16.103.2 zabbix client Centos 7 1611 最小化安装 172.16.103.3 1,配置 ...

  10. AppScan 10安装使用

    一.简介 AppScan是IBM的一款web安全扫描工具,具有利用爬虫技术进行网站安全渗透测试的能力,能够根据网站入口自动摸取网页链接进行安全扫描,提供了扫描.报告和修复建议等功能. appscan有 ...