【AtCoder】CADDi 2018
C - Product and GCD
题解
直接分解质因数,然后gcd每次多一个质因数均摊到每个\(N\)上的个数
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
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,P;
int64 g = 1;
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(N);read(P);
if(N == 1) {out(P);enter;return 0;}
for(int64 i = 2 ; i <= P / i ; ++i) {
if(P % i == 0) {
int64 cnt = 0;
while(P % i == 0) {P /= i;++cnt;}
int64 k = cnt / N;
while(k--) g *= i;
}
}
out(g);enter;
return 0;
}
D - Harlequin
题解
这题简直了,比赛完两分钟就想出来,比赛时候硬是怎么也想不出来
如果全是偶数肯定后手必胜,因为先手在哪个堆拿一个后手在这个堆跟一个
如果某个堆是奇数先手可以把局面转变为全是偶数然后自己当后手
所以有奇数先手必胜
全是偶数后手必胜
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
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;
int a[MAXN],cnt[2];
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(N);
for(int i = 1 ; i <= N ; ++i) read(a[i]);
for(int i = 1 ; i <= N ; ++i) {
if(a[i] & 1) {puts("first");return 0;}
}
puts("second");
return 0;
}
E - Negative Doubling
题解
有点麻烦的一道题,想起来容易写起来不容易
就是我对于每个序列肯定是要求不断乘4使得序列不降,正着反着都要算
就说从\(1-N\)不降
我们从后往前加数
如果加的这个数比它的后一个小,那么\(dp[i] = dp[i + 1]\)
否则后一个数肯定会变大,后一个数变大会引起之后的一些值变大,如果和后面相连且已经增加过的位置都会同时加上这个数,同时这个增加了还会使后面没有增加过的位置增加
这个可以用链表维护,因为每个点被增加后就被删除了,所以链表维护每次暴力更新就好
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
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,val[MAXN],pre[MAXN],suf[MAXN];
int64 a[MAXN],dp[2][MAXN],ans;
void Init() {
read(N);
for(int i = 1 ; i <= N ; ++i) read(a[i]);
}
void Solve() {
suf[0] = 1;
for(int i = 1 ; i <= N ; ++i) suf[i] = i + 1,pre[i] = i - 1;
pre[N + 1] = N;
for(int i = N ; i > 1 ; --i) {
if(a[i] >= a[i - 1]) {
int64 t = a[i];
while(t / 4 >= a[i - 1]) {val[i] -= 2; t /= 4;}
}
else {
int64 t = a[i];
while(t < a[i - 1]) {val[i] += 2; t *= 4;}
}
}
for(int i = N - 1 ; i >= 1 ; --i) {
dp[0][i] = dp[0][i + 1];
if(a[i] > a[i + 1]) {
int t = val[i + 1],p = i + 1;
while(1) {
dp[0][i] += 1LL * t * (suf[p] - p);
if(suf[p] - 1 != p) val[suf[p] - 1] += t;
if(val[suf[p] - 1] + val[suf[p]] <= 0) break;
if(suf[p] == N + 1) break;
t = val[suf[p] - 1] + val[suf[p]];
p = suf[p];val[p] = t;
pre[suf[p]] = pre[p];suf[pre[p]] = suf[p];
}
pre[suf[i + 1]] = pre[i + 1];
suf[pre[i + 1]] = suf[i + 1];
}
}
suf[0] = 1;
for(int i = 1 ; i <= N ; ++i) pre[i] = i - 1,suf[i] = i + 1;
pre[N + 1] = N;
memset(val,0,sizeof(val));
for(int i = 1 ; i < N ; ++i) {
if(a[i] < a[i + 1]) {
int64 t = a[i];
while(t < a[i + 1]) {t *= 4;val[i] += 2;}
}
else {
int64 t = a[i];
while(t / 4 >= a[i + 1]) {t /= 4;val[i] -= 2;}
}
}
for(int i = 2 ; i <= N ; ++i) {
dp[1][i] = dp[1][i - 1];
if(a[i] > a[i - 1]) {
int t = val[i - 1],p = i - 1;
while(1) {
dp[1][i] += 1LL * t * (p - pre[p]);
if(pre[p] + 1 != p) val[pre[p] + 1] += t;
if(val[pre[p] + 1] + val[pre[p]] <= 0) break;
if(pre[p] == 0) break;
t = val[pre[p] + 1] + val[pre[p]];
p = pre[p];val[p] = t;
pre[suf[p]] = pre[p];suf[pre[p]] = suf[p];
}
pre[suf[i - 1]] = pre[i - 1];
suf[pre[i - 1]] = suf[i - 1];
}
}
int64 ans = dp[0][1];
for(int i = 1 ; i <= N ; ++i) {
ans = min(dp[1][i] + i + dp[0][i + 1],ans);
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
return 0;
}
F - Square
题解
我们冷静一下很容易发现如果\(abs(x - y) > 2\)那么\((x,y)\)上的数加上\((y,x)\)的数肯定是偶数
这样的话我们统计一下没被占的对数,以及如果两个位置都有值是否合法,假如对数是\(cnt\),最后的答案要乘上\(2^cnt\)
这样的话我们只要对中间那一段\(abs(x - y) <= 2\)的部分dp就好了
我们记录中轴线上的点,中轴上的点可以确定\((i - 1,i + 1)\)和\((i + 1,i - 1)\)的奇偶性
\((i - 1,i - 1)\)和\((i,i)\)可以确定\((i - 1,i)\)和\((i,i - 1)\)的奇偶性,判一下就行
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
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);
}
const int MOD = 998244353;
int N,M;
int a[MAXN],b[MAXN],c[MAXN],dp[MAXN][2];
int d[MAXN][4][4];
map<pii,int> zz;
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
void Init() {
read(N);read(M);
for(int i = 1 ; i <= N ; ++i) memset(d[i],-1,sizeof(d[i]));
for(int i = 1 ; i <= M ; ++i) {
read(a[i]);read(b[i]);read(c[i]);
zz[mp(a[i],b[i])] = c[i];
if(abs(a[i] - b[i]) <= 2) {
int s = a[i],t = b[i];
if(s > t) swap(s,t);
for(int k = s - 1 ; k <= t + 1 ; ++k) {
if(k < 0 || k > N) continue;
if(abs(a[i] - k) <= 1 && abs(b[i] - k) <= 1) {
d[k][2 - (k - a[i])][2 - (k - b[i])] = c[i];
}
}
}
}
}
void Solve() {
int64 cnt = 1LL * N * N;
if(N == 2) cnt = 0;
else cnt -= (N - 3) * 5 + 9;
cnt /= 2;
for(int i = 1 ; i <= M ; ++i) {
if(abs(b[i] - a[i]) <= 2) continue;
if(zz.count(mp(b[i],a[i]))) {
if((zz[mp(b[i],a[i])] + c[i]) & 1) {puts("0");return;}
if(a[i] < b[i]) --cnt;
}
else --cnt;
}
int ans = fpow(2,cnt % (MOD - 1));
if(zz.count(mp(1,1))) dp[1][zz[mp(1,1)]] = 1;
else dp[1][0] = dp[1][1] = 1;
for(int i = 2 ; i <= N ; ++i) {
int t = 1;
if(d[i][1][2] == -1 && d[i][2][1] == -1) t = mul(t,2);
if(d[i][1][3] == -1 && d[i][3][1] == -1 && i != N) t = mul(t,2);
for(int k = 0 ; k <= 1 ; ++k) {
if(d[i][2][2] != -1 && d[i][2][2] != k) continue;
if(d[i][1][3] != -1 && d[i][3][1] != -1 && (d[i][1][3] ^ d[i][3][1]) != k) continue;
for(int h = 0 ; h <= 1 ; ++h) {
if(!dp[i - 1][h]) continue;
if(d[i][1][2] != -1 && d[i][2][1] != -1 && (d[i][2][1] ^ d[i][1][2]) != (k ^ h)) continue;
dp[i][k] = inc(dp[i][k],mul(t,dp[i - 1][h]));
}
}
}
ans = mul(ans,inc(dp[N][0],dp[N][1]));
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
return 0;
}
【AtCoder】CADDi 2018的更多相关文章
- 【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轮过后的最小 ...
随机推荐
- 【codevs1690】开关灯 (线段树 区间修改+区间求和 (标记))
[codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...
- Javascript实现倒计时和根据某时间开始计算时间
JavaScript 代码 <script type="text/javascript"> var time_start = new Date('2018','7',' ...
- 【BZOJ1228】[SDOI2009]E&D(博弈论)
[BZOJ1228][SDOI2009]E&D(博弈论) 题面 BZOJ 洛谷 题解 这种打表找规律的题目真的不知道可以说什么好啊... #include<iostream> #i ...
- 【洛谷P3916】图的遍历
题目大意:给定一个 N 个点,M 条边的有向图,求每个点能够到达的节点的最大编号是多少. 题解:因为题中所给图不一定是一个 DAG,因此无法进行按照拓扑序来动态规划,需要另辟蹊径.由于求的是每个节点能 ...
- CDN中,字体文件的跨域问题和解决
@font-face是CSS3中的一个特性,可以把自己定义的Web字体嵌入到网页中,随着@font-face,越来越多的网页采用字体图标作为网页中的小图形. 比如Bootstrap就采用了Glyphi ...
- unp的hello word
最近无聊,再学学unp吧.学学网络编程.下面是一个demo: intro/daytimetcpcli.c #include "unp.h" int main(int argc, c ...
- 第5月第15天 php email
1. <?php require_once "Mail.php"; $from = "luckyeggs<fuping304@163.com>" ...
- DataTable转Json(兼容easyUI特殊json分页)
用法:上述方法是DataTable的扩展方法:静态类静态方法,变量前用this (一)ps:普通datatable转标准json DataTable dt = 获取db中的datatable数据. s ...
- 转载 python多重继承C3算法
备注:O==object 2.python-C3算法解析: #C3 定义引用开始 C3 算法:MRO是一个有序列表L,在类被创建时就计算出来. L(Child(Base1,Base2)) = [ Ch ...
- 分模块开发创建dao子模块——(七)
1.选中父工程右键新建maven module