LightOJ1214 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 |
Output for Sample Input |
|
6 101 101 0 67 -101 101 7678123668327637674887634 101 11010000000000000000 256 -202202202202000202202202 -101 |
Case 1: divisible Case 2: divisible Case 3: divisible Case 4: not divisible Case 5: divisible Case 6: divisible |
题意:给出两个数a, b,问能否被b整除。
题解:基础数论。简单的同余定理应用,将a作为字串储存,相当于每x位(和b同位)模b一次,得到余数时相当于将这个区间改写成这个余数,移动区间继续运算。最终余数为零时代表可以被整除,非零则否。
补充:其实可以想成这样,三位数就是百进制,四位数就是千进制的同余定理。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#define ll long long
using namespace std; char a[];
int main()
{
int T, x, s;
ll t, b;
scanf("%d", &T);
for(int i=; i<=T; i++)
{
scanf("%s %lld", a, &b);
x=strlen(a);
if(a[]=='-')//注意负数变正
{
s=;
t=a[]-'';
}
else
{
s=;
t=a[]-'';
}
t=t%abs(b);
for(int j=s; j<x; j++)
{
t=(t*+a[j]-'')%abs(b); //同余定理的应用
} if(t==)
{
printf("Case %d: divisible\n", i);
}
else
printf("Case %d: not divisible\n", i); } }
LightOJ1214 Large Division 基础数论+同余定理的更多相关文章
- LightOJ1214 Large Division
/* LightOJ1214 Large Division http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1 ...
- 51nod 1433 0和5【数论/九余定理】
1433 0和5 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 小K手中有n张牌,每张牌上有一个一位数的数,这个 ...
- LightOJ1214 Large Division —— 大数求模
题目链接:https://vjudge.net/problem/LightOJ-1214 1214 - Large Division PDF (English) Statistics Forum ...
- L - Large Division (大数, 同余)
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...
- FZU 1057 a^b 【数论/九余定理】
Accept: 1164 Submit: 3722Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 对于任 ...
- 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 ≤ ...
- 1214 - Large Division -- LightOj(大数取余)
http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...
- Light oj 1214-Large Division (同余定理)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我 ...
- (大数 求余) 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 ...
随机推荐
- OOP 2.1 类和对象的基本概念2
1.成员函数的另一种写法:类的成员函数和类的定义分开写 e.g. class rectangle { public: int w,h; int area(); int p(); void init(i ...
- IO流详解
目录 IO流 IO流概述及其分类 IO概念 流按流向分为两种: 流按操作类型分为两种: 常用的IO流类 字节流的抽象父类: 字符流的抽象父类: InputStream & FileInputS ...
- 第八章 Mysql运算符
算术运算符 符号 表达式形式 作用 + x1+x2 加法 - x1-x2 减法 * x1*x2 乘法 / x1/x2 除法 div x1 div x2 同上 % x1%x2 取余 mod mod(x1 ...
- css3 关于文字,字体属性(转载)
1.text-overflow属性(实现省略号效果) text-overflow用来设置是否使用一个省略标记(…)标示对象内文本的溢出. [语法] ❤text-overflow只是用来说明文字溢出时用 ...
- 3dContactPointAnnotationTool开发日志(三二)
今天就是看怎么把论文的python源码预测出来的smpl模型的姿势和形状参数弄到unity版本的smpl里,但是python版本的和unity版本的不一样. 先看看他的fit_3d.py: ...
- PAT 甲级 1129 Recommendation System
https://pintia.cn/problem-sets/994805342720868352/problems/994805348471259136 Recommendation system ...
- 关于解决java读取excel文件遇空行抛空指针的问题 !
关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...
- [Google] 看雪论坛: 安卓碎片化的情况
2018年10月28日早间消息,谷歌方面发布了Android各版本的最新份额数据,截止到10月26日.即便是已经推出3个月了,Android 9 Pie系统的用户数仍旧没有超过0.1%,导致未出现在榜 ...
- C# 委托和事件高级进阶
本篇文章主要采用理论和代码实例相结合方式来论述委托和事件,涉及到一些边界技术,如软件架构的OCP原则(开-闭原则), 软件架构解耦,设计模式(Sender-Order)和事件驱动模型,有一定难度和深度 ...
- 【.Net】HttpClient 的使用
class Program { public static HttpClient Client; static HttpResponseMessage response; static void Ma ...