B - 等分數字串
給定一個只包含0-9的字符串S,請你判斷能否將S劃分爲兩個或兩個以上連續的子串,使得每一個子串中的數字總和都相等。
Input
輸入包含多組數據。
第一行包含一個整數N,代表數據組數。
以下N行,每行包含一個字符串S。
對於50%的數據,2 ≤ |S| ≤ 100
對於100%的數據,1 ≤ N ≤ 5, 2 ≤ |S| ≤ 100000
Output
對於每組數據,輸出一行,內容是YES或者NO,代表是否存在題目要求的劃分方案。
Sample Input
3 1248 2680 54174760
Sample Output
NO YES YES
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#define show(a) cout<<#a<<"="<<a<<endl
#define MAXN 1005
using namespace std;
int prime(int x)
{
for(int i=2; i<sqrt(x)+1; i++)
{
if(x%i==0) return i;
}
return 0;
}
int main()
{
int num;
scanf("%d",&num);
for(int i=0; i<num; i++)
{
string s;
cin>>s;
//cout<<s.length()<<endl;
int sum=0,maxn=0;
for(int i=0; i<s.length(); i++)
{
sum+=s[i]-'0';
maxn=max(maxn,s[i]-'0');
}
int now=(prime(sum)==0?1:sum/prime(sum)),group;
while(now>=maxn)
{
//show(now);
int cnt=0;
group=0;
int i;
for(i=0; i<s.length(); i++)
{
cnt+=s[i]-'0';
//show(cnt);
if(cnt==now)
{
cnt=0;
group++;
}
if(cnt>now)
break;
}
if(sum==group*now&&group>1)
{
break;
}
now--;
while(now>0&&sum%now!=0)
{
now--;
}
}
if(s.length()>1&&now>=maxn)
cout<<"YES\n";
else
cout<<"NO\n";
}
}