Uva 11361 Investigating Div-Sum Property 解题报告(递推)
来源:互联网 发布:品牌网络推广计划书 编辑:程序博客网 时间:2024/05/19 18:38
I
Next generation contest – 4
Time Limit – 4 secs
Investigating Div-Sum Property
An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is divisible by 3 and 12(3+7+0+2) is also divisible by 3. This property also holds for the integer 9.
In this problem, we will investigate this property for other integers.
Input
The first line of input is an integer T(T<100) that indicates the number of test cases. Each case is a line containing 3 positive integers A, B and K. 1 <= A <= B < 2^31 and 0<K<10000.
Output
For each case, output the number of integers in the range [A, B] which is divisible by K and the sum of its digits is also divisible by K.
Sample Input
Output for Sample Input
3
1 20 1
1 20 2
1 1000 4
20
5
64
解题报告:刘汝佳老师书上的题目。显然,这种题目一般都很恶心。思路可能并不难,但是代码实现却是比较复杂,码力强才能敲得又快又好(本人需加强……)。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int k;int tenPow[11];int top;int digital[11];int mod(int n){ return (n%k+k)%k;}void decomposition(int a){ top=0; while(a) { digital[top++] = a%10; a/=10; }}int matrix[11][90][90];void calMatrix(int n){ memset(matrix, 0, sizeof(matrix)); for(int i=0;i<10;i++) matrix[0][i%k][i%k]++; for(int i=1;i<n;i++) for(int j=0;j<10;j++) { for(int a=0;a<k;a++) for(int b=0;b<k;b++) { matrix[i][mod(a+j)][mod(b+tenPow[i]%k*j)]+=matrix[i-1][a][b]; } }}int calResult(){ int front=0; int res=0; int sum[12]; memset(sum,0,sizeof(sum)); for(int i=top-1;i>=0;i--) sum[i] = sum[i+1]+digital[i]; for(int i=top-1;i>0;i--) { for(int j=0;j<digital[i];j++) { res+=matrix[i-1][mod(-sum[i+1]-j)][mod(-tenPow[i]%k*front)]; front++; } front = (front*10)%k; } for(int j=0;j<=digital[0];j++) if(mod(front+j)==0 && mod(sum[1]+j)==0) res++; return res;}void work(){ int a,b; scanf("%d%d%d",&a,&b,&k); if(k>=90) { puts("0"); return; } decomposition(b); calMatrix(top-1); int ans = calResult(); decomposition(a-1); ans -= calResult(); printf("%d\n", ans);}void init(){ tenPow[0]=1; for(int i=1;i<10;i++) tenPow[i] = tenPow[i-1]*10;}int main(){ init(); int T; scanf("%d",&T); while(T--) work();}
- Uva 11361 Investigating Div-Sum Property 解题报告(递推)
- uva 11361 Investigating Div-Sum Property
- Investigating Div-Sum Property Uva 11361
- UVA - 11361 Investigating Div-Sum Property
- uva 11361 Investigating Div-Sum Property
- UVa 11361 Investigating Div-Sum Property
- uva 11361 Investigating Div-Sum Property
- UVA 11361Investigating Div-Sum Property
- UVa 11361Investigating Div-Sum Property
- UVa 11361 Investigating Div-Sum Property
- UVA 11361 - Investigating Div-Sum Property(数位DP)
- UVa 11361 - Investigating Div-Sum Property (数位DP)
- Investigating Div-Sum Property UVA
- 11361 - Investigating Div-Sum Property
- UVa 11361 Investigating Div-Sum Property / 数位DP
- uva 11361 - Investigating Div-Sum Property(数位dp)
- UVA 11361 Investigating Div-Sum Property(数位DP)
- UVa 11361 Investigating Div-Sum Property 数字和与倍数
- 使用WdatePicker.js显示日期选择控件,并进行设置
- 边缘检测(7)Canny边缘算子
- 使用jetty-maven-plugin插件进行测试
- 小布老师-PL_SQL(第四讲)
- qduoj-36 自然数拆分 (高效深搜)
- Uva 11361 Investigating Div-Sum Property 解题报告(递推)
- Python string
- 1047. Student List for Course
- 算法设计与分析第二周作业
- 思维导图@
- 第二周作业--------判断一个正整数是否为质数的算法
- 10.矩阵乘法
- 跨线程更新界面(winform下 C#)
- 1048. Find Coins