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;
}
- 《Redis设计与实现》读书笔记(二十七) ——Redis哨兵(sentinel)主服务器下线判断与故障转移
- 基于input子系统的sensor驱动调试(二)
- 编程语言中的闭包
- 《Redis设计与实现》读书笔记(二十八) ——Redis集群节点结构与槽分配
- Linux RCU 机制详解
- 《Redis设计与实现》读书笔记(二十九) ——Redis集群执行命令与重新分片
- 如何使用C语言的面向对象
- 《Redis设计与实现》读书笔记(三十) ——Redis集群节点复制与故障转移
- 掌握一点儿统计学
- 高通HAL层之bmp18x.cpp
- Oracle 数据库之最:你见过最高的 SQL Version 是多少?
- Android 子activity关闭 向父activity传值
- 《Redis设计与实现》读书笔记(三十一) ——Redis集群消息类型
- 统计学中的相关性分析
- 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 数组属性和方法
- Array - 295. Find Median from Data Stream
- Array - 239. Sliding Window Maximum
- Array - 164. Maximum Gap
- Array - 128. Longest Consecutive Sequence
- String - 13. Roman to Integer
- String - 171. Excel Sheet Column Number
- String - 168. Excel Sheet Column Title
- String - 316. Remove Duplicate Letters
- String - 38. Count and Say
- String - 161. One Edit Distance
- String - 6. ZigZag Conversion
- String - 179. Largest Number
- 利用Github Pages实现“服务CNAME”功能
- String - 49. Group Anagrams
- Flutter包大小治理上的探索与实践