Codeforces Round #319 (Div. 2) (ABCE题解)

2024-03-20 13:48

本文主要是介绍Codeforces Round #319 (Div. 2) (ABCE题解),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

比赛链接:http://codeforces.com/contest/577


A. Multiplication Table
time limit per test:1 second
memory limit per test:256 megabytes

Let's consider a table consisting of n rows and n columns. The cell located at the intersection of i-th row and j-th column contains number i × j. The rows and columns are numbered starting from 1.

You are given a positive integer x. Your task is to count the number of cells in a table that contain number x.

Input

The single line contains numbers n and x (1 ≤ n ≤ 105, 1 ≤ x ≤ 109) — the size of the table and the number that we are looking for in the table.

Output

Print a single number: the number of times x occurs in the table.

Sample test(s)
Input
10 5
Output
2
Input
6 12
Output
4
Input
5 13
Output
0
Note

A table for the second sample test is given below. The occurrences of number 12 are marked bold.



题目大意:n*n的矩阵,第i行是i为首项,i为公差的等差数列,求x在矩阵里出现了几次

题目分析:枚举约数判断

#include <cstdio>int main()
{int n, x, ans = 0;scanf("%d %d", &n, &x);for(int i = 1; i * i <= x; i ++){if(x % i == 0 && x / i <= n && i <= n){if(i * i == x)ans += 1;elseans += 2;}}printf("%d\n", ans);
}



B. Modulo Sum
time limit per test:2 seconds
memory limit per test:256 megabytes

You are given a sequence of numbers a1, a2, ..., an, and a number m.

Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m.

Input

The first line contains two numbers, n and m (1 ≤ n ≤ 106, 2 ≤ m ≤ 103) — the size of the original sequence and the number such that sum should be divisible by it.

The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

Output

In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.

Sample test(s)
Input
3 5
1 2 3
Output
YES
Input
1 6
5
Output
NO
Input
4 6
3 1 1 3
Output
YES
Input
6 6
5 5 5 5 5 5
Output
YES
Note

In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.

In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.

In the third sample test you need to choose two numbers 3 on the ends.

In the fourth sample test you can take the whole subsequence.


题目大意:给n个数字,问存不存在一个子序列的和是m的倍数


题目分析:和POJ 1745差不多,主要是这题的n太大不能看二维,但是当前状态可以由之前的累计状态推出,所以开两个数组一个记录累计的余数,一个记录当前的余数,a[i] = true,表示当前状态出现余数为i的情况,然后累计给b数组,一旦b[0] = true,则退出

#include <cstdio>
#include <cstring>
int const MAX = 1e3 + 5;
bool a[MAX], b[MAX], flag;int main()
{int n, m;scanf("%d %d", &n, &m);flag = false;for(int i = 0; i < n; i++){int tmp;scanf("%d", &tmp);memset(a, false, sizeof(a));a[tmp % m] = true;for(int j = 0; j < m; j++){if(b[j]){a[tmp % m] = true;a[(tmp + j) % m] = true;}}for(int j = 0; j < m; j++){if(a[j])b[j] = a[j];if(b[0]){flag = true;break;}}if(flag)break;}printf("%s\n", flag ? "YES" : "NO");
}


C. Vasya and Petya's Game
time limit per test:1 second
memory limit per test:256 megabytes
Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number.

Petya can ask questions like: "Is the unknown number divisible by number y?".

The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of.

Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about.

Input

A single line contains number n (1 ≤ n ≤ 103).

Output

Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n).

If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.

Sample test(s)
Input
4
Output
3
2 4 3 
Input
6
Output
4
2 4 3 5 
Note

The sequence from the answer to the first sample test is actually correct.

If the unknown number is not divisible by one of the sequence numbers, it is equal to 1.

If the unknown number is divisible by 4, it is 4.

If the unknown number is divisible by 3, then the unknown number is 3.

Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.


题目大意:两个人做游戏,A从1-n中选一个数字x,B通过询问一些数字对x的整除性判断x是什么(即该数字能不能被x整除),现在求B至少问多少次,每次问什么才能做到不管A从1-n中选的是哪个数字,他都能判断出来


题目分析:分析可以发现答案就是唯一分解后只有一种素数的数,证明可以通过唯一分解定理,脑补一下就好,注意0的时候只输出一个0

#include <cstdio>
int ans[1005];int main()
{int n, cnt = 0;scanf("%d", &n);for(int i = 2; i <= n; i++){bool flag = false;for(int j = 2; j <= i; j++){int tmp = i;while(tmp % j == 0){flag = true;tmp /= j;}if(tmp == 1)ans[cnt ++] = i;if(flag)break;}}printf("%d\n", cnt);for(int i = 0; i < cnt - 1; i++)printf("%d ", ans[i]);if(cnt)printf("%d\n", ans[cnt - 1]);
}



E. Points on Plane
time limit per test:2 seconds
memory limit per test:256 megabytes
On a plane are n points (xi, yi) with integer coordinates between 0 and 106. The distance between the two points with numbers a and b is said to be the following value: (the distance calculated by such formula is called Manhattan distance).

We call a hamiltonian path to be some permutation pi of numbers from 1 to n. We say that the length of this path is value .

Find some hamiltonian path with a length of no more than 25 × 108. Note that you do not have to minimize the path length.

Input

The first line contains integer n (1 ≤ n ≤ 106).

The i + 1-th line contains the coordinates of the i-th point: xi and yi (0 ≤ xi, yi ≤ 106).

It is guaranteed that no two points coincide.

Output

Print the permutation of numbers pi from 1 to n — the sought Hamiltonian path. The permutation must meet the inequality .

If there are multiple possible answers, print any of them.

It is guaranteed that the answer exists.

Sample test(s)
Input
5
0 7
8 10
3 4
5 0
9 12
Output
4 3 1 2 5 
Note

In the sample test the total distance is:

(|5 - 3| + |0 - 4|) + (|3 - 0| + |4 - 7|) + (|0 - 8| + |7 - 10|) + (|8 - 9| + |10 - 12|) = 2 + 4 + 3 + 3 + 8 + 3 + 1 + 2 = 26


题目大意:求一个排列,使得dist(pi, pi+1)的值小于等于25*10^8


题目分析:考虑把大坐标系竖直分成1000份,则竖直方向上最多走2*10^6*10^3 = 2e9,再考虑水平方向,再当前竖直块中最多移动2*1000次,从当前块移动到下一块最多走1000,一块中的移动就算3*1000,一共10^3块,因此水平最多走3e6,加起来肯定小于25e8,因此按纵坐标排序,给每块标号,当块标号为奇数时,从下往上,偶数时从上往下,画个图就看出来了

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector < pair<int, int> > vt[1005];int main()
{int n, x, y;scanf("%d", &n);for(int i = 1; i <= n; i++){scanf("%d %d", &x, &y);vt[x / 1000].push_back(make_pair(y, i));}for(int i = 0; i <= 1000; i++){sort(vt[i].begin(), vt[i].end());if(i & 1) reverse(vt[i].begin(), vt[i].end());int sz = vt[i].size();for(int j = 0; j < sz; j++)printf("%d ", vt[i][j].second);}printf("\n");
}



这篇关于Codeforces Round #319 (Div. 2) (ABCE题解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/829633

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述