栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python | 秦九昭算法详细介绍

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python | 秦九昭算法详细介绍

help(reduce)结果是

Help on built-in function reduce in module _functools:
reduce(...)
 reduce(function, sequence[, initial]) - value
 Apply a function of two arguments cumulatively to the items of a sequence,
 from left to right, so as to reduce the sequence to a single value.
 For example, reduce(lambda x, y: x y, [1, 2, 3, 4, 5]) calculates
 ((((1 2) 3) 4) 5). If initial is present, it is placed before the items
 of the sequence in the calculation, and serves as a default when the
 sequence is empty.

中文解释下 就是操作一个集合 把一系列数迭代操作成一个数。

不理解就看蓝色字的例子

2.大整数取模 hdu 1212 Big Number

1 题意

给你一个长度不超过1000的大数A 还有一个数值不超过100000的B 快速求A % B。

2 分析

由秦九昭算法可知 任意一个整数n akak-1ak-2.......a2a1a0可以拆分为

n (((((ak)*10 ak-1)*10 ak-2)*10 .......)*10 a1)*10 a0

例如 1234 ((1*10 2)*10 3)*10 4

 则大整数取模就可以转化为n个多项式每步取模。

3 贴份代码

#include iostream 
#include bits/stdc .h 
using namespace std;
const int maxn 10000 
char str[maxn];
int Horner(int mod){//秦九昭算法
 int len strlen(str);
 int ans 
 for(int i i i ){
 ans (ans*10 str[i] - 0 )%mod;
 return ans;
int main()
 int mod;
 while(scanf( %s%d ,str, mod)! EOF){
 int num Horner(mod);
 printf( %dn ,num);
 return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268034.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号