码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3419

时间:2014-12-15 00:09:40      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   sp   for   java   

The Three Groups

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 656    Accepted Submission(s): 291


Problem Description
There appeared in “Nouvelles Annales de Mathematiques” the following puzzle as a modification of any of my “Canterbury Puzzles.” Arrange the nine digits in three groups of two, three, and four digits, so that the first two numbers when multiplied together make the third. Thus, 12 * 483 = 5,796. I now also propose to include the cases where there are one, four and four digits, such as 4 * 1,738 = 6,952. Can you find all possible solutions in both cases?”- Amusement in Mathematics, by Ernest Dudeney.

Now we want to arrange some of the nine digits (without ‘0’) in three groups of a, b and c digits, so that the first two numbers when multiplied together make the third. In addition, no digit can be used more than once in a single multiplication. You have to find how many solution exist there for given a, b and c.
 

 

Input
There are multiple test cases. In addition, each test case is consisting of three integers a, b, c separated by spaces.(a , b , c >= 0 && a + b + c <= 9) Meaning of a, b, and c are described in the problem statement. The last case contains exactly three 0’s for all of a, b, c and indicates the end of input stream. This line should not be processed.
 

 

Output
Your program should print a single integer for each input in a single line. The integer will state that how many solution there are for the given size of a, b and c.
 

 

Sample Input
2 3 4 1 1 1 0 0 0
 

 

Sample Output
7 4 Note: The valid solutions for the second sample input-output are as following: 2*3 =6 2*4 =8 3*2 =6 4*2 =8
 

 

Author
Muhammed Hedayet
 

 

Source
 

 

Recommend
 
题解:
裸搜索。。。用了点记忆化存储
#include<iostream>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<vector>
using namespace std;
int a,b,c,dp[10][10][10],ans,x,y,z;
bool vis[10];
void dfs(int sum,int cnt,int ret)
{
      if(ret==1&&cnt==a)
          x=sum,sum=0,ret++,cnt=0;
      else if(ret==2&&cnt==b)
          y=sum,sum=0,ret++,cnt=0;
      else if(ret==3&&cnt==c)
      {
           z=sum;
           if(x*y==z)
                  ans++;
           return ;
      }
      for(int i=1;i<=9;i++)
      {
            if(!vis[i])
            {
                  vis[i]=true;
                  dfs(sum*10+i,cnt+1,ret);
                  vis[i]=false;
            }
      }
}
int main()
{
      while(scanf("%d%d%d",&a,&b,&c)!=EOF)
      {
            ans=0;
            memset(vis,0,sizeof(vis));
            if(a==0&&b==0&&c==0)
                  break;
            if(a>c||b>c||a+b<c)
            {
                  printf("0\n");
                  continue;
            }
            if(dp[a][b][c])
            {
                  printf("%d\n",dp[a][b][c]);
                  continue;
            }
            dfs(0,0,1);
            dp[a][b][c]=ans;
            printf("%d\n",dp[a][b][c]);
      }
      return 0;
}

  



hdu 3419

标签:des   blog   http   io   ar   os   sp   for   java   

原文地址:http://www.cnblogs.com/a972290869/p/4163947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!