【CodeForces - 922】 A B C D

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 749C Voting 是一道有趣的计数题,它的题意是给定了两个候选人 A 和 B 的得票情况,票数相等的情况下,我们需要按照一定的规则进行投票,直到最后一个候选人胜出。具体来说,如果当前的票数相等,我们需要按照轮流投票的规则,每次投给 A 或者 B,直到最后一个候选人胜出。如果某一时刻 A 的票数比 B 多 2 票以上,那么 A 就直接胜出,同样的,如果 B 的票数比 A 多 2 票以上,那么 B 就直接胜出。 我们可以用两个变量 sa 和 sb 来表示 A 和 B 的票数,然后依次遍历每一个投票者的选择。如果当前 A 和 B 的票数相等,我们就按照轮流投票的规则,每次投给 A 或者 B,直到最后一个候选人胜出。如果 A 和 B 的票数相差 2 票以上,那么直接输出胜出者的名字。最后一定会有一个候选人胜出,因为在每一轮投票之后,A 和 B 的票数都会有所增加,最终一定会有一个候选人胜出。 下面是该问题的 Java 代码实现: ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int sa = 0, sb = 0, pa = 0, pb = 0; boolean[] sign = new boolean[n]; for (int i = 0; i < n; i++) { if (s.charAt(i) == 'D') sa++; else sb++; } while (sa > 0 && sb > 0) { for (int i = 0; i < n; i++) { if (sign[i]) continue; if (s.charAt(i) == 'D') { if (pb > 0) { pb--; sa--; sign[i] = true; } else pa++; } else { if (pa > 0) { pa--; sb--; sign[i] = true; } else pb++; } if (sa == 0 || sb == 0) break; } } if (sa == 0) System.out.println("R"); else System.out.println("D"); } } ``` 该代码的时间复杂度为 $O(n)$,空间复杂度为 $O(n)$,其中 n 表示投票者的数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值