【CodeForces - 922】 A B C D

本文解析了四个算法挑战赛题目,包括玩具克隆、魔法森林、洞穴绘画和机器人吸尘器,介绍了每道题目的背景、输入输出要求及示例,并提供了对应的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A- Cloning Toys

Imp likes his plush toy a lot.

Recently, he found a machine that can clone plush toys. Imp knows that if he applies the machine to an original toy, he additionally gets one more original toy and one copy, and if he applies the machine to a copied toy, he gets two additional copies.

Initially, Imp has only one original toy. He wants to know if it is possible to use machine to get exactly x copied toys and y original toys? He can’t throw toys away, and he can’t apply the machine to a copy if he doesn’t currently have any copies.

Input
The only line contains two integers x and y (0 ≤ x, y ≤ 109) — the number of copies and the number of original toys Imp wants to get (including the initial one).

Output
Print “Yes”, if the desired configuration is possible, and “No” otherwise.

You can print each letter in arbitrary case (upper or lower).

Example
Input
6 3
Output
Yes
Input
4 2
Output
No
Input
1000 1001
Output
Yes
Note
In the first example, Imp has to apply the machine twice to original toys and then twice to copies.
代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long

const int N =  1e5+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;

/*-------------------------------------*/


int main(){
     int x,y;cin>>x>>y;
     if(y<=0) puts("No");
     else if(y==1&&x>0) {puts("No");}
     else{
        if(x<y-1) puts("No");
        else{
            x-=y-1;
            if(x&1) puts("No");
            else puts("Yes");
        }
     }
return 0;
}

B-Magic Forest

Imp is in a magic forest, where xorangles grow (wut?)

A xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of order n to get out of the forest.

Formally, for a given integer n you have to find the number of such triples (a, b, c), that:

1 ≤ a ≤ b ≤ c ≤ n;
, where denotes the bitwise xor of integers x and y.
(a, b, c) form a non-degenerate (with strictly positive area) triangle.
Input
The only line contains a single integer n (1 ≤ n ≤ 2500).

Output
Print the number of xorangles of order n.

Example
Input
6
Output
1
Input
10
Output
2
Note
The only xorangle in the first sample is (3, 5, 6).

代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long

const int N =  1e5+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;

/*-------------------------------------*/


int main(){
     int n; cin>>n;
     LL ans=0;
     for(int x=1;x<=n;x++){
        for(int y=1;y<=n;y++){
            int z=(x^y); z^=0;
            if(z>n || z<=0) continue;
            int mx=max(x,max(y,z));
            if(mx<x+y+z-mx) {
                //printf("%d %d %d\n",x,y,z);
                ans++;
            }
        }
     }
     cout<<ans/6;
return 0;
}

C - Cave Painting

Imp is watching a documentary about cave painting.

Some numbers, carved in chaotic order, immediately attracted his attention. Imp rapidly proposed a guess that they are the remainders of division of a number n by all integers i from 1 to k. Unfortunately, there are too many integers to analyze for Imp.

Imp wants you to check whether all these remainders are distinct. Formally, he wants to check, if all , 1 ≤ i ≤ k, are distinct, i. e. there is no such pair (i, j) that:

1 ≤ i < j ≤ k,
, where is the remainder of division x by y.
Input
The only line contains two integers n, k (1 ≤ n, k ≤ 1018).

Output
Print “Yes”, if all the remainders are distinct, and “No” otherwise.

You can print each letter in arbitrary case (lower or upper).

Example
Input
4 4
Output
No
Input
5 3
Output
Yes
Note
In the first sample remainders modulo 1 and 4 coincide.

暴力打表发现 好像枚举的次数不超过个位数。没想到暴力真可以过。

代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long

const int N =  1e5+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/
map<LL,LL>vis;
bool Judge(LL k,LL n){
    vis.clear();
    for(LL i=1;i<=k;i++){
        if(vis[n%i]) return false;
        vis[n%i]=1;
    }
    return true;
}

int main(){
    LL n,k;
    while(cin>>n>>k){

          if(Judge(k,n)) puts("Yes");
        else puts("No");
    }
return 0;
}

D-Robot Vacuum Cleaner

Pushok the dog has been chasing Imp for a few hours already.

Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner.

While moving, the robot generates a string t consisting of letters ‘s’ and ‘h’, that produces a lot of noise. We define noise of string t as the number of occurrences of string “sh” as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and and .

The robot is off at the moment. Imp knows that it has a sequence of strings ti in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.

Help Imp to find the maximum noise he can achieve by changing the order of the strings.

Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of strings in robot’s memory.

Next n lines contain the strings t1, t2, …, tn, one per line. It is guaranteed that the strings are non-empty, contain only English letters ‘s’ and ‘h’ and their total length does not exceed 105.

Output
Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.

Example
Input
4
ssh
hs
s
hhhs
Output
18
Input
2
h
s
Output
1
Note
The optimal concatenation in the first sample is ssshhshhhs.

分析: 贪心
其实我们可以发现对于任意的两个串,我们都可以贪心直接判断出来到底谁先谁后 最优。
首先对于每个串,我们可以知道其 s 的个数,h的个数,其本身可以组成sh的个数。
假设现在有两个串 A B ,
如果 A - B 链接串 的话,总共的贡献为 Ash + Bsh + As * Bh
如果 B - A 链接串的话,总共的贡献为 Ash + Bsh + Bs * Ah
我们可以发现两个串的先后顺序只和 其s和h的个数有关,所以我们可以sort排序得到一个最优的链接串。
然后就是对于一个串来说,怎么求它的可以组成sh的个数?很简单,我们可以倒着枚举这个串,如果是h, 贡献为这个位置前所有的s个数,如果为s,总共s的个数减减。
代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long

const int N =  1e5+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;

/*-------------------------------------*/

struct Node{
    LL s,h,sh;
    string t;
    bool operator < ( const Node &b ) const{
        return  (s*b.h)> (h*b.s);
    }
}node[N];

int main(){
    int n;scanf("%d",&n);
    LL cnts=0;
    for(int i=0;i<n;i++){
        string str; cin>>str;
        LL cnt=0;
        for(int j=0;j<str.size();j++) if(str[j]=='s') cnt++;
        cnts+=cnt;
        LL ge=str.size()-cnt;
        node[i].s=cnt; node[i].h=ge; node[i].t=str;
    }

    sort(node,node+n);
    string s=""; //  合成最优的串
    for(int i=0;i<n;i++){
        s+=node[i].t;
    }

    LL ans=0;
    for(int j=s.size()-1;j>=0;j--){
        if(s[j]=='h') ans+=cnts;
        else cnts--;
    }
    cout<<ans;
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值