Educational Codeforces Round 81 (Rated for Div. 2) B - Infinite Prefixes
Infinite Prefixes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given string s of length nn consisting of 0-s and 1-s. You build an infinite string t as a concatenation of an infinite number of strings s, or t=ssss…t=ssss… For example, if s= 10010, then t= 100101001010010...
Calculate the number of prefixes of tt with balance equal to xx. The balance of some string qq is equal to cnt0,q−cnt1,qcnt0,q−cnt1,q, where cnt0,qcnt0,q is the number of occurrences of 0 in qq, and cnt1,qcnt1,q is the number of occurrences of 1 in qq. The number of such prefixes can be infinite; if it is so, you must say that.
A prefix is a string consisting of several first letters of a given string, without any reorders. An empty prefix is also a valid prefix. For example, the string "abcd" has 5 prefixes: empty string, "a", "ab", "abc" and "abcd".
Input
The first line contains the single integer TT (1≤T≤100) — the number of test cases.
Next 2T lines contain descriptions of test cases — two lines per test case. The first line contains two integers n and x (
) — the length of string s and the desired balance, respectively.
The second line contains the binary string ss (|s|=n, si∈{0,1}).
It's guaranteed that the total sum of nn doesn't exceed
.
Output
Print T integers — one per test case. For each test case print the number of prefixes or −1 if there is an infinite number of such prefixes.
Example
input
4
6 10
010010
5 3
10101
1 0
0
2 0
01
output
3
0
1
-1
Note
In the first test case, there are 3 good prefixes of t: with length 28, 30 and 32.
题目大意
给一个长度已知可以无限重复的01串s,定义一个字符串的q值为’0’字符的个数减去’1’字符的个数,问给定字符串s的所有前缀中(包括空字符串)q值为x的个数
题目不难,但是细节比较麻烦,无非就是一个周期问题,但是一个周期之内会有极大极小,就可能导致多解,首先预处理出串的每一个位置的q值,然后遍历一遍01串,如果(x-该位置的q值)%(01串最后一个位置q值)=0,还没完,因为你得检验如果该位置+(x-该位置的q值)/(01串最后一个位置q值)>0就ok,完全可以,当然想到这还不够,,还有无限解的情况
何时解无穷?
01串最后一个位置q值=0: 如果x=0,那么一定有无穷解;
如果x
0,那么遍历一遍字符串,要是某一个位置q值=x,则也有无穷解
当然x=0的时候一定有解,因为空前缀也行~
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
ll a[100005];
int main()
{
ll t;
cin>>t;
for(rg i=1;i<=t;i++)
{
ll n,x,ans=0;
cin>>n>>x;
string s;
cin>>s;
s[0]=='0'?a[0]=1:a[0]=-1;
for(rg i=1;s[i];i++)
{
a[i]=a[i-1];
if(s[i]=='0')a[i]++;
else a[i]--;
}
if(!x&&!a[n-1])
{
cout<<-1<<endl;
continue;
}
if(!x)ans++;
if(!a[n-1])
{
ll tep=0,flag=0;
for(rg i=0;i<n;i++)
{
if(a[i]==x)
{
cout<<-1<<endl;
flag=1;
break;
}
}
if(!flag)cout<<0<<endl;
continue;
}
for(rg i=0;i<n;i++)
{
if((x-a[i])%a[n-1]==0&&i+1+(x-a[i])/a[n-1]*n>0)ans++;
}
cout<<ans<<endl;
}
while(1)getchar();
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 你不就是加了 2 行代码,为什么要用 2 天?
- Android 使用ZXing生成带logo的二维码
- Resolved versions for app (26.1.0) and test app (27.1.1) differ.
- APP 引导页、欢迎页运用
- 管道与重定向、文件查找-Linux每日一练(7)
- 软件包管理、计划任务-Linux每日一练(8)
- AkShare-能源数据-碳排放-湖北
- AkShare-能源数据-碳排放-广州
- AkShare-股票数据-次新股
- 问与答87: 如何根据列表内容在文件夹中查找图片并复制到另一个文件夹中?
- Java反射是什么?看这篇绝对会了!
- Vuejs 3.0 正式版发布!One Piece. 代号:海贼王
- ES6中模块导入遇到的问题及其解决办法
- 还在手写任务调度代码?试试这款可视化分布式调度框架!
- 3分钟搞定微信小程序类美团用户商家距离计算