http://acm.hnu.cn/online/?action=problem&type=show&id=12813&courseid=267
Restore Calculation |
Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB |
Total submit users: 19, Accepted users: 17 |
Problem 12813 : No special judgement |
Problem description |
The Animal School is a primary school for animal children. You are a fox attending this school. One day, you are given a problem called "Arithmetical Restorations" from the rabbit teacher, Hanako. Arithmetical Restorations are the problems like the following:
You are clever in mathematics, so you immediately solved this problem. Furthermore, you decided to think of a more difficult problem, to calculate the number of possible assignments to the given Arithmetical Restorations problem. If you can solve this difficult problem, you will get a good grade. Shortly after beginning the new task, you noticed that there may be too many possible assignments to enumerate by hand. So, being the best programmer in the school as well, you are now trying to write a program to count the number of possible assignments to Arithmetical Restoration problems. |
Input |
The input is a sequence of datasets. The number of datasets is less than 100. Each dataset is formatted as follows. A Each dataset consists of three strings, A, B and C. They indicate that the sum of A and B should be C. Each string consists of digits ( It is guaranteed that each string contains between 1 and 50 characters, inclusive. You can also assume that the lengths of three strings are equal. The end of input is indicated by a line with a single zero. |
Output |
For each dataset, output the number of possible assignments to the given problem modulo 1,000,000,007. Note that there may be no way to solve the given problems because Ms. Hanako is a careless rabbit. |
Sample Input |
3?4 12? 5?6 ?2?4 5?7? ?9?2 ????? ????? ????? 0 |
Sample Output |
2 40 200039979 |
Judge Tips |
The answer of the first dataset is 2. They are shown below.
|
题意:一共有多少种可以成立的方法。
思路:三重for循环,dp[i][0]表示从右往左第i位不进位的情况数量,dp[i][1]表示仅为的情况数量。
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
char s[4][100];
int num[4][100][2];
long long dp[100][2],M=1000000007;
int main()
{ int i,j,k,len,a,b,c,num1,num2,num3,num4,p1,p2,p3;
while(~scanf("%s",s[1]))
{ if(s[1][0]=='0')
break;
len=strlen(s[1]);
scanf("%s",s[2]);
scanf("%s",s[3]);
for(i=1;i<=3;i++)
{ for(j=2;j<=len;j++)
if(s[i][j-1]=='?')
{ num[i][j][0]=0;
num[i][j][1]=9;
}
else
{ num[i][j][0]=s[i][j-1]-'0';
num[i][j][1]=s[i][j-1]-'0';
}
if(s[i][0]=='?')
{ num[i][1][0]=1;
num[i][1][1]=9;
}
else
{ num[i][1][0]=s[i][1-1]-'0';
num[i][1][1]=s[i][1-1]-'0';
}
}
memset(dp,0,sizeof(dp));
dp[len+1][0]=1;
for(i=len;i>=1;i--)
{ num1=0;num2=0;num3=0,num4=0;
for(a=num[1][i][0];a<=num[1][i][1];a++)
for(b=num[2][i][0];b<=num[2][i][1];b++)
for(c=num[3][i][0];c<=num[3][i][1];c++)
{ if((a+b)%10==c)
{ if(a+b<10)
num1++;
else
num2++;
}
if((a+b+1)%10==c)
{ if(a+b+1<10)
num3++;
else
num4++;
}
}
dp[i][0]=(dp[i+1][0]*num1+dp[i+1][1]*num3)%M;;
dp[i][1]=(dp[i+1][0]*num2+dp[i+1][1]*num4)%M;
}
printf("%I64d\n",dp[1][0]);
}
}