【AtCoder】ARC060
ARC060
C - 高橋君とカード / Tak and Cards
每个数减去A,然后转移N次,每次选或不选,最后是和为0的时候的方案数,负数可以通过把所有数右移2500做到
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,A;
int x[55];
int64 dp[2][55 * 55 * 2];
int V = 2505;
void Solve() {
read(N);read(A);
for(int i = 1 ; i <= N ; ++i) {
read(x[i]);
x[i] -= A;
}
int cur = 0;
dp[cur][V] = 1;
for(int i = 1 ; i <= N ; ++i) {
memset(dp[cur ^ 1],0,sizeof(dp[cur ^ 1]));
for(int j = 0 ; j <= 2 * V ; ++j) {
if(j + x[i] >= 0) {
dp[cur ^ 1][j + x[i]] += dp[cur][j];
}
dp[cur ^ 1][j] += dp[cur][j];
}
cur ^= 1;
}
out(dp[cur][V] - 1);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
D - 桁和 / Digit Sum
小于1e6的可以暴力,大于1e6的显然只有两维数
\(N = kb + r,S = k + r\)
必要条件是\(N - S = k(b - 1)\)
枚举\(N - S\)的约数然后求出b看是否合法即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int64 N,S,b = 1e18;
bool check(int64 x) {
if(N / x >= x) return false;
int64 k = N / x,r = N % x;
return k + r == S;
}
void Solve() {
read(N);read(S);
if(N == S) b = N + 1;
if(N > S) {
for(int64 i = 1 ; i <= (N - S) / i ; ++i) {
if((N - S) % i == 0) {
int64 t = (N - S) / i + 1;
if(check(t)) b = min(t,b);
int64 a = (N - S) / i;
t = (N - S) / a + 1;
if(check(t)) b = min(t,b);
}
}
}
for(int64 i = 2 ; i <= 1000000 ; ++i) {
int64 x = N,cnt = 0;
while(x) {
cnt += x % i;
x /= i;
}
if(cnt == S) {b = min(b,i);break;}
}
if(b == 1e18) {puts("-1");}
else {out(b);enter;}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
E - 高橋君とホテル / Tak and Hotels
这个倍增一下就好\(st[i][j]\)表示从i走\(2^j\)天能到的宾馆是啥
每次查询是log的
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,L,Q;
int x[MAXN],st[MAXN][20];
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) read(x[i]);
read(L);read(Q);
for(int i = 1 ; i <= N ; ++i) {
int t = upper_bound(x + 1,x + N + 1,x[i] + L) - x - 1;
st[i][0] = t;
}
for(int j = 1 ; j <= 19 ; ++j) {
for(int i = 1 ; i <= N ; ++i) {
st[i][j] = st[st[i][j - 1]][j - 1];
}
}
int a,b;
for(int i = 1 ; i <= Q ; ++i) {
read(a);read(b);
if(a > b) swap(a,b);
int p = a,ans = 0;
for(int j = 19 ; j >= 0 ; --j) {
if(st[p][j] < b) {
p = st[p][j];
ans += (1 << j);
}
}
++ans;
out(ans);enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
F - 最良表現 / Best Representation
假如所有字母一样,答案是\(|s|\),方案数是1
假如最小循环节长度是\(|s|\)答案是1,方案数是1
剩下情况最小答案都是2,因为一定存在一种断开某个循环子串的中间使得两边都不为循环串,当循环次数为2的时候这个成立,当循环次数为3以上时,如果一个循环节尝试断开的每个位置两边都是循环串,那么这个循环节一定可以变小,与事实不符
注意判断循环节的方式是
N % (N - next[N]) == 0 ? N - next[N] : N
也就是有些情况下前后缀即使重合了,也可能没有循环节
之后我们只需要枚举位置使得两边的最小循环节都是自身就好了,取模1000000007是不存在的
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 500005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
char s[MAXN];
int N;
int nxt[MAXN],suf[MAXN];
bool vis[MAXN];
int gcd(int a,int b) {
return b == 0 ? a : gcd(b,a % b);
}
void Solve() {
scanf("%s",s + 1);
N = strlen(s + 1);
for(int i = 2 ; i <= N ; ++i) {
int p = nxt[i - 1];
while(p && s[p + 1] != s[i]) p = nxt[p];
if(s[p + 1] == s[i]) nxt[i] = p + 1;
else nxt[i] = 0;
}
if(nxt[N] == N - 1) {
out(N);enter;puts("1");
}
else if(nxt[N] == 0 || N % (N - nxt[N]) != 0) {
puts("1");puts("1");
}
else {
int cnt = 0,l = N - nxt[N];
suf[N] = N + 1;
for(int i = N - 1 ; i >= 1 ; --i) {
int p = suf[i + 1];
while(p <= N && s[p - 1] != s[i]) p = suf[p];
if(s[p - 1] == s[i]) suf[i] = p - 1;
else suf[i] = N + 1;
}
for(int i = N - 1 ; i > 1 ; --i) {
if(suf[i] - i < (N + 1 - i) && (N + 1 - i) % (suf[i] - i) == 0) vis[i - 1] = 1;
}
for(int i = 2 ; i <= N ; ++i) {
if(i - nxt[i] < i && i % (i - nxt[i]) == 0) vis[i] = 1;
}
for(int i = 1 ; i < N ; ++i) {
if(!vis[i]) ++cnt;
}
puts("2");out(cnt);enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
【AtCoder】ARC060的更多相关文章
- 【AtCoder】ARC092 D - Two Sequences
[题目]AtCoder Regular Contest 092 D - Two Sequences [题意]给定n个数的数组A和数组B,求所有A[i]+B[j]的异或和(1<=i,j<=n ...
- 【Atcoder】CODE FESTIVAL 2017 qual A D - Four Coloring
[题意]给定h,w,d,要求构造矩阵h*w满足任意两个曼哈顿距离为d的点都不同色,染四色. [算法]结论+矩阵变换 [题解] 曼哈顿距离是一个立着的正方形,不方便处理.d=|xi-xj|+|yi-yj ...
- 【AtCoder】ARC 081 E - Don't Be a Subsequence
[题意]给定长度为n(<=2*10^5)的字符串,求最短的字典序最小的非子序列字符串. http://arc081.contest.atcoder.jp/tasks/arc081_c [算法]字 ...
- 【AtCoder】AGC022 F - Leftmost Ball 计数DP
[题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...
- 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT
[题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...
- 【AtCoder】ARC067 F - Yakiniku Restaurants 单调栈+矩阵差分
[题目]F - Yakiniku Restaurants [题意]给定n和m,有n个饭店和m张票,给出Ai表示从饭店i到i+1的距离,给出矩阵B(i,j)表示在第i家饭店使用票j的收益,求任选起点和终 ...
- 【AtCoder】ARC095 E - Symmetric Grid 模拟
[题目]E - Symmetric Grid [题意]给定n*m的小写字母矩阵,求是否能通过若干行互换和列互换使得矩阵中心对称.n,m<=12. [算法]模拟 [题解]首先行列操作独立,如果已确 ...
- 【Atcoder】AGC022 C - Remainder Game 搜索
[题目]C - Remainder Game [题意]给定n个数字的序列A,每次可以选择一个数字k并选择一些数字对k取模,花费2^k的代价.要求最终变成序列B,求最小代价或无解.n<=50,0& ...
- 【Atcoder】AGC 020 B - Ice Rink Game 递推
[题意]n个人进行游戏,每轮只保留最大的a[i]倍数的人,最后一轮过后剩余2人,求最小和最大的n,或-1.n<=10^5. [算法]递推||二分 [题解]令L(i),R(i)表示第i轮过后的最小 ...
随机推荐
- XML、HTML、XHTML的关系
标记语言 XML.HTML.XHTML这三者都有ML.ML(Markup Language)标记语言在维基百科中的解释是: 一种将文本以及文本相关的信息结合起来,展示出关于文档结构和数据处理细节的计算 ...
- 2018 Nowcoder Multi-University Training Contest 10
Practice Link J. Rikka with Nickname 题意: 给出\(n\)个字符串,要求依次合并两个串\(s, t\),满足将\(t\)合并到\(s\)中变成\(r\),使得\( ...
- Windows 消息以及消息处理算法--线程和消息队列详解
Windows以消息驱动的方式,使得线程能够通过处理消息来响应外界. Windows 为每个需要接受消息和处理消息的线程建立消息队列(包括发送消息队列,登记消息队列,输入消息队列,响应消息队列),其中 ...
- 内存管理2-set方法的内存管理
1.对象之间的内存管理: 每个学生都有一本书 book类 @price 学生类 @age @book -------------------- #import "book.h" ...
- 消息中间件MQ
消息中间件MQ:为方便预览,将思维导图上传至印象笔记,博客园直接上传图片受限于图片大小 https://app.yinxiang.com/shard/s24/nl/27262531/c3e137a5- ...
- Server2012R2实现活动目录(Active Directory)双域控制器互为冗余
在活动目录中部署两台主控域控制器,两台域控制器互为冗余. Server 2012 R2新建活动目录和DC refer to: https://www.cnblogs.com/jfzhu/p/40061 ...
- linux tftp配置 (Ubuntu18.04)
安装tftp客户端和服务器sudo apt-get install tftp-hpa tftpd-hpa xinetdtftp-hpa是客户端tftpd-hpa是服务器 配置文件 sudo vim / ...
- route按需加载的3种方式:vue异步组件、es提案的import()、webpack的require.ensure()
1. vue异步组件技术 vue-router配置路由,使用vue的异步组件技术,可以实现按需加载. 但是,这种情况下一个组件生成一个js文件.举例如下: { path: '/promisedemo' ...
- SQL-W3School-高级:SQL Date 函数
ylbtech-SQL-W3School-高级:SQL Date 函数 1.返回顶部 1. SQL 日期 当我们处理日期时,最难的任务恐怕是确保所插入的日期的格式,与数据库中日期列的格式相匹配. 只要 ...
- Python 数据库的Connection、Cursor两大对象
Python 数据库的Connection.Cursor两大对象 pymysql是Python中操作MySQL的模块,其使用方法和py2的MySQLdb几乎相同. Python 数据库图解流程 Con ...