这道题目的意思是给你提供a, b, n 三个数

a为 输入的数字 ,你需要在a后面加n次 ,每次可以加0-9

但要保证每次加上去的那个数字能被b整除

不过数据规模有点大,用搜索会MLE(即使开了个开栈挂

#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ; string a;
int b, n;
bool flag = false; string Multiply(string s,int x) //大数乘以整形数
{
reverse(s.begin(),s.end());
int cmp=;
for(int i=;i<s.size();i++)
{
cmp=(s[i]-'')*x+cmp;
s[i]=(cmp%+'');
cmp/=;
}
while(cmp)
{
s+=(cmp%+'');
cmp/=;
}
reverse(s.begin(),s.end());
return s;
} string Except(string s,int x) //大数除以整形数
{
int cmp=,ok=;
string ans="";
for(int i=;i<s.size();i++)
{
cmp=(cmp*+s[i]-'');
if(cmp>=x)
{
ok=;
ans+=(cmp/x+'');
cmp%=x;
}
else{
if(ok==)
ans+=''; //注意这里啊。才找出错误
}
}
return ans;
} void dfs(string a, int len){
if(flag) return;
if(n == len){
cout << a << endl;
flag = true;
return;
}
for(int i = ; i < ; ++i){
string num = a;
num.push_back('' + i);
string nn = Except(num, b);
string aa = Multiply(nn, b);
if(aa.compare(num) == ){
dfs(num, len + );
}
}
} int main(){
int i, j, k, t, m, numCase = ;
//string hh = "123";
//cout << atoi(hh.c_str());
while(cin >> a >> b >> n){
dfs(a, );
if(!flag){
cout << "-1" << endl;
} } return ;
}

MLE Code

解题思路:

非常巧妙,就是先在a后面加一个数字0-9如果能整除b,那么易得an后面跟无论多少个0都能整除b

所以就在an后面构造出(n-1)个0即可

无须搜索,超过30层容易爆空间~

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ; int main(){
int i, j, k, t, n, m, numCase = ;
int a, b, num;
//string hh = "123";
//cout << atoi(hh.c_str());
while(cin >> a >> b >> n){
bool flag = false;
for(i = ; i < ; ++i){
num = a * + i;
if(num % b == ){
a = num;
flag = true;
break;
}
}
if(!flag){
cout << - << endl;
return ;
}
cout << num;
for(i = ; i < n; ++i) cout << '';
cout << endl;
} return ;
}

CodeForces 260A Adding Digits的更多相关文章

  1. Codeforces Gym 100531D Digits 暴力

    Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...

  2. Codeforces 998D. Roman Digits 【打表找规律】

    <题目链接> 题目大意: 现在有无限个 1,5,10,50这四个数字,从中恰好挑选n个数字,问你这些数字的和总共有多少种不同的情况. 解题分析: 由于此题 n 的范围特别大,达到了1e9, ...

  3. Codeforces Round #158 (Div. 2)

    A. Adding Digits 枚举. B. Ancient Prophesy 字符串处理. C. Balls and Boxes 枚举起始位置\(i\),显然\(a_i \le a_j, 1 \l ...

  4. 1269 - Consecutive Sum

    1269 - Consecutive Sum    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB ...

  5. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  6. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

  7. CodeForces 1060 B Maximum Sum of Digits

    Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...

  8. Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

  9. 【codeforces 509C】Sums of Digits

    [题目链接]:http://codeforces.com/contest/509/problem/C [题意] 给你一个数组b[i] 要求一个严格升序的数组a[i]; 使得a[i]是b[i]各个位上的 ...

随机推荐

  1. yii教程

    http://www.yiichina.com/doc 官网是很好的参考文档

  2. codeforces 613A. Peter and Snow Blower

    题目链接 给一个多边形, 一个多边形外的定点, 求这个点距离多边形的最短距离和最长距离. 最长距离肯定是和某个顶点的连线, 而最短距离是和点的连线或是和某条边的连线. 对于一条边上的两个点a, b, ...

  3. MongoDB Query

    每条数据格式如下 { "_id" : ObjectId("5383298561aa33a422d8603e"), "day" : " ...

  4. android下文件下载

    public static void downFile(final String url){ new Thread(){ public void run(){ FileOutputStream os= ...

  5. codeigniter IE浏览器下无法登录的解决的方法

    站点搬迁到新的server后,CI 框架做的站点IE浏览器下无法登录.登录时候採用CI自带的SESSION机制,事实上是以COOKIE保存. 网上搜索到IE浏览器不支持域名存在- _. 不是这个原因, ...

  6. C# - String与StringBuilder

    A String object is called immutable (read-only), because its value cannot be modified after it has b ...

  7. hdu4300之KMP&&EKMP

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. HYSBZ1588 营业额统计【Splay】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4366582.html   ---by 墨染之樱花 [题目链接]http://www.lydsy ...

  9. asp.net MVC 使用JQuery.Ajax

    使用到:Jquery.js 以及 Newtonsoft.Json.dll 客户端调用方式: $("#ButAjax").click(function() {$.ajax({type ...

  10. C++对C语言的非面向对象特性扩充(2)

    上一篇随笔写了关于C++在注释,输入输出,局部变量说明的扩充,以及const修饰符与C中的#define的比较,也得到了几位学习C++朋友们的帮助讲解,十分感谢,我也希望欢迎有更多学习C++的朋友一起 ...