A. Balanced Bitstring

目录

1.Problem

2.Input

3.Output

4.Examples

4.1input

4.2output

5.Code

6.Conclusion


1.Problem

A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called kk-balanced if every substring of size kk of this bitstring has an equal amount of 0 and 1 characters (k2k2 of each).

You are given an integer kk and a string ss which is composed only of characters 0, 1, and ?. You need to determine whether you can make a kk-balanced bitstring by replacing every ? characters in ss with either 0 or 1.

A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

2.Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.

The first line of each test case contains two integers nn and kk (2≤k≤n≤3⋅1052≤k≤n≤3⋅105, kk is even)  — the length of the string and the parameter for a balanced bitstring.

The next line contains the string ss (|s|=n|s|=n). It is given that ss consists of only 0, 1, and ?.

It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053⋅105.

3.Output

For each test case, print YES if we can replace every ? in ss with 0 or 1 such that the resulting bitstring is kk-balanced, or NO if it is not possible.

4.Examples

4.1input

9
6 4
100110
3 2
1?1
3 2
1?0
4 4
????
7 4
1?0??1?
10 10
11??11??11
4 2
1??1
4 4
?0?0
6 2
????00

4.2output

YES
YES
NO
YES
YES
NO
NO
YES
NO

5.Code

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

ll readint(){
    ll x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

ll qpow(ll x, ll p) {
    ll ret = 1;
    for(; p; p >>= 1, x = x * x) if(p & 1) ret = ret * x;
    return ret;
}

int main() {
    int T = readint();
    while(T--) {
        ll x = readint(), y = readint();
        if(x % y != 0) {
            printf("%lld\n", x);
            continue;
        }
        vector<pii> fac(0);
        for(int i = 2; i * i <= y; i++) {
            if(y % i == 0) {
                pii tmp = make_pair(i, 0);
                while(y % i == 0) tmp.second++, y /= i;
                fac.push_back(tmp);
            }
        }
        if(y > 1) fac.push_back(make_pair(y, 1));
        ll mina = LLONG_MAX;
        for(auto r : fac) {
            int num = 0;
            ll tmp = x;
            while(tmp % r.first == 0) num++, tmp /= r.first;
            mina = min(mina, qpow(r.first, num - r.second + 1));
        }
        printf("%lld\n", x / mina);
    }
    return 0;
}

6.Conclusion

这段代码的功能是解决一个数学问题:对于给定的整数 x 和 y,如果 x 能够被 y 整除,那么找到一个最小的正整数 d,使得 x/d 不再能够被 y 整除。

具体步骤如下:

  1. 从标准输入读入一个整数 T,表示测试用例的数量。
  2. 对于每个测试用例,从标准输入读入两个整数 x 和 y。
  3. 如果 x 不能被 y 整除,直接输出 x,并进入下一个测试用例。
  4. 如果 x 能够被 y 整除,找出 y 的所有素因子以及它们的次数。
  5. 对于每个素因子,计算 x 中该素因子的次数,并计算最小的正整数 d,使得 x/d 不再能够被 y 整除。
  6. 输出 x/d 作为结果。

总体来说,这段代码通过分解 y 的素因子,计算 x 中各素因子的次数,然后根据这些次数计算最小的正整数 d,最终输出 x/d 作为答案。这样可以满足题目中所描述的条件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳而生__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值