[回溯]Super Number uva10624
来源:互联网 发布:手机淘宝开店开通不了 编辑:程序博客网 时间:2024/05/19 13:09
Problem B
Super Number
Input: Standard Input
Output: Standard Output
Time Limit: 3 Seconds
Don't you think 162456723 very special? Look at the picture below if you are unable to find its speciality. (a | b means ‘b is divisible by a’)
Figure: Super Numbers
Given n, m (0 < n < m < 30), you are to find a m-digit positive integer X such that for every i (n <= i <= m), the first i digits of X is a multiple ofi. If more than one such X exists, you should output the lexicographically smallest one. Note that the first digit of X should not be 0.
Input
The first line of the input contains a single integer t(1 <= t <= 15), the number of test cases followed. For each case, two integers n and m are separated by a single space.
Output
For each test case, print the case number and X. If no such number, print -1.
Sample Input Output for Sample Input
2
1 10
3 29
Case 1: 1020005640
Case 2: -1
Problemsetter: Rujia Liu, Member of Elite Problemsetters' Panel
Special Thanks to:
Monirul Hasan (Alternate solution)
Shahriar Manzoor (Figure Drawing)
题意:给定两个数字n,m;求一个长度为m的数字使得这个数字的任意前i(n=<i<=m)个数字组成的数能被i整数。
这道题按照我的思路就是回溯暴力。暂时没想到更好的方法了。
#include<iostream>#include<cstring>using namespace std;int n,m,tag;int arry[50];int mod(int cnt){ int sum=0; for(int i=0;i<cnt;i++) { sum=(sum*10+arry[i])%cnt; } return sum;}void dfs(int pos){ if(pos==m) { tag=1; return; } if(tag) return; for(int i=0;i<=9;i++) { arry[pos]=i; if(pos<n-1||(pos>=n-1&&!mod(pos+1))) { dfs(pos+1); if(tag) return; } }}int main(){ int k; cin>>k; for(int t=1;t<=k;t++) { tag=0; cin>>n>>m; memset(arry,0,sizeof(arry)); int i,j; for(i=1;i<=9;i++) { arry[0]=i; dfs(1); if(tag) break; } cout<<"Case "<<t<<": "; if(tag) { for(i=0;i<m;i++) cout<<arry[i]; } else cout<<"-1"; cout<<endl; } return 0;}
- [回溯]Super Number uva10624
- uva10624 - Super Number
- UVA10624 - Super Number(dfs)
- uva 10624 Super Number 回溯
- UVA - 10624 Super Number(回溯)
- uva 10624 - Super Number
- uva 10624Super Number
- UVa 10624 - Super Number
- UVA 10624 - Super Number
- 10624 - Super Number
- UVa 10624 - Super Number
- 10624 - Super Number
- UVA - 10624 Super Number
- leetcode Super Ugly Number
- Super Ugly Number | LeetCode
- leetcode Super Ugly Number
- Super Ugly Number
- 313Super Ugly Number
- 网络编程fhd的基础学习一
- 讲解MMU的好文章(一)
- Android中dp转px
- hdu 1030 Delta-Wave+
- 讲解MMU的好文章(二)
- [回溯]Super Number uva10624
- The type org.apache.axiom.om.OMElement cannot be resolved. It is indirectly referenced from require
- 查找2006-2007或2007-2008年度有欠费并且在之后年度有交款的sql语句
- 关于jvm五块内存的相关知识
- 讲解MMU的好文章(三)
- 从S0状态进入sleep状态的过程
- 基于xmpp协议,openfire服务器,处理被挤下线,或因网络掉线,第二次无法登录的问题
- 论文读书笔记-A text clustering framework for information retrieval
- Sqrt(x)