目录
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 整除。
具体步骤如下:
- 从标准输入读入一个整数 T,表示测试用例的数量。
- 对于每个测试用例,从标准输入读入两个整数 x 和 y。
- 如果 x 不能被 y 整除,直接输出 x,并进入下一个测试用例。
- 如果 x 能够被 y 整除,找出 y 的所有素因子以及它们的次数。
- 对于每个素因子,计算 x 中该素因子的次数,并计算最小的正整数 d,使得 x/d 不再能够被 y 整除。
- 输出 x/d 作为结果。
总体来说,这段代码通过分解 y 的素因子,计算 x 中各素因子的次数,然后根据这些次数计算最小的正整数 d,最终输出 x/d 作为答案。这样可以满足题目中所描述的条件。