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 ≤ ...
随机推荐
- CentOS 7 IPv6关闭
你可以用两个方法做到这个.方法 1编辑文件/etc/sysctl.conf,vi /etc/sysctl.conf添加下面的行:net.ipv6.conf.all.disable_ipv6 =1net ...
- 原创:MVC 5 实例教程(MvcMovieStore 新概念版:mvc5.0,EF6.01) - 3、创建项目
说明:MvcMovieStore项目已经发布上线,想了解最新版本功能请登录 MVC 影视(MvcMovie.cn) 进行查阅.如需转载,请注明出处:http://www.cnblogs.com/Dod ...
- [转载]SQL Server行列转换实现
可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P 完整语法: table_source PIVOT( 聚合函数(value_ ...
- Jenkins启动Tomcat时提示Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
Jenkins构建提示: [SSH] executing...Neither the JAVA_HOME nor the JRE_HOME environment variable is defi ...
- 新手上路,django学习笔记(1) 环境部署
很多年没写代码了,以前学的C#,用ASP.NET,但是最近几年没落了,JAVA在崛起,最近感觉Python比较火,总是在各种技术场合听到Python,或者身边的朋友在讨论Python,所以突然想学习一 ...
- Eclipse中Tomcat Server启动后马上又自动停止报错Address已经使用8005端口 Can't assign requested address (Bind failed)
Eclipse中Tomcat Server启动后马上又自动停止报错 Can't assign requested address (Bind failed) ,打开Tomcat Server的配置页面 ...
- Mac下su命令提示su:Sorry的解决办法
用 sudo su - 输入密码,获得root权限
- php—Smarty-缓存1(25)
一. 缓存原理: IE:将资源文件保存至本地 Smarty:将缓存保存到服务器 编译 < 缓存 < 静 ...
- php中的XML DOM(10)
1.PHP DOM (1) Php中的DOM跟javascript不一样,属性不用另外增加一个节点 2.主要类 DOMDocument :文档类 DOMNodeList :节点列表类 DOMNode ...
- “全栈2019”Java多线程第六章:中断线程interrupt()方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...