Разбор Codeforces Round #461 (Div. 2) C & D

C. Cave Painting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nk (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).

Examples
input
4 4
output
No
input
5 3
output
Yes
Note

In the first sample remainders modulo   1  and   4  coincide.
如果想要mod 1..k 的余数属于k组不同的class 那必须是一一对应的 因为余数必然小于k 所以可以确定余数范围是0..k-1 且每种只出现一次
 然后从后往前看,首先发现余数k-1只能通过mod k得到 余数k-2只能通过mod k 或 mod k-1 得到,但是k已经用过了 所以只能由k得到了 所以能建立了k组双射关系
n = k-1 mod k n = k-2 mod k-1 …… n = 0 mod 1 然后得到lcm(k,k-1,k-2,...,1) | n 发现lcm43 已经超过了 1e18 所以 只要超过42就直接no了 不然就check
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}

inline ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
const int maxn = 10000;
int main(){
ll n, k;

cin>>n>>k; unsigned ll lcmv=1;
if (k >= 43) {puts("No");return 0;}

for (int i=1; i<=k; i++){
    lcmv = lcm(lcmv, i);
    if ((n+1)%lcmv){
        puts("No");
        return 0;
    }
}
puts("Yes");
return 0;
}

D. Robot Vacuum Cleaner
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
4
ssh
hs
s
hhhs
output
18
input
2
h
s
output
1
Note

The optimal concatenation in the first sample is ssshhshhhs.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> ii;

struct myst
{
    ll scnt, hcnt;
    string st;
    bool operator<(myst& rhs) const {
        return (scnt * rhs.hcnt > rhs.scnt * hcnt);
    }
}st[100000];

int main(){
ll n, t, len, scnt, hcnt, toth = 0;
ll cnt =0;
string tmp;
cin>> n;
for (ll i=0; i<n; i++){
    cin>>tmp;
    len = tmp.length();
    hcnt = scnt = 0;
    for (ll j=0; j<len; j++){
        if (tmp[j] == 's') scnt++;
        if (tmp[j] == 'h') hcnt++;
    }
    st[i] = {scnt, hcnt, tmp};
}
sort(st, st + n);
tmp = "";
for (ll i=0; i<n; i++){
    //cout<<st[i].st<<endl;
    tmp += st[i].st;
}
//cout<<tmp<<endl;
len = tmp.length();
for (ll i=0; i<tmp.length(); i++){
    if (tmp[i] == 'h') toth++;
}
for (ll i=0; i<tmp.length(); i++){
    if (tmp[i] == 's') cnt+=toth;
    if (tmp[i] == 'h') toth--;
}
cout<<cnt<<endl;
}
总串中 如果相邻两个字串交换 只会影响这两个字串所在区间的sh值 不会影响整体的 所以如果sa*hb>sb*ha 就说明a应该放在b前面,sa是a中s的数量,sb是b中s的数量,h同理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值