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 超大数取模的更多相关文章

  1. uva 10692 - Huge Mods(数论)

    题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值. 解题思路:依据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+ph ...

  2. UVA 10692 Huge Mods(指数循环节)

    指数循环节,由于a ^x = a ^(x % m + phi(m)) (mod m)仅在x >= phi(m)时成立,故应注意要判断 //by:Gavin http://www.cnblogs. ...

  3. hdoj 4828 卡特兰数取模

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Sub ...

  4. Fibonacci数列对任何数取模都是一个周期数列

    题目是要求出斐波那契数列n项对一个正整数取模,那么可以把斐波那契数列取模后得到的数列周期求出来. 比如下面一个题目:求出f[n]的后4位,先求出数列对10000取模的周期,然后再查找即可. #incl ...

  5. UVA 10692 Huge Mod

    Problem X Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator f ...

  6. HPU 1471:又是斐波那契数列??(大数取模)

    1471: 又是斐波那契数列?? 时间限制: 1 Sec 内存限制: 128 MB 提交: 278 解决: 27 统计 题目描述 大家都知道斐波那契数列吧?斐波那契数列的定义是这样的: f0 = 0; ...

  7. 【Gym 100947E】Qwerty78 Trip(组合数取模/费马小定理)

    从(1,1)到(n,m),每次向右或向下走一步,,不能经过(x,y),求走的方案数取模.可以经过(x,y)则相当于m+n步里面选n步必须向下走,方案数为 C((m−1)+(n−1),n−1) 再考虑其 ...

  8. HDU 6211 卡常数取模 预处理 数论

    求所有不超过1e9的 primitive Pythagorean triple中第2大的数取模$2^k$作为下标,对应a[i]数组的和. 先上WIKI:https://en.wikipedia.org ...

  9. 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 ...

随机推荐

  1. js 表单操作

    order.aspx 订单页-  order-detail.aspx订单确认页-  操作:order.aspx提交订单@1,跳转到order-detail.aspx页面,确认页面操作:返回上一步@2- ...

  2. Cookie机制(会话cookie和持久化cookie)在客户端保持HTTP状态信息的方案

    1. Cookie只有一个name和一个value,不同于map;购物车设计的时候需要cookie,获取购物车的cookie id,以便于将物品多次放入购物车: 2.cookie获取了其地址,并且可以 ...

  3. 0. WP8.1学习笔记

    应用程序生命周期: 运行: 在程序NotRunning状态下点击图标,应用将处于Running状态,这会触发一个Actived事件 挂起: 在程序Running状态下, 点击返回键或win键会触发一个 ...

  4. userdebug版本开机串口log打开

    在/bootable/bootloader/lk/app/mt_boot/mt_boot.c里修改: if (!has_set_p2u) { #ifdef USER_BUILD sprintf(cmd ...

  5. easyui datagrid使用参考

    数据表格 继承$.fn.panel.defaults,使用$.fn.datagrid.defaults重载默认值. 依赖关系 控制面板 缩放 链接按钮 分页 使用方法 <table id=&qu ...

  6. Easyui主要组件用法

    Easyui主要组件用法说明: 1.  combogrid用法 说明:combogrid可提供翻页列表的数据选择并可进行数据的过滤查询(查询的传人参数为q,可在控制器中获取这个参数传过来的值,下面的示 ...

  7. 2016年11月11日 星期五 --出埃及记 Exodus 20:2

    2016年11月11日 星期五 --出埃及记 Exodus 20:2 "I am the LORD your God, who brought you out of Egypt, out o ...

  8. 编译android源码官方教程(3)下载代码

    https://source.android.com/source/downloading.html Downloading the Source IN THIS DOCUMENT Installin ...

  9. c#调用cmd的ping命令

    private static string CmdPing(string strIp) { Process p = new Process(); p.StartInfo.FileName = &quo ...

  10. SUSE Linux Enterprise Server 设置IP地址、网关、DNS

    说明: ip:192.168.21.172 子网掩码:255.255.255.0 网关:192.168.21.2 dns:8.8.8.8 8.8.4.4 1.设置ip地址 vi /etc/syscon ...