https://codeforces.com/contest/1056/problem/E
hash的进制可以适量大一点,字符a从1开始(如果从0开始可能算出相同的值,比如字符串多个a)。
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {
new Main().run();
}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run() {
work();
out.flush();
}
long mod = 1000000007;
long gcd(long a, long b) {
return a == 0 ? b : gcd(b % a, a);
}
long inf = Long.MAX_VALUE / 3;
long[] sum;
long[] rec;
long base=40;
void work() {
String str=ns();
int n=str.length();
int[] A=new int[n];
int c0=0,c1=0;
for(int i=0;i<n;i++){
A[i]=str.charAt(i)-'0';
if(A[i]==0){
c0++;
}else{
c1++;
}
}
char[] chs=ns().toCharArray();
int m=chs.length;
sum=new long[m];
rec=new long[m];
rec[0]=1;
for(int i=1;i<m;i++){
rec[i]=rec[i-1]*base%mod;
}
sum[0]=chs[0]-'a'+1;
for(int i=1;i<m;i++){
sum[i]=sum[i-1]*base+chs[i]-'a'+1;
sum[i]%=mod;
}
int ret=0;
out:
for(int i=1;i*c0<m;i++){
int r=m-(i*c0);
if(r%c1!=0)continue;
int l0=i,l1=r/c1;
long v0=-1,v1=-1;
for(int j=0,k=-1;j<n;j++){
if(A[j]==0){
long v=count(k,k+l0);
if(v0==-1||v==v0){
v0=v;
}else{
continue out;
}
k+=l0;
}else{
long v=count(k,k+l1);
if(v1==-1||v==v1){
v1=v;
}else{
continue out;
}
k+=l1;
}
}
if(v0!=v1){
ret++;
}
}
out.println(ret);
}
private long count(int s, int e) {
long v=s==-1?0:(sum[s]*rec[e-s]%mod);
return ((sum[e]-v)%mod+mod)%mod;
}
private ArrayList<long[]>[] ngw(int n, int m) {
ArrayList<long[]>[] graph = (ArrayList<long[]>[]) new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 1; i <= m; i++) {
long s = in.nextLong() - 1, e = in.nextLong() - 1, w = in.nextLong();
graph[(int) s].add(new long[]{e, w});
graph[(int) e].add(new long[]{s, w});
}
return graph;
}
private ArrayList<Integer>[] ng(int n, int m) {
ArrayList<Integer>[] graph = (ArrayList<Integer>[]) new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 1; i <= m; i++) {
int s = in.nextInt() - 1, e = in.nextInt() - 1;
graph[s].add(e);
// graph[e].add(s);
}
return graph;
}
private int ni() {
return in.nextInt();
}
private long nl() {
return in.nextLong();
}
private String ns() {
return in.next();
}
private long[] na(int n) {
long[] A = new long[n];
for (int i = 0; i < n; i++) {
A[i] = in.nextLong();
}
return A;
}
private int[] nia(int n) {
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = in.nextInt();
}
return A;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreElements())//回车,空行情况
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}