CodeForces 260A Adding Digits
这道题目的意思是给你提供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的更多相关文章
- Codeforces Gym 100531D Digits 暴力
Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...
- Codeforces 998D. Roman Digits 【打表找规律】
<题目链接> 题目大意: 现在有无限个 1,5,10,50这四个数字,从中恰好挑选n个数字,问你这些数字的和总共有多少种不同的情况. 解题分析: 由于此题 n 的范围特别大,达到了1e9, ...
- Codeforces Round #158 (Div. 2)
A. Adding Digits 枚举. B. Ancient Prophesy 字符串处理. C. Balls and Boxes 枚举起始位置\(i\),显然\(a_i \le a_j, 1 \l ...
- 1269 - Consecutive Sum
1269 - Consecutive Sum PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...
- [codeforces 509]C. Sums of Digits
[codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...
- 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 ...
- 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 ...
- 【codeforces 509C】Sums of Digits
[题目链接]:http://codeforces.com/contest/509/problem/C [题意] 给你一个数组b[i] 要求一个严格升序的数组a[i]; 使得a[i]是b[i]各个位上的 ...
随机推荐
- linux下挂载第二块硬盘
1.第一步:添加硬盘/新建分区(fdisk) a.查看当前系统所有硬盘及分区情况:fdisk -lb.在指定的硬盘(例:/dev/sda)上创建分区:fdisk /dev/sda , 根据提示进行下一 ...
- ORA-20000:ORU-10027:buffer overflow,limit of 10000 bytes错误4
今天再测试一个存储过程时,用DBMS_OUTPUT.PUT_LINE输出时,报 ORA-20000:ORU-10027:buffer overflow,limit of 10000 bytes SQL ...
- 超强1000 JQuery插件
转载:超强1000个jquery插件! http://www.cnblogs.com/chu888chu888/archive/2011/12/18/2292014.html
- 手机SIM卡介绍 三类不同标准的SIM卡
SIM卡的全称是Subscriber Identity Module,翻译过来也叫客户识别模块,也叫做智能卡.用户身份识别卡.这块小小的芯片可以存储用户的号码.信息,以及一定数量的联系人数据,配合我们 ...
- [Django实战] 第8篇 - 分页列表
当用户登录成功后,首先看到的是他自己之前提交的任务列表,本篇将实现该页面. 视图(views.py)里定义如下: from django.core.paginator import Paginator ...
- 一个发送电子邮件的bash脚本
第一个命令行參数会被当邮件内容发送出去 #!/bin/bash #@author Liuyang #@date 2015-02-15 Recipients=xxx@xx.com FROM=xxx@xx ...
- weblogic的ejb远程调用
这是一篇对EJB远程调用的简单范例. 1.环境:win7 + weblogic 12c + myeclipse8.5 2.目的:实现在myeclispe中对weblogic中EJ ...
- Scala函数---既存类型
语法: Type ::= InfixType ExistentialClauses ExistentialClauses ::= „forSome‟ „{‟ ExistentialDcl {semi ...
- js获取来源和当前域名
参考:http://www.cnblogs.com/zuosong160522/p/5755615.html http://www.oicqzone.com/pc/2014113020362.html
- js和jQuery写简单下拉菜单
1.jQuery写法 <head> <meta http-equiv="Content-Type" content="text/html; charse ...