POJ3373:Changing Digits(记忆化)
来源:互联网 发布:coc墙升级数据 编辑:程序博客网 时间:2024/05/16 12:16
Description
Given two positive integers n and k, you are asked to generate a new integer, say m, by changing some (maybe none) digits of n, such that the following properties holds:
- m contains no leading zeros and has the same length as n (We consider zero itself a one-digit integer without leading zeros.)
- m is divisible by k
- among all numbers satisfying properties 1 and 2, m would be the one with least number of digits different from n
- among all numbers satisfying properties 1, 2 and 3, m would be the smallest one
Input
There are multiple test cases for the input. Each test case consists of two lines, which contains n(1≤n≤10100) and k(1≤k≤104, k≤n) for each line. Both n and k will not contain leading zeros.
Output
Output one line for each test case containing the desired number m.
Sample Input
226191033219
Sample Output
2119103
看到一篇博客解释的非常详细,在此引用一下,传送门:http://blog.csdn.net/lyy289065406/article/details/6698787/
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;char s[105];int dp[105][10],tem[105],a[105],mod,len,f[105][10005],k;void init(){ int i,j; for(i = 0; i<=9; i++) dp[1][i] = i%mod; for(i = 2; i<=len; i++) for(j = 0; j<=9; j++) dp[i][j] = (dp[i-1][j]*10)%mod; memset(f,0,sizeof(f));}bool dfs(int cnt,int l,int k)//从l开始,还要改变cnt个数字,使得余数k变为0{ int i,j; if(!k) { for(i = 0; i<len; i++) printf("%d",tem[i]); printf("\n"); return 1; } if(!cnt) return 0; if(l>len-1) return 0; if(f[l][k]>=cnt) return 0; for(i = l; i<len; i++)//高位开始,从小取 { for(j = 0; j<a[i]; j++) { if(!i && !j) continue; tem[i] = j; int temp = (k-dp[len-i][a[i]]+dp[len-i][j])%mod; if(temp<0) temp+=mod; if(dfs(cnt-1,i+1,temp)) return 1; } tem[i] = a[i]; } for(i = len-1; i>=l; i--)//低位开始,要变大 { for(j = a[i]+1; j<=9; j++) { if(!i && !j) continue; tem[i] = j; int temp = (k-dp[len-i][a[i]]+dp[len-i][j])%mod; if(temp<0) temp+=mod; if(dfs(cnt-1,i+1,temp)) return 1; } tem[i] = a[i]; } f[l][k] = cnt;//l开始取cnt个数不能使得k变为0 return 0;}int main(){ int i,j; while(~scanf("%s",s)) { scanf("%d",&mod); len = strlen(s); init(); k = 0; for(i = 0; i<len; i++) tem[i] = a[i] = s[i]-'0'; for(i = len-1; i>=0; i--) k = (k+dp[len-i][a[i]])%mod; for(i = 0; i<len; i++) if(dfs(i,0,k)) break; } return 0;}
0 0
- POJ3373:Changing Digits(记忆化)
- poj3373--Changing Digits(DFS+剪枝///记忆化)
- Changing Digits poj3373
- POJ3373-Changing Digits
- poj3373——Changing Digits
- pku 3373 Changing Digits(记忆化搜索)
- POJ 3373 Changing Digits (记忆化搜索)
- poj 3373 Changing Digits(记忆化搜索)
- poj 3373 Changing Digits
- POJ 3373 Changing Digits
- POJ 3373 Changing Digits
- POJ 3373--Changing Digits
- POJ 3373 Changing Digits
- poj 3373 Changing Digits
- POJ-3373-Changing Digits
- POJ 3373 Changing Digits 好蛋疼的DP
- POJ 3373 Changing Digits(DP)
- Codeforces 56D Changing a String 编辑距离 记忆化dp
- BUPT2014新生暑假个人排位赛07
- hdu 1061 Rightmost Digit
- 背包问题
- 【leetcode】Best Time to Buy and Sell Stock I && II && III
- Java基础加强-java5的泛型
- POJ3373:Changing Digits(记忆化)
- cocos2d-x-3.2alpha0 如何创建新工程
- HDU 1281 棋盘游戏(匈牙利算法 二分最大匹配)
- C#:当选择DataGridView控件中的行时显示不同的颜色
- hdu 4858 项目管理 图分治 (复合算法)
- 我是无能无力了
- c++ primer(第五版)笔记 第一章
- 6天通吃树结构
- poj1481