【大数取模】HDOJ-1134、CODEUP-1086
1086: 大数取模
题目描述
为了使问题简单,保证B小于100000。
输入
输出
样例输入
12 7
152455856554521 3250
样例输出
5
1521
【概念】
(a+b)%n =(a%n+b%n)%n
(a-b)%n = (a%n-b%n)%n
实话说刚开始我没看懂。
代码:
int mod(char str[],int num)
{
int number[MAXN],i,d = ;
int len = strlen(str);
//将字符串数组转化为数字数组
for(i = ;i < len;i++)
number[i]=str[i]-'';
int remainder=;
for(i = ;i < len;i++)
{
remainder=(remainder * + number[i]) % num;
}
return remainder;
}
举个例子:
123 % 4 = 3
-1- (0 * 10 + 1) % 4 = 1; -2- (1 * 10 + 2) % 4 = 0;
-3- (0 * 10 + 3) % 4 = 3; -4- 得到最终结果3
也就是模拟了除法竖式的过程
【练习题】
- 题目链接:http://arena.acmclub.com/problem.php?id=1086
代码:
#include<cstdio>
#include<cstring>
const int MAXN = ;
int mod(char str[],int num)
{
int number[MAXN],i,d = ;
int len = strlen(str);
//将字符串数组转化为数字数组
for(i = ;i < len;i++)
number[i]=str[i]-'';
int remainder=;
for(i = ;i < len;i++)
{
remainder=(remainder * + number[i]) % num;
}
return remainder;
}
int main(){
char A[MAXN];
int B;
//'~'取反符号,当输入值不符合要求时停止
//while(~scanf("%s %d",A,&B)){ //判断有无结尾符结束
//while(scanf("%s %d",A,&B) != EOF){ //如果两个参数均被读入则返回参数个数
while(scanf("%s %d",A,&B) == ){
printf("%d\n",mod(A,B));
}
return ;
}
- 下面这种方法是运用到了C++ 的大数类,大数模板
代码:
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN = ;
const int DLEN = ;
char str[];
int modd;
class BigNum{
private:
int a[];
int len;
public:
BigNum(){len = ;memset(a,,sizeof(a));}
BigNum(const char*);
int operator %(const int &)const;
};
BigNum::BigNum(const char*s) //将一个字符串类型的变量转化为大数
{
int t,k,index,l,i;
memset(a,,sizeof(a));
l=strlen(s);
len=l/DLEN;
if(l%DLEN)
len++;
index=;
for(i=l-;i>=;i-=DLEN)
{
t=;
k=i-DLEN+;
if(k<)
k=;
for(int j=k;j<=i;j++)
t=t*+s[j]-'';
a[index++]=t;
}
}
int BigNum::operator %(const int & b) const{ //大数对一个int类型的变量进行取模运算
int i,d=;
for (i = len-; i>=; i--)
{
d = ((d * (MAXN+))% b + a[i])% b;
}
return d;
}
int main(){
while(~scanf("%s %d",str,&modd)){
BigNum big(str);
cout << big % modd << endl;
}
return ;
}
【大数取模】HDOJ-1134、CODEUP-1086的更多相关文章
- hdu2302(枚举,大数取模)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2303 题意:给出两个数k, l(4<= k <= 1e100, 2<=l<=1 ...
- (POJ2635)The Embarrassed Cryptographer(大数取模)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13041 Accep ...
- HDU4704Sum 费马小定理+大数取模
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4704 题目大意: 看似复杂,其实就是求整数n的划分数,4=1+1+2和4=1+2+1是不同的.因而可 ...
- HDU--1212大数取模
大数取模问题.题目传送门:HDU1212 #include <iostream> using namespace std; char a[1010]; int main() { int b ...
- ACM-ICPC 2018 焦作赛区网络预赛G Give Candies(隔板定理 + 小费马定理 + 大数取模,组合数求和)题解
题意:给你n个东西,叫你把n分成任意段,这样的分法有几种(例如3:1 1 1,1 2,2 1,3 :所以3共有4种),n最多有1e5位,答案取模p = 1e9+7 思路:就是往n个东西中间插任意个板子 ...
- HPU 1471:又是斐波那契数列??(大数取模)
1471: 又是斐波那契数列?? 时间限制: 1 Sec 内存限制: 128 MB 提交: 278 解决: 27 统计 题目描述 大家都知道斐波那契数列吧?斐波那契数列的定义是这样的: f0 = 0; ...
- 题解报告:hdu 1212 Big Number(大数取模+同余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 Problem Description As we know, Big Number is al ...
- HDU-2303 The Embarrassed Cryptographer 高精度算法(大数取模)
题目链接:https://cn.vjudge.net/problem/HDU-2303 题意 给一个大数K,和一个整数L,其中K是两个素数的乘积 问K的是否存在小于L的素数因子 思路 枚举素数,大数取 ...
- HDU-1226-超级密码-队列+广搜+大数取模
Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进制的数,并且只能由给定的M个数字构成,同 ...
随机推荐
- 项目中libevent几个问题
几个问题: .libevent到底用的是select还是iocp,然后是如何突破64限制的 typedef struct fd_set { u_int fd_count; /* how many ar ...
- Eclipse插件开发
最近在做Eclipse的插件开发,目前是在Eclipse3.x环境上进行开发,之后迁移到Eclipse4.x环境.会贡献在插件开发过程中遇到的所有问题以及相关技巧,敬请期待. SWT开发 JFace开 ...
- Synchronized Methods
Synchronized Methods The Java programming language provides two basic synchronization idioms: synchr ...
- POJ2104 k-th number 划分树
又是不带修改的区间第k大,这次用的是一个不同的方法,划分树,划分树感觉上是模拟了快速排序的过程,依照pivot不断地往下划分,然后每一层多存一个toleft[i]数组,就可以知道在这一层里从0到i里有 ...
- 如何处理JSON中的特殊字符
JSON 是适用于 Ajax 应用程序的一种有效格式,原因是它使 JavaScript 对象和字符串值之间得以快速转换.由于 Ajax 应用程序非常适合将纯文本发送给服务器端程序并对应地接收纯文本,相 ...
- javascript 在一个函数参数中包含另一个函数的引用
javascript函数的参数包含另一个函数的情形: <script> //b函数的参数func为另一个函数 function b(a, func) { alert(a); //调用参数 ...
- U盘文件夹被病毒隐藏,且不能取消解决办法
在cmd下进入到U盘,运行attrib -r -a -s -h *.* /s /d
- JS代码片段:日期格式化
Date.prototype.format = function(format) { var date = { "M+": this.getMonth() + 1, "d ...
- UVa 11524 - InCircle
推公式 #include <cstdio> #include <cmath> double Cal( double a, double b, double c ) { retu ...
- vim常用命令 vim键盘布局
vim键盘布局,vim快捷键 vim常用命令: