Codeforces Round #315 (Div. 2) (ABCD题解)

2024-03-20 13:58

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

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


A. Music
time limit per test:2 seconds
memory limit per test:256 megabytes

Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk.

Unfortunately, internet is not that fast in the city of Ekaterinozavodsk and the song takes a lot of time to download. But Lesha is quite impatient. The song's duration isT seconds. Lesha downloads the first S seconds of the song and plays it. When the playback reaches the point that has not yet been downloaded, Lesha immediately plays the song from the start (the loaded part of the song stays in his phone, and the download is continued from the same place), and it happens until the song is downloaded completely and Lesha listens to it to the end. Forq seconds of real time the Internet allows you to downloadq - 1 seconds of the track.

Tell Lesha, for how many times he will start the song, including the very first start.

Input

The single line contains three integers T, S, q (2 ≤ q ≤ 104,1 ≤ S < T ≤ 105).

Output

Print a single integer — the number of times the song will be restarted.

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

In the first test, the song is played twice faster than it is downloaded, which means that during four first seconds Lesha reaches the moment that has not been downloaded, and starts the song again. After another two seconds, the song is downloaded completely, and thus, Lesha starts the song twice.

In the second test, the song is almost downloaded, and Lesha will start it only once.

In the third sample test the download finishes and Lesha finishes listening at the same moment. Note that song isn't restarted in this case.


题目大意:一个人下载歌,每q个时间单位能下载q-1个时间单位的歌,歌的长度为T,下到S的时候开始播放,如果歌还没下完且放到了还未下载的地方,则重头开始放,问一共要放多少次


题目分析:一开始从s开始,设下了cur秒后听和下的进度相同,则 s + (q - 1) / q * cur = cur,解得cur = q * s,然后从头开始,设t'秒后进度相同,则(q - 1) / q * t' + cur = t',解得t' = cur * q,可见直接拿第一次进度相同的时间乘q就是接下来每次进度相同的时间


#include <cstdio>int main()
{int T, S, q, ans = 1, cur;scanf("%d %d %d", &T, &S, &q);cur = q * S;while(cur < T){cur = q * cur;ans ++;}printf("%d\n", ans);
}


B. Inventory
time limit per test:1 second
memory limit per test:256 megabytes

Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep the track of everything.

During an audit, you were surprised to find out that the items are not numbered sequentially, and some items even share the same inventory number! There is an urgent need to fix it. You have chosen to make the numbers of the items sequential, starting with 1. Changing a number is quite a time-consuming process, and you would like to make maximum use of the current numbering.

You have been given information on current inventory numbers forn items in the company. Renumber items so that their inventory numbers form apermutation of numbers from 1 to n by changing the number of as few items as possible. Let us remind you that a set ofn numbers forms a permutation if all the numbers are in the range from 1 to n, and no two numbers are equal.

Input

The first line contains a single integer n — the number of items (1 ≤ n ≤ 105).

The second line contains n numbersa1, a2, ..., an (1 ≤ ai ≤ 105) — the initial inventory numbers of the items.

Output

Print n numbers — the final inventory numbers of the items in the order they occur in the input. If there are multiple possible answers, you may print any of them.

Sample test(s)
Input
3
1 3 2
Output
1 3 2 
Input
4
2 2 3 3
Output
2 1 3 4 
Input
1
2
Output
1 
Note

In the first test the numeration is already a permutation, so there is no need to change anything.

In the second test there are two pairs of equal numbers, in each pair you need to replace one number.

In the third test you need to replace 2 by 1, as the numbering should start from one.


题目大意:给n个数,可以改变任意个数字的大小目标是将其改成1-n的一个排列,要求改变次数最小,输出排列


题目分析:记录原始序列已经在1-n位置的数,凡是大于n的或者重复出现的数字下标 标记一下,枚举一下1-n中还有哪些数字没出现,然后按顺序修改

#include <cstdio>
#include <cstring>
int const MAX = 1e5 + 5;
int a[MAX], need[MAX];
bool has[MAX], pos[MAX];int main()
{int n, num = 0;scanf("%d", &n);memset(has, false, sizeof(has));memset(pos, false, sizeof(pos));for(int i = 0; i < n; i++){scanf("%d", &a[i]);if(!has[a[i]] && a[i] <= n)has[a[i]] = true;elsepos[i] = true;}int cnt = 0;for(int i = 1; i <= n; i++)if(!has[i])need[cnt ++] = i;int idx = 0;for(int i = 0; i < n; i++)if(pos[i])a[i] = need[idx ++];for(int i = 0; i < n - 1; i++)printf("%d ", a[i]);printf("%d\n", a[n - 1]);
}



C. Primes or Palindromes?
time limit per test:3 seconds
memory limit per test:256 megabytes

Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable properties. Help Rikhail to convince the scientific community in this!

Let us remind you that a number is called prime if it is integer larger than one, and is not divisible by any positive integer other than itself and one.

Rikhail calls a number a palindromic if it is integer, positive, and its decimal representation without leading zeros is a palindrome, i.e. reads the same from left to right and right to left.

One problem with prime numbers is that there are too many of them. Let's introduce the following notation:π(n) — the number of primes no larger thann, rub(n) — the number of palindromic numbers no larger thann. Rikhail wants to prove that there are a lot more primes than palindromic ones.

