L1-046. 整除光棍(模拟除法)
2024-10-08 15:55:20
题意:
这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1、11、111、1111等。传说任何一个光棍都能被一个不以5结尾的奇数整除。比如,111111就可以被13整除。 现在,你的程序要读入一个整数x,这个整数一定是奇数并且不以5结尾。然后,经过计算,输出两个数字:第一个数字s,表示x乘以s是一个光棍,第二个数字n是这个光棍的位数。这样的解当然不是唯一的,题目要求你输出最小的解。
提示:一个显然的办法是逐渐增加光棍的位数,直到可以整除x为止。但难点在于,s可能是个非常大的数 —— 比如,程序输入31,那么就输出3584229390681和15,因为31乘以3584229390681的结果是111111111111111,一共15个1。
输入格式:
输入在一行中给出一个不以5结尾的正奇数x(< 1000)。
输出格式:
在一行中输出相应的最小的s和n,其间以1个空格分隔。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 20 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int main(){
string xx;
cin >> xx;
int len = xx.size();
int x = 0;
for(int i = 0; i < len; ++i){
x = x * 10 + xx[i] - '0';
}
int cnt = 0;
string s;
string ans;
while(1){
ans = "";
++cnt;
s += "1";
if(cnt < len) continue;
int sum = 0;
for(int i = 0; i < len; ++i){
sum = sum * 10 + 1;
}
int st = len;
if(x > sum){
sum = sum * 10 + 1;
++st;
}
if(cnt < st) continue;
while(st <= cnt){
int shang = sum / x;
ans += shang + '0';
sum %= x;
if(st == cnt) break;
++st;
sum = sum * 10 + 1;
}
if(sum == 0){
break;
}
}
cout << ans << " " << cnt << endl;
return 0;
}
最新文章
- WPF -Enum的三种绑定方法
- <;input type=";file"; id=";camera"; multiple=";multiple"; capture=";camera"; accept=";image/*";>; 上传图片,手机调用相册和摄像头
- nginx服务器在IE下载时,apk,ipa文件变成zip的解决方法
- Windows Azure上搭建SSTP VPN
- 用jstack工具分析java程序
- Servlet &; JSP - Filter
- js模拟触发事件
- 利用php获取图片完整Exif信息类 获取图片详细完整信息类
- sphinx 简介以及安装 以及php拓展开启
- CentOS 7 之找回失落的ifconfig
- Hadoop集群日常运维
- java读取property文件
- 河南多校大一训练赛 G 硬币
- yaf插件类的使用
- cannot update the cursor rep,since it is read-only
- 自动化运维工具Ansible介绍
- Python语言——基础01-环境安装、注释、变量
- ForkJoinPool 源码
- Java Integer 与 int 深刻理解
- ARM中几个典型的汇编指令解析