Codeforces Round #636 (Div. 3)
A. Candies
Recently Vova found nn candy wrappers. He remembers that he bought xx candies during the first day, 2x2x candies during the second day, 4x4x candies during the third day, ……, 2k−1x2k−1x candies during the kk-th day. But there is an issue: Vova remembers neither xx nor kk but he is sure that xx and kk are positive integers and k>1k>1.Vova will be satisfied if you tell him any positive integer xx so there is an integer k>1k>1 that x+2x+4x+⋯+2k−1x=nx+2x+4x+⋯+2k−1x=n. It is guaranteed that at least one solution exists. Note that k>1k>1.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow.The only line of the test case contains one integer nn (3≤n≤1093≤n≤109) — the number of candy wrappers Vova found. It is guaranteed that there is some positive integer xx and integer k>1k>1 that x+2x+4x+⋯+2k−1x=nx+2x+4x+⋯+2k−1x=n.OutputPrint one integer — any positive integer value of xx so there is an integer k>1k>1 that x+2x+4x+⋯+2k−1x=nx+2x+4x+⋯+2k−1x=n.ExampleinputCopy7
3
6
7
21
28
999999999
999999984
outputCopy1
2
1
7
4
333333333
333333328
Note
In the first test case of the example, one of the possible answers is x=1,k=2x=1,k=2. Then 1⋅1+2⋅11⋅1+2⋅1 equals n=3n=3.In the second test case of the example, one of the possible answers is x=2,k=2x=2,k=2. Then 1⋅2+2⋅21⋅2+2⋅2 equals n=6n=6.In the third test case of the example, one of the possible answers is x=1,k=3x=1,k=3. Then 1⋅1+2⋅1+4⋅11⋅1+2⋅1+4⋅1 equals n=7n=7.In the fourth test case of the example, one of the possible answers is x=7,k=2x=7,k=2. Then 1⋅7+2⋅71⋅7+2⋅7 equals n=21n=21.In the fifth test case of the example, one of the possible answers is x=4,k=3x=4,k=3. Then 1⋅4+2⋅4+4⋅41⋅4+2⋅4+4⋅4 equals n=28n=28.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
long long n;
cin >> n;
long long m = 3, k = 4;
while (n % m != 0)
{
m += k;
k = k * 2;
}
cout << n / m << endl;
}
return 0;
}
题目大意是对于一个数N,找到他的一个因数X,使得X*(2的等比数列和)=N,同时保证X尽可能大(有多组X,K符合条件时,取X大的一组),这题我一开始卡了好久,因为我同时循环了X,K导致超时,而这题的本质是一重循环,即把X看作N/(2的等比数列和,记为M),M从小到大进行循环,同时判断是否整除
Codeforces Round #637 (Div. 2) - Thanks, Ivan Belonogov!
A. Nastya and Rice
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped nn grains. Nastya read that each grain weighs some integer number of grams from a−ba−b to a+ba+b, inclusive (numbers aa and bb are known), and the whole package of nn grains weighs from c−dc−d to c+dc+d grams, inclusive (numbers cc and dd are known). The weight of the package is the sum of the weights of all nn grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the ii-th grain weighs some integer number xixi (a−b≤xi≤a+b)(a−b≤xi≤a+b), and in total they weigh from c−dc−d to c+dc+d, inclusive (c−d≤∑i=1nxi≤c+dc−d≤∑i=1nxi≤c+d).InputThe input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1000)(1≤t≤1000) — the number of test cases.The next tt lines contain descriptions of the test cases, each line contains 55 integers: nn (1≤n≤1000)(1≤n≤1000) — the number of grains that Nastya counted and a,b,c,da,b,c,d (0≤b<a≤1000,0≤d<c≤1000)(0≤b<a≤1000,0≤d<c≤1000) — numbers that determine the possible weight of one grain of rice (from a−ba−b to a+ba+b) and the possible total weight of the package (from c−dc−d to c+dc+d).OutputFor each test case given in the input print “Yes”, if the information about the weights is not inconsistent, and print “No” if nn grains with masses from a−ba−b to a+ba+b cannot make a package with a total mass from c−dc−d to c+dc+d.ExampleinputCopy5
7 20 3 101 18
11 11 10 234 2
8 9 7 250 122
19 41 21 321 10
3 10 8 6 1
outputCopyYes
No
Yes
No
Yes
Note
In the first test case of the example, we can assume that each grain weighs 1717 grams, and a pack 119119 grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs 1616 grams, and a pack 128128 grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that 33 grains of rice weigh 22, 22, and 33 grams, and a pack is 77 grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
int t;
cin >> t;
while (t--) {
int n,a,b,c,d;
cin>>n>>a>>b>>c>>d;
int l=n*(a-b),r=n*(a+b);
if (r<c-d||c+d<l)
cout << "No"<<endl;
else
cout << "Yes"<<endl;
}
return 0;
}
大意就是一个统计的问题,问总数在不在范围内,思路大致相同,但还不知道为什么没过
bool f = 0;
for (int i = a - b; i <= a + b; i++)
if ((n * i) >= (c - d) && (n * i) <= (c + d))
{
f = 1;
break;
}
if (f == 1) cout << “Yes” << endl;
else cout << “No” << endl;
这是报错 Checker Logwrong answer expected YES, found NO [124th token]意为124个错了
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200429145846516.png
看他测试数据可以发现其实是有规律的,我特地根据规律找到了第124个:14140,但发现结果是一样的,所以我暂时还没发现错哪(不是超时)
B. Nastya and Door
https://blog.csdn.net/li_wen_zhuo/article/details/105725569
自己的简化功夫还是没到家,只想到了将山峰单独出来,但没想到可以直接相减得到区间山峰数
Educational Codeforces Round 86 (Rated for Div. 2)
B. Binary Period
Let’s say string ss has period kk if si=si+ksi=si+k for all ii from 11 to |s|−k|s|−k (|s||s| means length of string ss) and kk is the minimum positive integer with this property.Some examples of a period: for ss=“0101” the period is k=2k=2, for ss=“0000” the period is k=1k=1, for ss=“010” the period is k=2k=2, for ss=“0011” the period is k=4k=4.You are given string tt consisting only of 0’s and 1’s and you need to find such string ss that:String ss consists only of 0’s and 1’s;The length of ss doesn’t exceed 2⋅|t|2⋅|t|;String tt is a subsequence of string ss;String ss has smallest possible period among all strings that meet conditions 1—3.Let us recall that tt is a subsequence of ss if tt can be derived from ss by deleting zero or more elements (any) without changing the order of the remaining elements. For example, tt=“011” is a subsequence of ss=“10101”.InputThe first line contains single integer TT (1≤T≤1001≤T≤100) — the number of test cases.Next TT lines contain test cases — one per line. Each line contains string tt (1≤|t|≤1001≤|t|≤100) consisting only of 0’s and 1’s.OutputPrint one string for each test case — string ss you needed to find. If there are multiple solutions print any one of them.ExampleinputCopy4
00
01
111
110
outputCopy00
01
11111
1010
Note
In the first and second test cases, s=ts=t since it’s already one of the optimal solutions. Answers have periods equal to 11 and 22, respectively.In the third test case, there are shorter optimal solutions, but it’s okay since we don’t need to minimize the string ss. String ss has period equal to 11.
题目大意是找到最小的K,使得添加后,不超过原序列长度的两倍的新序列为K循环序列(每K个相同)
#include<bits/stdc++.h>
using namespace std;
int main(){
int T;
cin>>T;
while(T--){
string s;
cin>>s;
bool same = 1;
for(char c:s) if(c!=s[0]) same=0;
if(same)
cout<<s<<endl;
else{
for(int i=0;i<2*s.size();i++)
cout<<(i%2);
cout<<endl;
}
}
return 0;
}
代码是提交时翻到别人记录看到的,不是我写的,codeblock运行不了,char c:s意为
for(int i=0;i<s.length();i++)
char c=s[i];
更新一下:看了别人的题解大概看懂了,题目中有很多提示的数据,有序列k=3,4,但实际上,仔细想一想,任一个序列,都可以是他两倍长度010101……的子序列,也就是说,k永远为1或2,我被提示里的k=3,4带偏了