uva 10692 Huge Mods 超大数取模
vjudge上题目链接:Huge Mods
附上截图:

题意不难理解,因为指数的范围太大,所以我就想是不是需要用求幂大法: AB % C = AB % phi(C) + phi(C) % C ( B > phi(C) ) 呢?后来发现确实需要用到,而且因为它有很多重指数,所以需要 dfs,深搜到最后一层后才返回,每次向上一层返回用求幂公式处理好的指数,然后本层用同样的原理去处理好当前层取模的值,并向上一层返回。欧拉函数预处理即可,这题的结束也有点卡人,我是用输入挂来处理的。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = ; int phi[M] = {,,};
inline void init(int n = M - ) {
for(int i = ; i <= n; ++i)
if(!phi[i])
for(int j = i; j <= n; j += i) {
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] / i *(i - );
}
} #include<cctype>
inline int read(int &x) {
x = ;
char ch = getchar();
while(!isdigit(ch) && ch != '#') ch = getchar();
if(ch == '#') return ;
while(isdigit(ch)) {
x = x * + (ch - '');
ch = getchar();
}
return ;
} int quick_mod(int a, int b, int m) {
int res = ;
while(b) {
if(b & ) res = res * a % m;
a = a * a % m;
b >>= ;
}
return res;
} int m,n,a[]; // id 为当前层的数组下标,mod 为当前层进行取模的模数,
// 由求幂公式可知 mod 每次向下一层传参时是传当前层的 mod 的欧拉函数,也就是 phi[mod] 的值
// dfs 向上一层返回用求幂公式处理好的指数,更多的参看代码了
int dfs(int id, int mod) {
if(id == n) {
if(a[id] > mod) return a[id] % mod + mod;
else return a[id];
}
int pow = dfs(id + , phi[mod]); int mul = , c = a[id], num = pow; // 因为忽略了 c <= mod 这个判断导致中间数据溢出,害我 TLE 了数次,T 得不明真相,
// 还在想是不是复杂度算错了,害得我一步步来痛苦地去调试 T.T
while(num && mul <= mod && c <= mod) {
if(num & ) mul *= c;
c *= c;
num >>= ;
}
if(num && (mul > mod || c > mod)) return quick_mod(a[id], pow, mod) + mod;
else return mul;
} int main() {
int Case = ;
init();
while(read(m)) {
read(n);
for(int i = ; i <= n; ++i)
read(a[i]);
printf("Case #%d: %d\n",++Case, dfs(,m) % m);
}
return ;
}
好久没做数论题了,果然很爽的感觉!虽然很难,虽然我还有 n 多的 XX 定理不会,不过我不会放弃这个如此吸引人的数学分支的,想当初搞 ACM 有很大原因也是因为她~
uva 10692 Huge Mods 超大数取模的更多相关文章
- uva 10692 - Huge Mods(数论)
题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值. 解题思路:依据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+ph ...
- UVA 10692 Huge Mods(指数循环节)
指数循环节,由于a ^x = a ^(x % m + phi(m)) (mod m)仅在x >= phi(m)时成立,故应注意要判断 //by:Gavin http://www.cnblogs. ...
- hdoj 4828 卡特兰数取模
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Sub ...
- Fibonacci数列对任何数取模都是一个周期数列
题目是要求出斐波那契数列n项对一个正整数取模,那么可以把斐波那契数列取模后得到的数列周期求出来. 比如下面一个题目:求出f[n]的后4位,先求出数列对10000取模的周期,然后再查找即可. #incl ...
- UVA 10692 Huge Mod
Problem X Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator f ...
- HPU 1471:又是斐波那契数列??(大数取模)
1471: 又是斐波那契数列?? 时间限制: 1 Sec 内存限制: 128 MB 提交: 278 解决: 27 统计 题目描述 大家都知道斐波那契数列吧?斐波那契数列的定义是这样的: f0 = 0; ...
- 【Gym 100947E】Qwerty78 Trip(组合数取模/费马小定理)
从(1,1)到(n,m),每次向右或向下走一步,,不能经过(x,y),求走的方案数取模.可以经过(x,y)则相当于m+n步里面选n步必须向下走,方案数为 C((m−1)+(n−1),n−1) 再考虑其 ...
- HDU 6211 卡常数取模 预处理 数论
求所有不超过1e9的 primitive Pythagorean triple中第2大的数取模$2^k$作为下标,对应a[i]数组的和. 先上WIKI:https://en.wikipedia.org ...
- UVa 11582 巨大的斐波那契数!(幂取模)
https://vjudge.net/problem/UVA-11582 题意: 输入两个非负整数a.b和正整数n,你的任务是计算f(a^b)除以n的余数.f[0]=0,f[1]=1,f[i+2]=f ...
随机推荐
- jdbc连接集合
JDBC里统一的使用方法: Class.for(jdbcDriverName); Connection conn=DriverManager.getConnection(url,u ...
- [团队项目]第二个冲刺 看板和燃尽图 Sprint2 6.8/6.9/6.10/6.11/6.12/6.13/6.14
1.开始一个新的冲刺: 起止:2016.6.1~2016.6.14 按照以下过程进行 ProductBacklog:继续向下细化 Sprint 计划会议:确定此次冲刺要完成的目标 Sprint Bac ...
- c#之双色球
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Unity脚本在层级面板中的执行顺序测试2
上一篇测试了生成顺序对执行顺序的影响,链接:LINK 执行顺序测试3: LINK 这篇主要测试一下Awake,OnEnable,Start三个常用消息的循环顺序 1.测试消息循环顺序 先上一个最简单的 ...
- #ifdef __cplusplus extern "C" { #endif
1.在好多程序中我们会遇到下面代码段 #ifdef __cplusplus extern "C" { #endif //c语法代码段 #ifdef __ ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B
Description You are given a table consisting of n rows and m columns. Numbers in each row form a per ...
- RS485模块(485与TTL信号的转换)
1 综述 MAX3483, MAX3485, MAX3486, MAX3488, MAX3490以及MAX3491是用于RS-485与RS-422通信的3.3V,低功耗收发器,每个器件中都具有一个驱动 ...
- Luence学习笔记
1.Luence的核心索引类 IndexWriter:建立索引的核心组件 Directory:代表一个lucene索引项的位置,是一个抽象类其子类有FSDirectory和RAMDirectory F ...
- PowerDesigner导出Excel
1.打开PowerDesigner,创建物理模型(Physical Data Model) 2.在PowerDesigner菜单栏中,依次点击“Tools ->Excute Commands-& ...
- null&this&super&向上转型
向上转型:父类声明子类实例化对象 例如A是父类 B是子类 1 : 实际上是父类的对象但将会丢失子类没有的父类方法 ,如果调用方法 将会调用子类重写的父类方法 2:上转型对象不能调用子类新增 ...