Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)

来源:互联网 发布:信用卡账单修改软件 编辑:程序博客网 时间:2024/05/16 01:49
B. Divisiblity of Differences
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.

Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset.

Input

First line contains three integers n, k and m (2 ≤ k ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.

Second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the numbers in the multiset.

Output

If it is not possible to select k numbers in the desired way, output «No» (without the quotes).

Otherwise, in the first line of output print «Yes» (without the quotes). In the second line print k integers b1, b2, ..., bk — the selected numbers. If there are multiple possible solutions, print any of them.

Examples
input
3 2 31 8 4
output
Yes1 4 
input
3 3 31 8 4
output
No
input
4 3 52 7 7 7
output
Yes2 7 7 

题意是给你n个数,要求你从中取k个数,这k个数两两之差可以被m整除。


两个数之差可以被一个数m整除,意味着这两个数%k的值会相同。

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 100000;inline int get(){char c;while((c = getchar()) < '0' || c > '9');int cnt = c - '0';while((c = getchar()) >= '0' && c <= '9') cnt = cnt * 10 + c - '0';return cnt;}int N,K,M;int a[MAXN+10];int b[MAXN+10];int cnt[MAXN+10];int main(){#ifdef lwy/*#elsefreopen(".in","r",stdin);freopen(".out","w",stdout);*/#endif scanf("%d %d %d",&N,&K,&M); for(int i = 1; i <= N; i++){ scanf("%d",&a[i]); b[i] = a[i] % M;}for(int i = 1; i <= N; i++){cnt[b[i]]++;}for(int i = 0; i <= M-1; i++){if(cnt[i] >= K){printf("Yes\n");int tot=0;for(int j = 1; j <= N; j++){if(b[j] == i) printf("%d ",a[j]),tot++;if(tot == K) return 0;}}}printf("No");return 0;}


C. Classroom Watch
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system.

Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova.

Input

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

Output

In the first line print one integer k — number of different values of x satisfying the condition.

In next k lines print these values in ascending order.

Examples
input
21
output
115
input
20
output
0
Note

In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21.

In the second test case there are no such x.


题意是给你一个数n,要你求出是否有一个数,它与它的各个位数之和等于n,如果有多解,则按从小到大顺序输出。

这是题需要审题的题目。

注意到n<=10^9,那么各个位数之和绝对不会大于81,从n-81到n枚举下答案就行了。

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 0;inline int get(){char c;while((c = getchar()) < '0' || c > '9');int cnt = c - '0';while((c = getchar()) >= '0' && c <= '9') cnt = cnt * 10 + c - '0';return cnt;}int n;int ans[100],tot=0;int main(){#ifdef lwy/*#elsefreopen(".in","r",stdin);freopen(".out","w",stdout);*/#endifscanf("%d",&n);int t;t = max(n-90,1);for(int i = n; i >= t; i--){int k=i,sum=0;while(k != 0){sum += (k % 10);k /= 10;}sum += i;if(sum == n){tot++;ans[tot] = i;}}printf("%d\n",tot);if(tot > 0){for(int i = tot; i >= 1; i--){printf("%d ",ans[i]);}}return 0;}


D. Sorting the Coins
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins together. Their favorite occupation is to sort collections of coins. Sasha likes having things in order, that is why he wants his coins to be arranged in a row in such a way that firstly come coins out of circulation, and then come coins still in circulation.

For arranging coins Dima uses the following algorithm. One step of his algorithm looks like the following:

  1. He looks through all the coins from left to right;
  2. If he sees that the i-th coin is still in circulation, and (i + 1)-th coin is already out of circulation, he exchanges these two coins and continues watching coins from (i + 1)-th.

Dima repeats the procedure above until it happens that no two coins were exchanged during this procedure. Dima calls hardness of ordering the number of steps required for him according to the algorithm above to sort the sequence, e.g. the number of times he looks through the coins from the very beginning. For example, for the ordered sequence hardness of ordering equals one.

Today Sasha invited Dima and proposed him a game. First he puts n coins in a row, all of them are out of circulation. Then Sasha chooses one of the coins out of circulation and replaces it with a coin in circulation for n times. During this process Sasha constantly asks Dima what is the hardness of ordering of the sequence.

The task is more complicated because Dima should not touch the coins and he should determine hardness of ordering in his mind. Help Dima with this task.

Input

The first line contains single integer n (1 ≤ n ≤ 300 000) — number of coins that Sasha puts behind Dima.

Second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position p1, then coin located at position p2 and so on. Coins are numbered from left to right.

Output

Print n + 1 numbers a0, a1, ..., an, where a0 is a hardness of ordering at the beginning, a1 is a hardness of ordering after the first replacement and so on.

Examples
input
41 3 4 2
output
1 2 3 2 1
input
86 8 3 4 7 2 1 5
output
1 2 2 3 4 3 4 5 1

Note

Let's denote as O coin out of circulation, and as X — coin is circulation.

At the first sample, initially in row there are coins that are not in circulation, so Dima will look through them from left to right and won't make any exchanges.

After replacement of the first coin with a coin in circulation, Dima will exchange this coin with next three times and after that he will finally look through the coins and finish the process.

XOOO  →  OOOX

After replacement of the third coin, Dima's actions look this way:

XOXO  →  OXOX  →  OOXX

After replacement of the fourth coin, Dima's actions look this way:

XOXX  →  OXXX

Finally, after replacement of the second coin, row becomes consisting of coins that are in circulation and Dima will look through coins from left to right without any exchanges.

这道题题意比较复杂,给你n个数字,每个数字代表一个位置,总长度为n,每输入一个数字,相当于给这个位置带上了标记,有标记的数字必须移动到没标记的数字后面。每输入一个数字,你都需要输出把所有带标记的位置(包括之前输入的数字)都移动到不带标记数字后所需要操作的步骤数。

操作是这样的。

把这个串从左往右扫描,如果发现位置 i 带标记 而 i+1 不带标记,那么就将这两个位置交换,直到扫描到不需要交换为止。

可以观察出,答案就是等于最后面的不带标记的位置之前的带标记的位置的个数+1

那么直接求解就很好算了。(蒟蒻居然想用链表瞎搞,强行把问题复杂化了..)

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 300000;inline int get(){char c;while((c = getchar()) < '0' || c > '9');int cnt = c - '0';while((c = getchar()) >= '0' && c <= '9') cnt = cnt * 10 + c - '0';return cnt;}int N,P;bool a[MAXN+10];int Now;int main(){#ifdef lwy/*#elsefreopen(".in","r",stdin);freopen(".out","w",stdout);*/#endifmemset(a,false,sizeof(a));scanf("%d",&N);Now = N;printf("1 ");for(int i = 1; i <= N; i ++){int k; scanf("%d",&k);a[k] = true;while(a[Now]) Now --;printf("%d ",1 + i - (N - Now));} return 0;}


阅读全文
0 0