COOK50小结
很遗憾。看到第五题的通过人数就不敢做了。待日后补上。
A题
求最长的连续子序列,使得他们满足gcd为1。
如果有相邻的两个数的gcd为1,那么整个序列的gcd值也就是1,
否则就是该序列不存在。
/*************************************************************************
> File Name: A.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月23日 星期二 13时57分22秒
> Propose:
************************************************************************/
#include <map>
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ const int MAX_N = ;
int N, A[MAX_N];
#define rep(i, n) for (int i = (1); i <= (n); i++) int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
} int main(void) {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
cin >> N;
bool flag = false;
rep (i, N) {
cin >> A[i];
if (i > && gcd(A[i], A[i-]) == ) flag = true;
}
if (flag) cout << N << endl;
else cout << - << endl;
} return ;
}
B题
统计一个每个位置向右和向下是否都没有障碍。
/*************************************************************************
> File Name: B.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月23日 星期二 14时30分39秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ char a[][];
int N, T;
bool col[][], row[][];
#define rep(i, n) for (int i = (1); i <= n; i++)
#define per(i, n) for (int i = (n); i >= 1; i--) int main(void) {
ios::sync_with_stdio(false);
cin >> T;
while (T--) {
cin >> N;
rep (i, N) rep (j, N) cin >> a[i][j], row[i][j] = col[i][j] = true; rep (i, N) per (j, N) if ((j < N && !row[i][j+]) || (a[i][j] != '.')) row[i][j] = false;
rep (j, N) per (i, N) if ((i < N && !col[i+][j]) || (a[i][j] != '.')) col[i][j] = false; int res = ;
rep (i, N) rep (j, N) if (row[i][j] && col[i][j]) res++;
cout << res << endl;
} return ;
}
C题
先预处理出每个数的所有的质因子。 并记录下每个质因子在序列中最后一次出现的位置即可。
/*************************************************************************
> File Name: C.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月23日 星期二 17时24分56秒
> Propose:
************************************************************************/
#include <set>
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ const int MAX_N = ;
int A[MAX_N], p[MAX_N], cnt, last[MAX_N*];
bool vis[MAX_N*];
#define rep(i, n) for (int i = (1); i <= n; i++) void prime() {
cnt = ;
memset(vis, false, sizeof(vis));
for (int i = ; i < ; i++) if (!vis[i]) {
p[cnt++] = i;
for (int j = *i; j < ; j += i) vis[j] = true;
}
} void read(int &res) {
res = ;
char c = ' ';
while (c < '' || c > '') c = getchar();
while (c >= '' && c <= '') res = res * + c - '', c = getchar();
} vector<int> factor[];
vector<int>::iterator it;
int main(void) {
prime();
rep (i, ) {
int x = i;
for (int j = ; p[j]*p[j] <= x; j++) {
if (x % p[j] == ) {
factor[i].push_back(p[j]);
while (x % p[j] == ) x /= p[j];
}
}
if (x > ) factor[i].push_back(x);
} int T, N;
read(T);
while (T--) {
read(N);
rep (i, N) read(A[i]); int l = , r = , res = ;
memset(last, , sizeof(last));
for (it = factor[A[l]].begin(); it != factor[A[l]].end(); ++it) last[*it] = l;
while (l <= r && r <= N) {
int m = ;
for (it = factor[A[r]].begin(); it != factor[A[r]].end(); ++it) {
if (last[*it] >= l) m = max(m, last[*it]);
last[*it] = r;
}
if ( == m) res = max(res, r - l + );
else l = m + ;
r++;
} if (res == ) puts("-1");
else printf("%d\n", res);
} return ;
}
D题
分奇数行和偶数行讨论,并得出递推式,即可用矩阵加速。
/*************************************************************************
> File Name: D.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月24日 星期三 13时20分04秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ typedef long long LL;
const int MOD = 1e9 + ;
#define rep(i, n) for (int i = (1); i <= (n); i++) struct Matrix {
LL mat[][];
int n, m;
Matrix() {}
Matrix(int n, int m):n(n), m(m) {
memset(mat, , sizeof(mat));
}
Matrix operator * (const Matrix &b) const {
Matrix c(n, b.m);
rep (i, n) rep (k, n) rep (j, b.m) c.mat[i][j] = (c.mat[i][j] + mat[i][k] * b.mat[k][j]) % MOD;
return c;
}
}; Matrix pow_mod(Matrix a, int b) {
Matrix res(a.n, a.m);
rep (i, a.n) res.mat[i][i] = ;
while (b) {
if (b & ) res = res * a;
a = a * a;
b >>= ;
}
return res;
} int main(void) {
ios::sync_with_stdio(false);
int T, N, M;
cin >> T;
while (T--) {
cin >> N >> M;
Matrix a(M, M), b(M, M);
rep (i, M) {
if (i - >= ) a.mat[i][i-] = b.mat[i][i-] = ;
if (i + <= M) a.mat[i][i+] = b.mat[i][i+] = ;
a.mat[i][i] = ;
}
Matrix res = pow_mod(a*b, (N% == ? N/- : N/));
if (N % == ) res = b * res;
Matrix f1(M, );
rep (i, M) f1.mat[i][] = ;
res = res * f1; LL ans = ;
rep (i, M) ans = (ans + res.mat[i][]) % MOD;
cout << ans << endl;
} return ;
}
E题
COOK50小结的更多相关文章
- 从零开始编写自己的C#框架(26)——小结
一直想写个总结,不过实在太忙了,所以一直拖啊拖啊,拖到现在,不过也好,有了这段时间的沉淀,发现自己又有了小小的进步.哈哈...... 原想框架开发的相关开发步骤.文档.代码.功能.部署等都简单的讲过了 ...
- Python自然语言处理工具小结
Python自然语言处理工具小结 作者:白宁超 2016年11月21日21:45:26 目录 [Python NLP]干货!详述Python NLTK下如何使用stanford NLP工具包(1) [ ...
- java单向加密算法小结(2)--MD5哈希算法
上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...
- iOS--->微信支付小结
iOS--->微信支付小结 说起支付,除了支付宝支付之外,微信支付也是我们三方支付中最重要的方式之一,承接上面总结的支付宝,接下来把微信支付也总结了一下 ***那么首先还是由公司去创建并申请使用 ...
- iOS 之UITextFiled/UITextView小结
一:编辑被键盘遮挡的问题 参考自:http://blog.csdn.net/windkisshao/article/details/21398521 1.自定方法 ,用于移动视图 -(void)mov ...
- K近邻法(KNN)原理小结
K近邻法(k-nearst neighbors,KNN)是一种很基本的机器学习方法了,在我们平常的生活中也会不自主的应用.比如,我们判断一个人的人品,只需要观察他来往最密切的几个人的人品好坏就可以得出 ...
- scikit-learn随机森林调参小结
在Bagging与随机森林算法原理小结中,我们对随机森林(Random Forest, 以下简称RF)的原理做了总结.本文就从实践的角度对RF做一个总结.重点讲述scikit-learn中RF的调参注 ...
- Bagging与随机森林算法原理小结
在集成学习原理小结中,我们讲到了集成学习有两个流派,一个是boosting派系,它的特点是各个弱学习器之间有依赖关系.另一种是bagging流派,它的特点是各个弱学习器之间没有依赖关系,可以并行拟合. ...
- scikit-learn 梯度提升树(GBDT)调参小结
在梯度提升树(GBDT)原理小结中,我们对GBDT的原理做了总结,本文我们就从scikit-learn里GBDT的类库使用方法作一个总结,主要会关注调参中的一些要点. 1. scikit-learn ...
随机推荐
- jmeter参数化遇到的问题
遇到的问题是点击运行后,察看结果树没有任何结果,且右上角的警告日志是: meter.threads.JMeterThread: Test failed! java.lang.IllegalArgume ...
- 廖雪峰Java15JDBC编程-3JDBC接口-4JDBC事务
1 数据库事务:Transaction 1.1 定义 若干SQL语句构成的一个操作序列 要么全部执行成功 要么全部执行不成功 1.2 数据库事务具有ACID特性: Atomicity:原子性 一个事务 ...
- LUOGU P4163 [SCOI2007]排列
传送门 解题思路 首先我们发现这道题s的长度很小,所以考虑点暴力的做法,状压dp或搜索.本蒟蒻搜索永远调不对,所以就写了个状压dp.因为所有s里的数都要出现一次,并且最后的答案是要求整除,那么我们设d ...
- codevs1222 信与信封的问题
二分图匹配. 先匹配一次,一定是完美匹配.然后枚举每条边,去掉它,若是不能完美匹配,这条边就必须. #include<cstdio> #include<cstring> #in ...
- BZOJ 1579 [Usaco2009 Feb]Revamping Trails 道路升级
堆优化的dijkstra. 把一个点拆成k个. 日常空间要开炸一次.. //Twenty #include<cstdio> #include<cstring> #include ...
- 正则获取html标签字符串中图片地址
html标签字符串: var htmlStr = "<div class='testClass'><img=http://www.chinanews.com/part/ho ...
- ps命令详解-转
名称:ps使用权限:所有使用者使用方式:ps [options] [--help]说明:显示瞬间行程 (process) 的动态参数:ps的参数非常多, 在此仅列出几个常用的参数并大略介绍含义-A ...
- .Net Core微服务系列--服务发现
什么是服务发现 首先我们先思考一个问题,当我们在浏览器中输入一个域名比如baidu.com,然后发生了什么才能让我们访问到百度的网页?简单来说,浏览器会首先从主机的hosts文件中查看是否有baidu ...
- 懒散惯了,该收收心了,两天了,封装了一个R0下注册表类
写得乱七八糟. 看着自己写的代码,感觉都不像自己了. 我写的代码,风格这么差了么?思路这么乱了么? 我写代码这么累么? 不像以前的我了... 这段时间,太懒散了... 该继续努 ...
- Jmeter接口测试(第二篇)
一.新建项目 1.运行Jmeter.bat打开Jmeter 2.添加线程组(测试计划->添加->Thread(users)->线程组) 3.添加HTTP请求(线程组->添加-& ...