He asked you to solve the following problem: for a given value of the coefficientA find the maximum n, such that π(n) ≤ A·rub(n).

Input

The input consists of two positive integers p, q, the numerator and denominator of the fraction that is the value ofA ().

Output

If such maximum number exists, then print it. Otherwise, print"Palindromic tree is better than splay tree" (without the quotes).

Sample test(s)
Input
1 1
Output
40
Input
1 42
Output
1
Input
6 4
Output
172


题目大意:π(n) 为不大于n的素数个数,rub(n)为不大于n的回文数个数,A=p/q,现在要求最大的n,使得π(n) ≤ A·rub(n)

题目分析:预处理这两类数的个数,数组尽量往大了开吧,1e7过了

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long  
using namespace std;
int const MAX = 1e7 + 5;
int cntpr[MAX], cntpa[MAX], p[MAX];
bool prime[MAX];void get_prime()
{int pnum = 1;memset(cntpr, 0, sizeof(cntpr));memset(prime, true, sizeof(prime));for(int i = 2; i <= MAX; i++){cntpr[i] = cntpr[i - 1];if(prime[i]){cntpr[i] ++;p[pnum ++] = i;}for(int j = 1; j <= pnum && i * p[j] < MAX; j++){prime[i * p[j]] = false;if(i % p[j] == 0)break;}}
}bool judge(int x)
{char s[10];sprintf(s, "%d", x);int len = strlen(s);for(int i = 0; i < len / 2; i++)if(s[i] != s[len - i - 1])return false;return true;
}void get_palindromes()
{memset(cntpa, 0, sizeof(cntpa));for(int i = 1; i <= MAX; i++){cntpa[i] = cntpa[i - 1];if(judge(i))cntpa[i] ++;}
}int main()
{bool flag = false;int p, q;int ans = 0;get_prime();get_palindromes();scanf("%d %d", &p, &q);for(int i = 1; i <= MAX; i++){if((ll) q * cntpr[i] <= (ll) p * cntpa[i]){ans = max(ans, i);flag = true;}}if(flag)printf("%d\n", ans);elseprintf("Palindromic tree is better than splay tree\n");
}



D. Symmetric and Transitive
time limit per test:1.5 seconds
memory limit per test:256 megabytes

Little Johnny has recently learned about set theory. Now he is studying binary relations. You've probably heard the term "equivalence relation". These relations are very important in many areas of mathematics. For example, the equality of the two numbers is an equivalence relation.

A set ρ of pairs (a, b) of elements of some setA is called a binary relation on set A. For two elements a and b of the set A we say that they are in relationρ, if pair , in this case we use a notation.

Binary relation is equivalence relation, if:

  1. It is reflexive (for any a it is true that);
  2. It is symmetric (for any a, b it is true that if , then);
  3. It is transitive (if and, than).

Little Johnny is not completely a fool and he noticed that the first condition is not necessary! Here is his "proof":

Take any two elements, a and b. If , then (according to property (2)), which means (according to property (3)).

It's very simple, isn't it? However, you noticed that Johnny's "proof" is wrong, and decided to show him a lot of examples that prove him wrong.

Here's your task: count the number of binary relations over a set of size n such that they are symmetric, transitive, but not an equivalence relations (i.e. they are not reflexive).

Since their number may be very large (not 0, according to Little Johnny), print the remainder of integer division of this number by109 + 7.

Input

A single line contains a single integer n(1 ≤ n ≤ 4000).

Output

In a single line print the answer to the problem modulo 109 + 7.

Sample test(s)
Input
1
Output
1
Input
2
Output
3
Input
3
Output
10
Note

If n = 1 there is only one such relation — an empty one, i.e.. In other words, for a single elementx of set A the following is hold:.

If n = 2 there are three such relations. Let's assume that setA consists of two elements, x and y. Then the valid relations are,ρ = {(x, x)}, ρ = {(y, y)}. It is easy to see that the three listed binary relations are symmetric and transitive relations, but they are not equivalence relations.


题目大意:学过离散数学的同学理解起题意更快,求1到n,n个元素组成的集合中,满足对称性和传递性但不满足自反性的二元组关系集合的个数

题目分析:先结束下样例3,可以是
空集
{<a, a>}, {<b, b>},{<c, c>}
{<a, a>, <b, b>}, {<a, a>, <c, c>}, {<b, b>, <c, c>}
{<a, a>, <b, b>, <a, b>, <b, a>},{<b, b>, <c, c>, <b, c>, <c, b>},{<a, a>, <c, c>, <a, c>, <c, a>} 一共10个
因为每组等价关系的个数正好等于Bell数,所以不难推出ans[i] = Bell[i + 1] - Bell[i],从下一个等价关系个数中减去从当前等价关系推出的个数即为当前不满足自反的二元组关系个数,直接推Bell三角形即可

#include <cstdio>
#define ll long long
int const MAX = 4005;
int const MOD = 1e9 + 7;
ll dp[MAX][MAX];int main()
{int n;scanf("%d", &n);dp[0][0] = 1;for(int i = 1; i <= n; i++){dp[i][0] = dp[i - 1][i - 1];for(int j = 1; j <= i; j++)dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % MOD;}printf("%I64d\n", dp[n][n - 1]);
}





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



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

相关文章

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+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述