LightOJ 1214 Large Division
Large Division
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if there exists an integer c such that a = b * c.
Input
Input starts with an integer T (≤ 525), denoting the number of test cases.
Each case starts with a line containing two integers a (-10200 ≤ a ≤ 10200) and b (|b| > 0, b fits into a 32 bit signed integer). Numbers will not contain leading zeroes.
Output
For each case, print the case number first. Then print 'divisible' if a is divisible by b. Otherwise print 'not divisible'.
Sample Input
6
101 101
0 67
-101 101
7678123668327637674887634 101
11010000000000000000 256
-202202202202000202202202 -101
Sample Output
Case 1: divisible
Case 2: divisible
Case 3: divisible
Case 4: not divisible
Case 5: divisible
Case 6: divisible
解题思路:
本题考查大数运算,每次测试给定一个整数T为测试数量,之后跟随T行,每行都给出两个数字,第一个数字是一个大于10200 且小于10200的数字,第二个数字是一个32位的int,要求计算第一个数是否可以整除第二个数,若可以整除输出divisible否则输出not divisible。
由于第一个数字超出可以直接存储的范围太多,我们不能直接对其进行运算,那么就换一种运算方式,按位对其进行运算,至于如何按位运算,小学时学过对数字运算极为方便的方法——竖式。
以12345678 / 9为例子

由于题目中已经告诉我们输入的数字中不包含先导0,所以按照小学的算法我们用第一个数首位除数除以第二个数,9 除以 1得0余1,继续运算将余数1与下一个数结合得到12, 12除以9得1余3,将3与下一个数结合,得到33,以此类推直到运算到最后一位,我们便可以得到最终的余数。之后判断余数是否为0就可以得出答案。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = +;
struct Big{ //Big储存输入的大数
int num[maxn];
int len;
Big(){
len = ;
memset(num, , sizeof(num));
}
};
int divide(Big a, int b){ //传入被除数与除数
Big c;
LL mod = ; //mod记路余数
for(int i = ; i < a.len; i++){ //从首位开始按位运算
mod = mod * + a.num[i]; //mod要用long long 因为mod * 10后可能超int范围
if(mod >= b) //如果除不开就去计算下一位,除的开就进行计算
mod = mod % b; //当前值除以b找到新的余数
}
return (int)mod;
}
int main()
{
int t, b; //t为测试数量
string str; //str记录输入第一个数字
while(scanf("%d", &t) != EOF){
for(int i = ; i <= t; i++){
cin >> str >> b;
Big a; //a记录第一个数
if(str[] == '-'){ //第一个数字如果是负数就去掉符号
int cnt = ;
a.len = str.size() - ;
for(string::iterator it = ++str.begin(); it != str.end(); it++){
a.num[cnt++] = *it - '';
}
}else{ //正数直接记录入a
int cnt = ;
a.len = str.size();
for(string::iterator it = str.begin(); it != str.end(); it++){
a.num[cnt++] = *it - '';
}
}
if(b < )
b = -b;
if(!divide(a, b)){ //只要mod为0就可以整除,否则不能整除
printf("Case %d: divisible\n", i);
}else{
printf("Case %d: not divisible\n", i);
}
}
}
return ;
}
LightOJ 1214 Large Division的更多相关文章
- LightOJ 1214 Large Division 水题
java有大数模板 import java.util.Scanner; import java.math.*; public class Main { public static void main( ...
- light oj 1214 - Large Division
1214 - Large Division PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB G ...
- light oj 1214 - Large Division 大数除法
1214 - Large Division Given two integers, a and b, you should check whether a is divisible by b or n ...
- 1214 - Large Division -- LightOj(大数取余)
http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...
- lightoj 1214
lightoj 1214 Large Division (大数除法) 链接:http://www.lightoj.com/volume_showproblem.php?problem=1214 题意 ...
- LightOJ1214 Large Division —— 大数求模
题目链接:https://vjudge.net/problem/LightOJ-1214 1214 - Large Division PDF (English) Statistics Forum ...
- (大数 求余) Large Division Light OJ 1214
Large Division Given two integers, a and b, you should check whether a is divisible by b or not. We ...
- LightOJ1214 Large Division
/* LightOJ1214 Large Division http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1 ...
- K - Large Division 判断a是否是b的倍数。 a (-10^200 ≤ a ≤ 10^200) and b (|b| > 0, b fits into a 32 bit signed integer). 思路:取余;
/** 题目:K - Large Division 链接:https://vjudge.net/contest/154246#problem/K 题意:判断a是否是b的倍数. a (-10^200 ≤ ...
随机推荐
- struts2+ckeditor配置图片上传
又是一个漫漫长夜. 公司的编辑器坏了,用的是百度编辑器,上传图片的网址被框架给拦截了,我们本地怎么测试都没问题,放到服务器就这样了.和老李找了半天,疯了,没原因的. 笔者以前用过jsp+ckedito ...
- TFS (Team Foundation Server) 2013集成Maven构建
Team Foundation Server原生就支持跨平台的构建,包括Ant和Maven两种构建方式.通过配置构建服务器,连接TFS源代码库,可以实现持续集成构建,自动检测代码库健康状况,进而实现自 ...
- 遇到了IE10不能登录的问题,很早就有解决方案了
1..net 2.0 的程序,请打开项目,打开vs开发环境的工具菜单下的 Package Manager Console ,中文名:程序包管理控制台,在打开的控制台中输入如下命令:Install-P ...
- solr分词一:mmseg4j
刚接触Lucene2.x和Solr2.x的时候,谈到中文分词,会让我立即想到用庖丁中文分词,庖丁中文分词因巨大的中文词库以及支持不限制个数的用户自定义词库,而且是纯文本格式,一行一词,使用后台线程检测 ...
- HTML解析器软件
HTML解析器软件 HTML文档解析器 HTMLParser HTML Parser 是一个对HTML进行分析的快速实时的解析器,最新的发行版本是1.6,另外2.0的开发版本已经两年没有进展了.示例代 ...
- (zxing.net)一维码UPC E的简介、实现与解码
UPC(Universal Product Code)码是最早大规模应用的条码,其特性是一种长度固定.连续性的条 码,目前主要在美国和加拿大使用,由于其应用范围广泛,故又被称万用条码. UPC码仅可 ...
- mysql 判断表字段或索引是否存在 - 举一反三
判断字段是否存在: DROP PROCEDURE IF EXISTS schema_change; DELIMITER // CREATE PROCEDURE schema_change() BEGI ...
- JS 返回上一页并刷新,但不用重新加载整个页面(ajax实现)
需求 有三个页面A.B.C,点击A=>B,点击B=>C,在C中添加内容,点击确定返回到B,此时B页面需重新加载新的内容.再次点击B的返回按钮,希望返回到A而不是C. ===== 2017/ ...
- IECapt、CutyCapt 生成网页快照
IECapt.CutyCapt 生成网页快照 http://iecapt.sourceforge.net/ http://cutycapt.sourceforge.net/ 1.以管理员身份 运行c ...
- collections 模块常用方法学习
前情提要: 1:模块介绍 个人认为就是 python自带的骚操作模块.如果基础能力够给力的话,完全用不到 个人认为解析式才是装逼神奇,用模块的都是伪娘 2:deque 双向列表 from coll ...