【大数取模】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个数字构成,同 ...
随机推荐
- 浅谈Feature Scaling
浅谈Feature Scaling 定义:Feature scaling is a method used to standardize the range of independent variab ...
- Unity3D开发之“获取IOS设备所在的国家代码"
原地址:http://dong2008hong.blog.163.com/blog/static/469688272014021025578/ 在前一段时间游戏开发中需要实现获取IOS设备所在的国家代 ...
- NGINX的奇淫技巧 —— 6. IF实现数学比较功能 (1)
NGINX的奇淫技巧 —— 6. IF实现数学比较功能 (1) ARGUS 1月13日 发布 推荐 0 推荐 收藏 3 收藏,839 浏览 nginx的if支持=.!= 逻辑比较, 但不支持if中 & ...
- C/C++框架和库
http://blog.csdn.net/xiaoxiaoyeyaya/article/details/42541419 值得学习的C语言开源项目 - 1. Webbench Webbench是一个在 ...
- SDUT2087离散事件模拟-银行管理
呃,这个题,我只想仰天长啸:无语死我了,还动用了繁和帅锅给我改,妹的,做题一定要仔细仔细再仔细啊,这种小错误都犯真是该打. 题目描述 现在银行已经很普遍,每个人总会去银行办理业务,一个好的银行是要考虑 ...
- DataRow.RowState 属性
RowState 的值取决于两个因素:已对该行执行的操作的类型,以及是否已对 DataRow 调用了 AcceptChanges. private void DemonstrateRowState() ...
- Android:查看应用创建的数据库
每个Android应用程序都可以使用SQLite数据库.它创建的位置在data/data/<项目文件夹>/databases/ 运行后打开,window->show view-> ...
- 转:在MyEclipse+Hibernate
源地址http://blog.renren.com/share/270733118/5528463779 1.实验环境准备 MyEclipse8.6 Tomcat6.0.20 MySQL5.1 ...
- 转TerreyLee AJAX入门系列2——ScriptManager的理解总结
ScriptManager的功能之一就是处理页面上局部更新,对于这点,我想大家都知道.但是他工作的原理到底是什么呢,这个暂且不从正面来回答. 我们这样想一下,目前能够真正实现局部刷新的就是js+xml ...
- Size Classes with Xcode 6
转载自 http://www.cocoachina.com/ios/20141015/9919.html 总结:通过在Size Classes不同的模式下设置,可以实现不同设备在横屏.竖屏下UIVie ...