-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.cpp
58 lines (57 loc) · 1.21 KB
/
C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
int main()
{
fastio;
long long n,i,j,ara[30],sum=0,count=0,p=0,s[30][30],dif;
cin>>n;
while(n>0)
{
count++;
ara[count]=n%10;
sum=sum+n%10;
n=n/10;
}
dif=count;
if(sum%3==0)
{
p=1;
cout<<0;
}
else
{
for(i=0;i<=count;i++)
{
for(j=0;j<=count;j++)
s[i][j]=0;
}
for(i=1;i<=count;i++)
s[i][i]=ara[i];
for(i=1;i<=count;i++)
{
for(j=i;j<=count;j++)
{
if(i==1 && j==count)
continue;
if(s[i][j]==0)
{
if(s[i][j-1]==0)
s[i][j]=s[i-1][j]-ara[i-1];
else if(s[i-1][j]==0)
s[i][j]=s[i][j-1]+ara[j];
}
if((sum-s[i][j])%3==0)
{
p=1;
dif=min(dif,j-i+1);
}
}
}
if(p==0)
cout<<"-1";
else
cout<<dif;
}
return 0;
}