Sums gym100753M
Sums gym100753M
同余最短路模板,然而这个东西貌似也可以做去年D1T2
首先我们选择一个模数作为基准,然后剩下的这样连边:
对于一个面值为 x 的硬币 ,当前在 u 这个点(感性理解一下吧)
u + x < Mod
这种情况直接从u向 u + x 连一条长度为0的边,表示我们在 0 * M + (u + x) 的时候就已经可以凑出了
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的更多相关文章
- 正睿OI国庆DAY2:图论专题
正睿OI国庆DAY2:图论专题 dfs/例题 判断无向图之间是否存在至少三条点不相交的简单路径 一个想法是最大流(后来说可以做,但是是多项式时间做法 旁边GavinZheng神仙在谈最小生成树 陈主力 ...
- [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 ...
- UVA-11997 K Smallest Sums
UVA - 11997 K Smallest Sums Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- POJ3187Backward Digit Sums[杨辉三角]
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6350 Accepted: 36 ...
- Leetcode Find K Pairs with smallest sums
本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...
- SPOJ TSUM Triple Sums(FFT + 容斥)
题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...
- ural 2065. Different Sums
2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...
- codeforces 477A A. Dreamoon and Sums(数学)
题目链接: A. Dreamoon and Sums time limit per test 1.5 seconds memory limit per test 256 megabytes input ...
- [codeforces 509]C. Sums of Digits
[codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...
随机推荐
- AIApe问答机器人Scrum Meeting 4.23
Scrum Meeting 1 日期:2021年4月23日 会议主要内容概述:各成员汇报进度情况,前后端针对WebAPI进行协调与统一工作. 一.进度情况 组员 负责 两日内已完成的工作 后两日计划完 ...
- 『学了就忘』Linux基础 — 3、CentOS镜像下载
下载CentOS镜像可以从官网下载:https://www.centos.org/download/. 也可以从国内的镜像网站下载. 阿里云:https://mirrors.aliyun.com/ce ...
- [调试笔记] 10.8模拟赛11 T4 甜圈
这题正解线段树维护哈希,同机房神犇已经讲的很明白了.这里只说sbwzx在调试的时候犯的sb错误. 1.关于pushdown和update 众所周知,sbwzx一写带lazy的线段树,就必在pushdo ...
- 示波器分析I2C时序波形图
对于嵌入式开发的朋友来说,I2C协议实在是再熟悉不过了,有太多的器件,采用的都是通过I2C来进行相应的设置.今天,我们就随便聊聊这个I2C协议. I2C协议中最重要的一点是I2C地址.这个地址有7位和 ...
- Python课程笔记(十)
不陌生,之前学习一个开源SpringBoot项目,Mysql5.5更换到5.7搞得头疼. 数据库连接的坑之前写的IDEA系列连接会遇到的问题.课程代码 今天上课就主要学习了python如何连接mysq ...
- 阿里P7面试官:请你简单说一下类加载机制的实现原理?
面试题:类加载机制的原理 面试官考察点 考察目标: 了解面试者对JVM的理解,属于面试八股文系列. 考察范围: 工作3年以上. 技术背景知识 在回答这个问题之前,我们需要先了解一下什么是类加载机制? ...
- best-time-to-buy-and-sell-stock leetcode C++
Say you have an array for which the i th element is the price of a given stock on day i. If you were ...
- hdu 1160 FatMouse's Speed(最长不下降子序列+输出路径)
题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to ...
- Language Server for Java™ 1.0 在VS Code上正式发布!
Nick Zhu form Senior Program Manager, Developer Division at Microsoft 今天,我们很高兴与大家宣布:Language Server ...
- Linux&C———进程间通信
管道和有名管道 消息队列 共享内存 信号 套接字 由于进程之间的并不会像线程那样共享地址空间和数据空间,所以进程之间就必须有自己特有的通信方式,这篇博客主要介绍自己了解到的几种进程之间的通信方式,内容 ...