首页 > 代码库 > hdu 3419
hdu 3419
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.
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 41 1 10 0 0
Sample Output
74Note:The valid solutions for the second sample input-output are as following:2*3 =62*4 =83*2 =64*2 =8
Author
Muhammed Hedayet
Source
HDOJ Monthly Contest – 2010.06.05
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。