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 ≤ ...
随机推荐
- Sql Server中使用存储过程来实现一些时间差的改变
Sql Server中的时间差是使用DATEDIFF来是现的 语法如下:DATEDIFF(要显示时间格式,开始时间,结束时间) 比如:DATEDIFF(minute,'2019-2-28 8:30', ...
- javacript 实现瀑布流原理和效果, 滚动加载图片【图文解析 附源码】
先科普下瀑布流吧 瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pin ...
- 遇到问题-----cas4.2.x登录成功后报错No principal was found---cas中文乱码问题完美解决
情况 我们之前已经完成了cas4.2.x登录使用MongoDB验证方式并且自定义了加密. 单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密 但是悲剧的是 ...
- Mysql 中日期类型bigint和datetime互转
MySql数据库中字段类型bigint 长度是10位的 mysql> select (from_unixtime(1554047999))as datatime;+--------------- ...
- ceph: health_warn clock skew detected on mon的解决办法
造成集群状态health_warn:clock skew detected on mon节点的原因有两个,一个是mon节点上ntp服务器未启动,另一个是ceph设置的mon的时间偏差阈值比较小. 排查 ...
- django系列8.5--使用装饰器(视图函数中)实现用户登录状态检验
views.py def session_auth(fn): def inner(request,*args,**kwargs): status = request.session.get('sess ...
- djang系列5.5-- 图书管理系统实例
一.表格设计 E-R图 分析图 models.py from django.db import models # Create your models here. class Author(model ...
- Web Api 端点设计 与 Oauth
最近一直看这方面的东西,总结如下: 在后续会进行实例demo演示,本篇进行理论详解. 下篇相关博客: <Web Api 内部数据思考 和 利用http缓存优化 Api> <API接口 ...
- 苹果笔记本调整 pycharm 字体大小的地方
我想,对于习惯了使用windows版本 或者 乌班图版本 的pycharm 的人而言, mac版本调节字体的地方藏的实在是太坑爹了.
- angular核心原理解析3:指令的执行过程
指令的执行过程分析. 我们知道指令的执行分两个阶段,一个是compile,一个是link. 我们可以在指令中自定义compile和link. 首先,我们来讲解如何自定义link函数 举个例子: < ...