Codeforces Round #741 (Div. 2)

Codeforces Round #741 (Div. 2)(https://codeforces.com/contest/1562)
A. The Miracle and the Sleeper(https://codeforces.com/contest/1562/problem/A)
You are given two integers l and r, l≤r. Find the largest possible value of amodb over all pairs (a,b) of integers for which r≥a≥b≥l.

As a reminder, amodb is a remainder we get when dividing a by b. For example, 26mod8=2.

Input
Each test contains multiple test cases.

The first line contains one positive integer t (1≤t≤104), denoting the number of test cases. Description of the test cases follows.

The only line of each test case contains two integers l, r (1≤l≤r≤109).

Output
For every test case, output the largest possible value of amodb over all pairs (a,b) of integers for which r≥a≥b≥l.

Example
inputCopy
4
1 1
999999999 1000000000
8 26
1 999999999
outputCopy
0
1
12
499999999
Note
In the first test case, the only allowed pair is (a,b)=(1,1), for which amodb=1mod1=0.

In the second test case, the optimal choice is pair (a,b)=(1000000000,999999999), for which amodb=1.
题意:
给定l,r,找到符合r≥a≥b≥l条件下a mod b的最大值
一个数x去模一个小于x的数,能得到的最大值应该为x mod (x/2+1),再注意x是否在[l,r]中即可
AC代码:

#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out                 \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++) 
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 1e5+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int solve()
{
    ll a,b;
    cin>>a>>b;
    ll x=(b/2+1);
    if(x>=a)
    cout<<b-x;
    else{
        cout<<b-a;
    }
    return 0;
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

B. Scenes From a Memory(https://codeforces.com/contest/1562/problem/B)
During the hypnosis session, Nicholas suddenly remembered a positive integer n, which doesn’t contain zeros in decimal notation.

Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?

For some numbers doing so is impossible: for example, for number 53 it’s impossible to delete some of its digits to obtain a not prime integer. However, for all n in the test cases of this problem, it’s guaranteed that it’s possible to delete some of their digits to obtain a not prime number.

Note that you cannot remove all the digits from the number.

A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. 1 is neither a prime nor a composite number.

Input
Each test contains multiple test cases.

The first line contains one positive integer t (1≤t≤103), denoting the number of test cases. Description of the test cases follows.

The first line of each test case contains one positive integer k (1≤k≤50) — the number of digits in the number.

The second line of each test case contains a positive integer n, which doesn’t contain zeros in decimal notation (10k−1≤n<10k). It is guaranteed that it is always possible to remove less than k digits to make the number not prime.

It is guaranteed that the sum of k over all test cases does not exceed 104.

Output
For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions.

If there are multiple solutions, print any.

Example
inputCopy
7
3
237
5
44444
3
221
2
35
3
773
1
4
30
626221626221626221626221626221
outputCopy
2
27
1
4
1
1
2
35
2
77
1
4
1
6
Note
In the first test case, you can’t delete 2 digits from the number 237, as all the numbers 2, 3, and 7 are prime. However, you can delete 1 digit, obtaining a number 27=33.

In the second test case, you can delete all digits except one, as 4=22 is a composite number.

题意:给你一个数,问你最多能删除多少个数字,让剩下的数位组成的十进制数是一个合数或者是1
根据题意:若这个数里含有1,4,6,8,9中的任意一个,那么我们就可以将其他数字全删掉,最后只剩下一位上述几个数字中的某数,对于剩下的2,3,5,7,
若其中某个数字出现两次,我们就可以得到一个形如ii的合数,否则,2,3,5,7中任意一个数字的个数小于等于1,我们将这四个数字能组合出的合数找出来即可。

因为把57当成素数让我WA了好几发…

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out                 \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++) 
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 1e5+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int solve()
{
    int k;
    cin>>k;
    string s;
    cin>>s;
    int cnt[10]={0};
    int pos[10]={0};
    for(int i=0;i<k;i++){
        cnt[s[i]-'0']++;
        pos[s[i]-'0']=i;
    }
    for(int i=1;i<10;i++){
        if(i==1||i==4||i==6||i==8||i==9){
            if(cnt[i]>0){
                cout<<1<<endl<<i;
                return 0;
            }
        }
    }
    for(int i=1;i<10;i++){
        if(cnt[i]>=2){
            cout<<2<<endl<<i<<i;
            return 0;
        }
    }
    if(cnt[2]>0){
        if(cnt[3]>0&&pos[3]<pos[2]) {
            cout<<2<<endl<<3<<2;
            return 0;
        }
        if(cnt[5]>0){
            cout<<2<<endl;
            if(pos[5]<pos[2]) cout<<5<<2;
            else cout<<2<<5;
            return 0;
        }
        if(cnt[7]>0){
            cout<<2<<endl;
            if(pos[7]<pos[2]) cout<<7<<2;
            else cout<<2<<7;
            return 0;
        }
    }

    if(cnt[5]>0){
        if(cnt[3]>0&&pos[3]<pos[5]) {
            cout<<2<<endl<<3<<5;
            return 0;
        }
        if(cnt[7]>0) {

            cout<<2<<endl;;
            if(pos[7]<pos[5]) cout<<7<<5;
            else cout<<5<<7;
            return 0;
        }
    }
    return 0;
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

C. Rings(https://codeforces.com/contest/1562/problem/C)
Somewhere in a parallel Middle-earth, when Saruman caught Frodo, he only found n rings. And the i-th ring was either gold or silver. For convenience Saruman wrote down a binary string s of n characters, where the i-th character was 0 if the i-th ring was gold, and 1 if it was silver.

Saruman has a magic function f, which takes a binary string and returns a number obtained by converting the string into a binary number and then converting the binary number into a decimal number. For example, f(001010)=10,f(111)=7,f(11011101)=221.

Saruman, however, thinks that the order of the rings plays some important role. He wants to find 2 pairs of integers (l1,r1),(l2,r2), such that:

1≤l1≤n, 1≤r1≤n, r1−l1+1≥⌊n2⌋
1≤l2≤n, 1≤r2≤n, r2−l2+1≥⌊n2⌋
Pairs (l1,r1) and (l2,r2) are distinct. That is, at least one of l1≠l2 and r1≠r2 must hold.
Let t be the substring s[l1:r1] of s, and w be the substring s[l2:r2] of s. Then there exists non-negative integer k, such that f(t)=f(w)⋅k.
Here substring s[l:r] denotes slsl+1…sr−1sr, and ⌊x⌋ denotes rounding the number down to the nearest integer.

Help Saruman solve this problem! It is guaranteed that under the constraints of the problem at least one solution exists.

Input
Each test contains multiple test cases.

The first line contains one positive integer t (1≤t≤103), denoting the number of test cases. Description of the test cases follows.

The first line of each test case contains one positive integer n (2≤n≤2⋅104) — length of the string.

The second line of each test case contains a non-empty binary string of length n.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For every test case print four integers l1, r1, l2, r2, which denote the beginning of the first substring, the end of the first substring, the beginning of the second substring, and the end of the second substring, respectively.

If there are multiple solutions, print any.

Example
inputCopy
7
6
101111
9
111000111
8
10000000
5
11011
6
001111
3
101
30
100000000000000100000000000000
outputCopy
3 6 1 3
1 9 4 9
5 8 1 4
1 5 3 5
1 6 2 4
1 2 2 3
1 15 16 30
Note
In the first testcase f(t)=f(1111)=15, f(w)=f(101)=5.

In the second testcase f(t)=f(111000111)=455, f(w)=f(000111)=7.

In the third testcase f(t)=f(0000)=0, f(w)=f(1000)=8.

In the fourth testcase f(t)=f(11011)=27, f(w)=f(011)=3.

In the fifth testcase f(t)=f(001111)=15, f(w)=f(011)=3.
给一个01字符串,在字符串里找到两个长度大于字符串长度一半的子串t,w,且存在一个非负整数k,使得f(t)=f(w)*k,f(t)表示t的二进制数对应得十进制值。
k是大于等于0的整数,我们考虑三种k值,当k=1时,两个子串值必须相等,而两个子串的值想要相等有两种场景,一是两个字符串完全一样,另一种则是某一个子串含有前导零,
这给了我们提示,如果给定的字符串仅由0/1构成那么1 n-1 2 n就是一种解,否则,我们去找到字符串中出现的第一个0来构造答案,若这个0位置在n/2之前,设为pos,则pos n pos+1 n为一种解,
相反,1 pos 1 pos-1 为一种解(k=2).

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out                 \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++) 
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 4e6+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int find(string s){
    for(int i=0;i<s.size();i++)
        if(s[i]=='0') return i+1;
    return -1;
}
int solve()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int pos=find(s);
    if(pos==-1){
        printf("1 %d 2 %d",n-1,n);
        return 0;
    }
    if(pos>s.size()/2){
        printf("1 %d 1 %d",pos,pos-1);
    }
    else{
        printf("%d %d %d %d",pos,n,pos+1,n);
    }
    return 0;
}
int main()
{
    // Q_in_out
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout<<endl;
    }
    return 0;
}

D1. Two Hundred Twenty One (easy version)
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.

Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.

The main element of this machine are n rods arranged along one straight line and numbered from 1 to n inclusive. Each of these rods must carry an electric charge quantitatively equal to either 1 or −1 (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.

More formally, the rods can be represented as an array of n numbers characterizing the charge: either 1 or −1. Then the condition must hold: a1−a2+a3−a4+…=0, or ∑i=1n(−1)i−1⋅ai=0.

Sparky charged all n rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has q questions. In the ith question Sparky asks: if the machine consisted only of rods with numbers li to ri inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don’t have to remove the rods at all.

If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.

Help your friends and answer all of Sparky’s questions!

Input
Each test contains multiple test cases.

The first line contains one positive integer t (1≤t≤103), denoting the number of test cases. Description of the test cases follows.

The first line of each test case contains two positive integers n and q (1≤n,q≤3⋅105) — the number of rods and the number of questions.

The second line of each test case contains a non-empty string s of length n, where the charge of the i-th rod is 1 if si is the “+” symbol, or −1 if si is the “-” symbol.

Each next line from the next q lines contains two positive integers li ans ri (1≤li≤ri≤n) — numbers, describing Sparky’s questions.

It is guaranteed that the sum of n over all test cases does not exceed 3⋅105, and the sum of q over all test cases does not exceed 3⋅105.

Output
For each test case, print a single integer — the minimal number of rods that can be removed.

Example
inputCopy
3
14 1
±-+±–+±+±
1 14
14 3
±-+±–++±–
1 14
6 12
3 10
4 10
±±
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
outputCopy
2
2
1
0
1
2
1
2
1
2
1
1
2
1
Note
In the first test case for the first query you can remove the rods numbered 5 and 8, then the following set of rods will remain: ±-±-+±+±. It is easy to see that here the sign-variable sum is zero.

In the second test case:

For the first query, we can remove the rods numbered 1 and 11, then the following set of rods will remain: --+±–+±–. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered 9, then the following set of rods will remain: —+±. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all.

题意,给一个长为n的字符串和查询次数q,字符串只由±构成,+代表1,-代表-1,要求求出最少从字符串中删掉多少+/-能使a1-a2+a3-a4+…=0(a[i]表示字符串对应的值)

#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out                 \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++) 
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 3e5+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int a[N];
char s[N];
int solve()
{
    int n,q;
    cin>>n>>q;
    cin>>s+1;
    for(int i=1;i<=n;i++){
        a[i]=(s[i]=='+'?1:-1);
        if(i%2==0) a[i]*=-1;
        a[i]+=a[i-1];
    }
    int l,r;
    for(int i=0;i<q;i++){
        cin>>l>>r;
        if((r-l+1)&1) cout<<1<<endl;
        else cout<<(a[r]-a[l-1]==0?0:2)<<endl;
    }
    return 0;
}
int main()
{
    Q_in_out;
    int t;
    t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        // cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值