Codeforces Round #319 (Div. 2)
不要想复杂,第一题就是纯暴力
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f; int main(void) {
int n, x; scanf ("%d%d", &n, &x);
int ans = 0;
for (int i=1; i<=n; ++i) {
if (x % i == 0) {
int y = x / i;
if (1 <= y && y <= n) ans++;
}
}
printf ("%d\n", ans); return 0;
}
题意:在1~n之间猜一个数字x,可以问一些问题,x是否能整除y,问最少要问多少问题才能确定x
分析:我的方法就是把2~n的数字的质因数筛选出来,由此可以判断一些素数和不同素数的乘积。但是这样遗漏了平方数,比如能确定x整除3,结果可能是3也可能是9,所以最后把质因数的幂也放进答案
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <map>
using namespace std; const int N = 1e3 + 10;
const int INF = 0x3f3f3f3f;
map<int, int> mp;
vector<int> ans; bool is_prime(int x) {
if (x == 2 || x == 3) return true;
if (x % 6 != 1 && x % 6 != 5) return false;
for (int i=5; i*i<=x; i+=6) {
if (x % i == 0 || x % (i + 2) == 0) return false;
}
return true;
} void factorize(int x) {
for (int i=2; i*i<=x; ++i) {
while (x % i == 0) {
if (!mp[x/i] && x / i != 1 && is_prime (x / i)) {
ans.push_back (x/i); mp[x/i] = 1;
}
x /= i;
}
}
if (x != 1) {
if (!mp[x] && is_prime (x)) {
ans.push_back (x); mp[x] = 1;
}
}
} int main(void) {
int n; scanf ("%d", &n);
mp.clear (); ans.clear ();
for (int i=n; i>1; --i) {
factorize (i);
} int sz = ans.size ();
for (int i=0; i<sz; ++i) {
int x = ans[i]; int t = x * x;
while (t <= n) {
if (!mp[t]) {
ans.push_back (t);
}
t *= x;
}
}
sort (ans.begin (), ans.end ());
sz = ans.size ();
printf ("%d\n", sz);
for (int i=0; i<sz; ++i) {
printf ("%d%c", ans[i], i == sz - 1 ? '\n' : ' ');
} return 0;
}
DP+抽屉原理 B - Modulo Sum
题意:问n个数字能否选出几个数字使得它们的和能整除m
分析:我觉得这是C题的难度。首先能想到同余模定理,对每个数字先取模。当n <= m时,用dp做,dp[i][j] 表示前i个数字,和取模后为j的方案有没有,那么就像01背包一样对与ai 取或不取,结果是dp[n][0]
当n > m时,官方题解是说弄个前缀和,根据抽屉原理,一定存在suml == sumr,那么sum (l + 1, r) == 0,也就是一定是YES了,这个我不会证明。戳开别人的代码发现可以用set来记录suml sumrd的值,如果那么根据上述一定是YES。
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
using namespace std; typedef long long ll;
const int N = 1e6 + 10;
const int M = 1e3 + 10;
const int INF = 0x3f3f3f3f;
int a[N];
bool dp[M][M];
set<int> S; int main(void) {
int n, m; scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i]); a[i] %= m;
} bool flag = false;
memset (dp, false, sizeof (dp));
if (n <= m) {
for (int i=1; i<=n; ++i) {
dp[i][a[i]] = true;
}
for (int i=2; i<=n; ++i) {
for (int j=0; j<m; ++j) {
dp[i][j] |= dp[i-1][j];
int pre = (j - a[i] + m) % m;
dp[i][j] |= dp[i-1][pre];
}
}
flag = dp[n][0];
}
else {
S.clear ();
int sum = 0;
for (int i=1; i<=n; ++i) {
sum = (sum + a[i]) % m;
if (S.find (sum) != S.end ()) flag = true;
S.insert (sum);
}
// flag = true;
} puts (flag ? "YES" : "NO"); return 0;
}
Codeforces Round #319 (Div. 2)的更多相关文章
- Codeforces Round 319 # div.1 & 2 解题报告
Div. 2 Multiplication Table (577A) 题意: 给定n行n列的方阵,第i行第j列的数就是i*j,问有多少个格子上的数恰为x. 1<=n<=10^5, 1< ...
- Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造
B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...
- Codeforces Round #319 (Div. 1) C. Points on Plane 分块
C. Points on Plane Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/pro ...
- Codeforces Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学
C. Vasya and Petya's Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
- Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题
A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...
- 构造+分块思想 Codeforces Round #319 (Div. 1) C
http://codeforces.com/contest/576/problem/C 题目大意: 给你一个曼哈顿距离的图,然后要求你找到一个链,链穿了所有的点 然后要求这链的长度<=25*10 ...
- Codeforces Round #319 (Div. 2) E - Points on Plane
题目大意:在一个平面里有n个点,点坐标的值在1-1e6之间,让你给出一个遍历所有点的顺序,要求每个点走一次,且 曼哈顿距离之和小于25*1e8. 思路:想了一会就有了思路,我们可以把1e6的x,y坐标 ...
- Codeforces Round #319 (Div. 2) D - Invariance of Tree
Invariance of Tree 题目大意:给你一个有1-n组成的序列p,让你构造一棵树,如果节点a和b之间有一条边,则p[a]和p[b]之间也有一条边. 思路:没啥思路,看了题解菜爆. 我们可以 ...
- Codeforces Round #319 (Div. 2) D
E A tree of size n is an undirected connected graph consisting of n vertices without cycles. Conside ...
随机推荐
- iOS simulator+Appium
Why are you trying to run iOS automation on a real device? That's a bad idea. iOS Automation on a re ...
- bash shell最基本的语法
1 shell语句的基本构成 shell每个基本的构成元素之间都相隔一个空格. 比如[ -e file ],[.-e.file.]这四个基本元素之间都相隔了一个空格. 同样的道理[ ! -e file ...
- mysql 发生系统错误1067
一般是由配置文件错误语法不正确引起的,如my.ini本人在mysql mysql-5.6.29-winx64 配置过程中遇到“发生系统错误1067”主要由于下面两个目录写的格式不正确引起的正确写法如下 ...
- linux初级学习笔记十:linux grep及正则表达式!(视频序号:04_4)
本节学习的命令:grep 本节学习的技能: grep对文本的匹配 正则表达式的使用 知识点十:grep及正则表达式(4_4) grep,egrep,fgrep: grep: 根据模式搜索文本,并将符合 ...
- OpenCV——PS滤镜算法之 Ellipsoid (凹陷)
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- VS 一些配置设置
/************************************************************************ * VS 一些配置设置 * 说明: * 最近要用到C ...
- 「网络流24题」「LuoguP4016」 负载平衡问题
Description GGG 公司有 nnn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 nnn 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. ...
- 【SDOI2009】SuperGCD
[题目链接] 点击打开链接 [算法] 1.关于求最大公约数的算法 若使用辗转相除法,那么显然会超时 不妨这样思考 : 要求gcd(a,b), 若a为偶数,b为偶数,则gcd(a,b) = 2 * gc ...
- javascript之this指向
情况一: 如果一个函数中有this,但是没有被上一级调用,this指向window 例: function a(){ var num='11'; console.log(this.num); //u ...
- Watir: Win32ole对于excel某些指令无法操作的时候有如下解决方案
Similar Threads 1. WIN32OLE - failed to create WIN32OLE 2. WIN32OLE#[] and WIN32OLE#[]= method in Ru ...