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

54 篇文章 0 订阅
53 篇文章 0 订阅

比赛链接: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;
            else
                ans += 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");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值