天天看點

HDU 4403 A very hard Aoshu problem

Description

Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a problem to test his students: 

Given a serial of digits, you must put a '=' and none or some '+' between these digits and make an equation. Please find out how many equations you can get. For example, if the digits serial is "1212", you can get 2 equations, they are "12=12" and "1+2=1+2". Please note that the digits only include 1 to 9, and every '+' must have a digit on its left side and right side. For example, "+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and "11+1=12" are different equations. 

Input

There are several test cases. Each test case is a digit serial in a line. The length of a serial is at least 2 and no more than 15. The input ends with a line of "END". 

Output

For each test case , output a integer in a line, indicating the number of equations you can get. 

Sample Input

1212

12345666

1235

END

Sample Output

2

2

資料小,直接枚舉全部情況即可

#include<iostream>
#include<cstdlib>
#include<string.h>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
char s[maxn];
int n,ans,vis[maxn];

int main()
{
    int i,j,k;
    while (~scanf("%s",s),strcmp(s,"END"))
    {
        n=strlen(s);    ans=0;
        for ( j=0;j<n-1;j++)
        {
            for ( i=0;i<(1<<n);i++) vis[i]=0;
            for ( i=0;i<(1<<n);i++)
            if(!vis[i|(1<<j)])
            {    
                vis[i|(1<<j)]=1;
                __int64 q=0,h=0,bef=0;
                for ( k=0;k<=j;k++)
                {
                    bef=bef*10+s[k]-'0';
                    if ((i&(1<<k))||k==j) q+=bef,bef=0;
                }
                bef=0;
                for ( k=j+1;s[k];k++)
                {
                    bef=bef*10+s[k]-'0';
                    if ((i&(1<<k))||!s[k+1]) h+=bef,bef=0;
                }
                if (q==h) 
                {
                    ans++;
                    //printf("%d %d %d\n",q,i,j);
                }
            }
        }
        printf("%d\n",ans/2);
    }
    return 0;
}      

重溫一遍,這次用了map

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int read()
{
  char ch = getchar();
  while (ch<'0' || ch>'9') ch = getchar();
  int x = ch - '0';
  while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
  return x;
}
int T, n;
map<LL, int> M;
char s[N];

int main()
{
  while (scanf("%s", s + 1) != EOF, s[1] != 'E')
  {
    n = strlen(s + 1);
    LL ans = 0;
    rep(i, 1, n - 1)
    {
      M.clear();
      rep(j, 0, (1 << i - 1) - 1)
      {
        LL now = s[1] - '0', res = 0;
        rep(k, 1, i - 1)
        {
          if ((1 << k - 1)&j) res += now, now = 0;
          now = now * 10 + s[k + 1] - '0';
        }
        M[res + now]++;
      }
      char *g = s + i;
      rep(j, 0, (1 << n - i - 1) - 1)
      {
        LL now = g[1] - '0', res = 0;
        rep(k, 1, n - i - 1)
        {
          if ((1 << k - 1)&j) res += now, now = 0;
          now = now * 10 + g[k + 1] - '0';
        }
        ans += M[res + now];
      }
    }
    printf("%lld\n", ans);
  } 
  return 0;
}