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的更多相关文章

  1. LightOJ 1214 Large Division 水题

    java有大数模板 import java.util.Scanner; import java.math.*; public class Main { public static void main( ...

  2. light oj 1214 - Large Division

    1214 - Large Division   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB G ...

  3. 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 ...

  4. 1214 - Large Division -- LightOj(大数取余)

    http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...

  5. lightoj 1214

    lightoj 1214 Large Division  (大数除法) 链接:http://www.lightoj.com/volume_showproblem.php?problem=1214 题意 ...

  6. LightOJ1214 Large Division —— 大数求模

    题目链接:https://vjudge.net/problem/LightOJ-1214 1214 - Large Division    PDF (English) Statistics Forum ...

  7. (大数 求余) 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 ...

  8. LightOJ1214 Large Division

    /* LightOJ1214 Large Division http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1 ...

  9. 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 ≤ ...

随机推荐

  1. [python01] python列表,元组对比Erlang的区别总结

    数据结构是通过某种方式组织在一起的数据元素的集合,这些数据元素可以是数字,字符,甚至可以是其他的数据结构. python最基本的数据结构是sequence(序列):6种内建的序列:列表,元组,字符串, ...

  2. 常用脚本--生成指定表的INSERT 语句

    --================================================= --摘抄自http://www.cnblogs.com/sunth/archive/2013/0 ...

  3. Solr查询query效果对比

    q条件 默认分词(org.apache.solr.analysis.TokenizerChain) "parsedquery" IK分词(org.wltea.analyzer.lu ...

  4. solr特点三: boost(改变默认打分排序)

    有时候默认的字段打分不能满足我们的需要,如我们想把关键词出现在标题中的优先显示. 测试于:Solr 4.5.1, Jdk 1.6.0_45, Tomcat 6.0.37 | CentOS 5.7 实现 ...

  5. SQL 从数据库中随机取n条数据

    用NEWID()方法. * ,NEWID() AS random from [toblename] order by random 其中的1可以换成其他任意整数,表示取的数据条数

  6. Js异常捕获

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. WPF XamlObjectWriterException:无法创建未知类型"Grid"

    using (FileStream fs = new FileStream("UnitFile/Report2.xaml", FileMode.Open)) { rootEleme ...

  8. lamp-linux3

    LAMP编程之Linux(3) 一.权限管理 1.权限介绍(重点) 在Linux中分别有读.写.执行权限: 读权限: 对于文件夹来说,读权限影响用户是否能够列出目录结构 对于文件来说,读权限影响用户是 ...

  9. [CSS3] 各种角度的三角形绘制

    #triangle-up { width:; height:; border-left: 50px solid transparent; border-right: 50px solid transp ...

  10. jmeter - jp@gc - Active Threads Over Time(多台负载用户)

    问题: 线程数设置:30,远程启动2台机子 查看 jp@gc - Active Threads Over Time图,发现只统计了1台机子的线程数,线程数并不是60: 解决办法: 官方文档中提到: 1 ...