【大数取模】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个数字构成,同 ...
随机推荐
- Introduction to Deep Neural Networks
Introduction to Deep Neural Networks Neural networks are a set of algorithms, modeled loosely after ...
- uva 11986
假设有n只老鼠 每只老鼠有两种状态 死或活 则n只老鼠有 2^n方种状态 所以n只老鼠可以确定2^n只瓶子 #include <cstdio> #include <cstdlib&g ...
- Memcache安全配置
Memcache安全配置 瞌睡龙 · 2014/01/20 17:59 0x00 Memcache简介 Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash ...
- linux源代码阅读笔记 八进制
c语言中,众所周知,以0x开头的数是16进制数.例如 0x8FFF 然而较少使用的是八进制数.它以0开头.例如 01234
- Python新式类和旧式类的区别
新式类是为了统一**而在2.2中开始引入的. 代码讲解 上面的例子比较明白的说明了问题. B是定义的新式类.那么输入b的时候,不论是type(b),还是b.__class__都是输出的<clas ...
- linux 命令小例
xargs示例: ls |xargs -i mv {} /opt find示例: find -mtime +n -name “*.avi” -type f -exec rm {} \; find - ...
- lintcode:三数之和
题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...
- Unity UGUI —— 无限循环List(转载)
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; ...
- HTML5入门2---js获取HTML元素的值
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- s.charAt()
public class ish{public static void main(String[]args){ String s="call me ishmae";System.o ...