【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轮过后的最小 ...
随机推荐
- spark学习笔记之面试
spark常见面试 map与flatMap的区别 题:以下代码输出有什么不同 val books=List("Hadoop","Hive","HDFS ...
- c++ 判断是元音还是辅音
#include <iostream> using namespace std; int main() { char c; int isLowercaseVowel, isUppercas ...
- scrapy框架之spider
爬取流程 Spider类定义如何爬取指定的一个或多个网站,包括是否要跟进网页里的链接和如何提取网页内容中的数据. 爬取的过程是类似以下步骤的循环: 1.通过指定的初始URL初始化Request,并指定 ...
- 微信小程序开发常见坑
前段时间稍微涉猎了微信小程序开发,踩了一些坑,在此总结出来,希望能为小伙伴们提供一点帮助. 页面跳转 对于页面跳转,可能习惯性想到wx.navigateTo,但是在跳转到目标页面是一个tab时,此接口 ...
- vue实现穿梭框效果
vue实现穿梭框效果 一.总结 一句话总结: 用两个数组分别记录左右框框里面的值,用两个数组绑定checkbox,用来记录选中的checkbox值,根据选中的checkbox的值实现删除增加即可 1. ...
- 《你不知道的JavaScript(上)》笔记——动态作用域
动态作用域让作用域作为一个在运行时就被动态确定的形式, 而不是在写代码时进行静态确定的形式.动态作用域并不关心函数和作用域是如何声明以及在何处声明的, 只关心它们从何处调用. 换句话说, 作用域链是基 ...
- vsCode创建自己的代码模板
(一)新建html快捷键 当我们想在VSCode中新建html代码时,可以 输入! 然后回车或者Tab即可自动生成一个html文件模板,效果如下: 效果如下: 但是有时候我们需要创建一些个性化的,可能 ...
- 《Flutter 实战》开源电子书
<Flutter 实战>开源电子书 转 https://blog.csdn.net/OQjya206rsQ71/article/details/86619630 关于 Flutter ...
- hadoop2.7.7+habse2.0.5+zookeeper3.4.14+hive2.3.5单机安装
环境 腾讯云centos7 1.hadoop下载 http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.7.7/hadoop-2.7.7.tar ...
- @Value()读取配置文件属性,读出值为null的问题
一.问题描述 自定义一个Filter如下: @Component public class JwtFilter extends GenericFilterBean{ @Value("${jw ...