uva10692 - Huge Mods poj2164: Remainder Calculator 指数循环节

Codeforces Round #640 (Div. 4)题解 Codeforces Round #640 A. Sum of Round Numbers B. Same Parity Summands C. K-th Not Divisible by n D. Alice, Bob and Candies E. Special Elements F. Binary String Reconstruction G. Special Permutation DIV.4 #640Codeforces Round #640A. Sum of Round NumbersB. S 阅读详情

Problem X

Huge Mod

Input: standard input
Output: standard output
Time Limit: 1 second

2^3^4^5 mod 10 = ?The operator for exponentiation is different from the addition, subtraction, multiplication or division operators in the sense that the default associativity for exponentiation goes right to left instead of left to right. So unless we mess it up by placing parenthesis,2^3^2 should mean2^(3^2)=2^9=512 not(2^3)^2=8^2=64. This leads to the obvious fact that if we take the levels of exponents higher (i.e., 2^3^4^5^3), the numbers can become quite big. But let's not make life miserable. We being the good guys would force the ultimate value to be no more than 10000.

Given a 1, a 2, a 3, ... , a N and m(=10000)
you only need to compute a 1^a 2^a 3^...^a N mod m.

Input

There can be multiple (not more than 100) test cases. Each test case will be presented in a single line. The first line of each test case would contain the value for M(2<=M<=10000). The next number of that line would be N(1<=N<=10). Then N numbers - the values for a 1, a 2, a 3, ... , a N would follow. You can safely assume that 1<=a i<=1000. The end of input is marked by a line containing a single hash ('#') mark.

Output

For each of the test cases, print the test case number followed by the value of a 1^a 2^a 3^...^a N mod m on one line. The sample output shows the exact format for printing the test case number.

Sample Input

Sample Output

10 4 2 3 4 5
100 2 5 2
53 3 2 3 2
#
Case #1: 2
Case #2: 25
Case #3: 35

  首先要知道一个公式

 

A^x % m = A^(x%phi(m)+phi(m)) % m (x >= phi(m))
  这个证明没怎么看懂   点击打开链接  ,但是我感觉应该是这样的:如果A和m不互质的话,最小循环节可能小于phi[m],并且phi[m]是这个最小循环节的倍数,也就是phi[m]也是它的一个循环节,只是不是最小的。A和m互质的话,根据欧拉定理,最小循环节就是phi[m]。所以不管怎样,phi[m]都是循环节。那为什么要加上phi[m]呢?因为不一定从一开始就构成循环节,应该从phi[m]开始构成循环节,比如A=2,m=4,那么序列式2,0,0,0...不知道除了M是A的幂的情况还有没有别的情况也不符合,感觉应该是没有了。

  用这个公式递归计算,递归函数的含义是算当前底数的上面那一堆数的次方的部分,如果大于等于M,就对M取余再加上M。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define MAXN 10010
#define MAXM 1300
#define MOD 1000000007
typedef long long LL;
using namespace std;
char str[MAXN<<2];
int M,N,K,prime[MAXM],vis[MAXN],a[MAXN];
void prime_table(){
    memset(vis,0,sizeof(vis));
    for(int i=2;i<MAXN;i++){
        if(!vis[i]) prime[K++]=i;
        for(int j=0;j<K&&i*prime[j]<MAXN;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int euler(int n){
    int ret=n;
    for(int i=0;i<K&&prime[i]*prime[i]<=n;i++) if(n%prime[i]==0){
        ret=ret/prime[i]*(prime[i]-1);
        while(n%prime[i]==0) n/=prime[i];
    }
    if(n>1) ret=ret/n*(n-1);
    return ret;
}
//如果x^n<M 返回x^n,否则返回x^n%M+M
LL bigpow(int x,int n,int M){
    LL m=1;
    int flag=1;
    for(int i=0;i<n;i++){
        m*=x;
        if(m>=M){
            flag=0;
            break;
        }
    }
    if(flag) return m;
    LL ret=1,t=x%M;
    while(n){
        if(n&1) ret=ret*t%M;
        t=t*t%M;
        n>>=1;
    }
    return ret+M;
}
int solve(int cur,int M){
    if(cur==N-1){
        if(a[cur]>M) return a[cur]%M+M;
        return a[cur];
    }
    int p=solve(cur+1,euler(M));
    return bigpow(a[cur],p,M);
}
int main(){
    freopen("in.txt","r",stdin);
    prime_table();
    int cas=0;
    while(scanf("%s",str)!=EOF&&str[0]!='#'){
        sscanf(str,"%d",&M);
        scanf("%d",&N);
        for(int i=0;i<N;i++) scanf("%d",&a[i]);
        printf("Case #%d: %d\n",++cas,solve(0,M)%M);
    }
    return 0;
}

2164: Remainder Calculator

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 106  Solved: 23
[ Submit][ Status][ Web Board]

Description

In this problem, you are given two positive integers n and m  and then n positive integer a1,a2,a3...an . 

Suppose that for each 1<=i<=n ,bi=(ai)!  . You should find  mod m

Input

The number of test cases comes in the first line. 

For each test case, first there are two integers  n and m such that 1<=n<=100 . Then you are given  a1,a2,a3...an.

Output

For each test case, print  mod m

Sample Input

12 1003 8

Sample Output

76

  这个麻烦一点,要注意的是如果a[i]>=M,那么肯定对M取余是0,直接返回M。否则算阶乘的话会超时。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define MAXN 110
#define MAXM 1300
#define MOD 1000000007
typedef long long LL;
using namespace std;
LL T,N,M,a[MAXN];
LL euler(LL n){
    LL ret=n;
    for(LL i=2;i*i<=n;i++) if(n%i==0){
        ret=ret/i*(i-1);
        while(n%i==0) n/=i;
    }
    if(n>1) ret=ret/n*(n-1);
    return ret;
}
//如果x^n<M 返回x^n,否则返回x ^n%M+M
LL bigpow(LL f,LL n,LL M){
    if(M==1||f>=M) return M;
    LL flag=1,x=1;
    for(LL i=2;i<=f;i++){
        x*=i;
        if(x>=M){
            flag=0;
            x%=M;
        }
    }
    if(flag){
        LL m=1;
        flag=0;
        for(LL i=0;i<n;i++){
            m*=x;
            if(m>=M){
                flag=0;
                break;
            }
        }
        if(flag) return m;
    }
    LL ret=1,t=x%M;
    while(n){
        if(n&1) ret=ret*t%M;
        t=t*t%M;
        n>>=1;
    }
    return ret+M;
}
LL solve(LL cur,LL M){
    if(a[cur]>=M) return M;
    if(cur==N-1){
        LL t=1,flag=1;
        for(LL i=2;i<=a[cur];i++){
            t*=i;
            if(t>=M){
                flag=0;
                t%=M;
            }
        }
        if(flag) return t;
        return t%M+M;
    }
    LL p=solve(cur+1,euler(M));
    return bigpow(a[cur],p,M);
}
int main(){
    freopen("in.txt","r",stdin);
    scanf("%lld",&T);
    while(T--){
        scanf("%lld%lld",&N,&M);
        for(LL i=0;i<N;i++) scanf("%d",&a[i]);
        printf("%lld\n",solve(0,M)%M);
    }
    return 0;
}



【OpenCV】复杂场景下的高精度圆检测实战(附完整代码) 本文针对OpenCV霍夫圆检测在复杂场景下易失效的问题,提出了一套基于轮廓分析的高精度圆检测实战方案。通过图像预处理、轮廓发现及多轮几何筛选(面积、周长、椭圆拟合、多边形逼近),有效排除噪声和类圆干扰物,显著提升复杂场景下的检测鲁棒性,并附完整可调参的Python代码实现。 阅读详情

相关推荐

华为防火墙USG6600全流量日志配置指南:从流量监控到NAT日志的完整方案

本文详细介绍了华为USG6600防火墙全流量日志配置方案,涵盖流量监控、NAT日志及安全事件溯源等核心功能。通过精细化配置和性能优化技巧,帮助企业构建高效、合规的日志体系,提升网络安全运维效率。文章特别强调了日志采样与字段过滤的实用方法,适用于高流量环境下的资源优化。

gin88的博客 360

POJ 2164 && LA 3218 Find the Border (Geometry, PSLG 平面直线图)

2164 -- Find the Border   卡了一个月啊,都快想放弃了。今天看了一下LRJ的PSLG版的代码,才想起来我的模拟捆包裹法存在的漏洞。那就是对点的坐标进行排序的时候,应该用的sgn()函数来进行比较,而不是我一开始的直接对浮点数进行比较。如果直接对浮点数进行比较,本来距离差不多的点就会被拉伸开来,导致应该是相邻点的点被分隔了。   做法比较简单,我的捆包裹法是先将所有的交点...

weixin_34203426的博客 169

JMeter压测Dubbo接口的完整落地方案(适配3.x)

Dubbo作为主流Java微服务RPC框架,其接口压测长期面临协议私有、注册发现复杂、序列化耦合度高等技术门槛。理解Dubbo基于TCP长连接、泛化调用与SPI扩展机制的核心原理,是实现稳定压测的前提;技术价值在于绕过HTTP网关直击服务核心链路,保障大促等关键场景的性能可信度;典型应用场景包括电商订单链路压测、风控/库存等内部Dubbo服务SLA验证,以及CI/CD中性能左移实践。本文聚焦Dubbo 3.x版本,提供零第三方插件、可复用、避坑的JMeter Java Sampler落地路径。

weixin_33700809的博客 559

poj 2164 (卷包裹算法)

取了个数组名_end[]  ,Re了一天, 都要哭了。。。。。_end[]  不能用。    view cod#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const

非确定有限状态自动机 728

雷达信号处理——恒虚警检测(CFAR)

上图展示的是同时在距离维和多普勒维进行恒虚警检测的原理,如果只是在距离维或多普勒维做CFAR,检测单元的左右两侧均有设定好数量的保护单元和参考单元。不同的CFAR检测器适用于不同的场景,CA-CFAR算法适用于均匀环境中的单目标或彼此相距较远的多目标环境,在邻近多目标环境会发生目标的遮蔽效应,在杂波边缘会发生杂波边缘效应。从图中也可以看到,根据阈值的选择不同,有不同的检测器,其中CA-CFAR是取的参考单元的均值,SO-CFAR取的是参考单元的最小值,而GO-CFAR是取的参考单元的最大值。

诚朴勇毅 3万+

OpenClaw 最强技能 self-improving-agent 详解:让 AI 从错误中自主学习

self-improving-agent 是 OpenClaw 生态中最受欢迎的技能插件,下载量突破 268k。它能让 AI 记住犯过的错误和解决方案,实现持续自我改进。本文深入讲解其工作原理、安装配置和实战案例。测试数据显示:部署流程时间从 15 分钟降到 2 分钟(87%↓),API 调试时间从 2 小时降到 20 分钟,数据库查询编写时间减少 90%。文章包含完整架构图、配置文件示例、3 个详细实战案例和性能测试数据,适合开发者、运维和数据分析师使用。

qq_38610923的博客 3651

UVA - 10692 Huge Mods (欧拉函数)

Problem X Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator for exponentiation is different from the addition, subtraction, multiplication or division opera

那些年 1347

基于LLaMA-action模型的文档摘要任务微调

文档摘要是指从长文档中提取关键信息,生成简短而准确的总结。抽取式摘要:从原文中直接抽取关键句子或短语组成摘要。生成式摘要:通过生成新的句子来表达原文的核心内容。混合式摘要:结合抽取式和生成式方法,既保留原文的关键句子,又生成新的内容。新闻网站:自动生成新闻摘要,帮助用户快速了解新闻要点。社交媒体:为用户提供新闻摘要,便于分享和传播。学术搜索引擎:为用户提供学术文献的摘要,帮助用户快速筛选相关文献。学术写作辅助:帮助研究人员快速生成文献综述部分。企业内部文档管理:自动生成文档摘要,便于员工快速了解文档内容。

加入“Super Entity”,与全能开发团队共探AI智能体与数字人项目,开启前沿技术之旅。 1436

uva10692-指数循环节

题目链接:https://vjudge.net/problem/UVA-10692 求 a1 ^ (a2 ^ (a3 ^ (... ^ an) )) % m 的值 利用指数循环节的降幂公式 a ^ b % mod = a ^ (b ^ phi(mod) + phi(mod)) % mod 递归的进行计算即可,phi()为欧拉函数,可以预处理出来 代码: #

Coder__Yang的博客 455

【Agent从入门到实践】40 容错优化:异常处理、重试机制、降级策略

各位小伙伴,咱们的多Agent框架一路优化到现在,决策快、执行效率高、资源用得省,看似啥都行,但只要一遇到网络波动、工具故障、数据异常调用web_search工具时网络超时,整个检索任务直接失败,写作Agent没数据可用向量库连接异常,向量检索直接崩掉,依赖检索结果的Agent全趴窝单个Agent执行出错,整个并行任务流程直接中断,前功尽弃高并发下部分工具调用失败,框架没有备用方案,只能返回任务失败说白了,咱们之前的框架只有正常执行逻辑,没有异常兜底逻辑。

人工智能AI技术 919

基于强化学习的多无人机路径规划

我们使用了Dueling Double Deep Q-Network(DDDQN)算法来训练智能体,这个算法在深度Q学习算法的基础上进行了升级,具有更快的收敛速度和更好的性能。本文提出了一种基于强化学习的算法,实现了多无人机路径规划,并提供相应的MATLAB代码。通过基于强化学习的多无人机路径规划算法,我们能够高效地解决复杂环境下的任务需求,并在实际应用中取得了良好的效果。首先,我们定义了状态空间和动作空间。实验结果表明,我们的方法能够有效地解决多无人机路径规划问题,提高了任务完成的效率和准确率。

DjjPython的博客 828

uva 10692 - Huge Mods(数论)

题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值。 解题思路:根据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+phi[M](phi[M]为M的欧拉函数),这样就可以根据递归去求解。 #include #include #include const int maxn = 15; int A[

不慌不忙、不急不躁 1833

目前最强的价格行为技术分析AI!大A也能用!还免费!

有球友推荐了一个历史最强的价格行为技术分析AI工具,使用下来确实很惊艳,解决了 很多AI工具黑盒过程,难以回溯的问题,他的决策过程可解释,提供决策树、诊断结果、原始响应、调试信息和未来走势预期。用户能够看到模型为什么选择“做多、做空或不下单”。会检查置信度、入场条件、盈亏比、结构失效和冷却期。最大的优点不是“预测一定准确”,而是把主观的价格行为分析整理成了可重复、可解释、可校验、可持续跟踪的工程化流程。本文记录本项目在 Windows 上的实际部署与启动方法。

木头大左的博客 95

UVA10692 Huge Mods(指数循环节)

题意:给定你一个MOD,然后再给你一个nnn,随之nnn个数。 让你计算a1a2a3....mod  MODa_1^{a_2^{a_3^....}}\mod MODa1a2a3.​...​​modMOD 的值。 思路: 指数循环节。根据欧拉降幂公式 可以递归计算。网上一部分代码是错误的。因为要注意后面的条件。只有大于等于的时候才可以套用公式,否则,直接快速幂返回即可。 const ll N = ...

一身清贫怎敢入繁华 321

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

UVA 10692 - Huge Mods(数论)

UVA 10692 - Huge Mods 题目链接 题意:求a0a1a2...mod m 思路:直接算肯定不行,利用欧拉定理ab=a(b mod phi(m) + phi(m))(b&gt;=phi(m)),对指数进行降值处理。然后就能够利用高速幂去计算了,计算过程利用递归求解。 代码: #include &lt;stdio.h&gt; #inc...

weixin_33912453的博客 176

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Codeforces Contest 1070 problem A Find a Number —— bfs求a的最小倍数且所有位的和是b

You are given two positive integers d and s. Find minimal positive integer n which is divisible by d and has sum of digits equal to s. Input The first line contains two positive integers d and s (1≤d≤...

天翼之城的博客 269

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

You are given two positive integers a and b

You are given two positive integers a and b. In one move, you can change a in the following way: Choose any positive odd integer x (x>0) and replace a with a+x; choose any positive even integer y ...

lzx's blog 3050

You are given two integers n and k.

E - 1000 You are given two integers n and k. You should create an array of n positive integers a1,a2,…,an such that the sum (a1+a2+⋯+an) is divisible by k and maximum element in a is minimum possible. What is the minimum possible maximum element in a? Inpu

weixin_46358119的博客 1348

UVA10692+636

The operator for exponentiation is differentfrom the addition, subtraction, multiplicationor division operators in the sensethat the default associativity for exponentiationgoes right to left instea

raptor 2100

UVa10692,Huge Mod,数论,欧拉定理,指数循环节

UVa10692,Huge Mod,数论,欧拉定理,指数循环节// author:latstars // time:2016-09-02 19:08:11 // 题目:UVa10692,Huge Mod // 题意:给定一个数组,指定指数运算a[1]^a[2]^a[3]...^a[n] // 指数运算的计算顺序是从上到下的也就是先计算p=a[2]^a[3]...^a[n] // 在计算a[1]^p

latstars的博客 568

uva 10692——Huge Mods

题目大意:给定第一个数M,后面有n的数,求解a[1]^a[2]^a[3]^…..%m的解   思路:开始的时候并不知道从哪里下手,一开始收到前面某题除4的印象,然后一直对4取余,知道a[1],计算后发现那一套只适用于求解最后一位的情况,苦思敏想不得其解,最后不得不去找答案,原来涉及到剩余系定理,即a^b=a^(b%phi[M])+phi[M])(phi[M]为M的欧拉函数) 如此一来,只是不断

坚持不懈 809

uva 10692 高次幂取模

Huge Mod Input:standard input Output:standard output Time Limit:1 second The operator for exponentiation is different from the addition, subtraction, multiplication or division operators in the...

weixin_30919919的博客 136

uva10951(多项式gcd)

类比整数辗转相除法,此题为多项式辗转相除法求最大公因多项式 #include&lt;bits/stdc++.h&gt; using namespace std; const int maxn = 1e5; typedef vector&lt;int&gt; vint; int n, x, y, t, kase, a[105]; vint f, g, ans; void read(int &amp...

UESTC_Charlie的博客 1076

Uva--10692--Huge Mods【数论】

这个东西叫指数循环节,有个公式: A^x % m = A^(x%phi(m)+phi(m)) % m (x >= phi(m)) 其中phi(m)是m的欧拉函数值,然后用递归层层推下来 #include #include #include #include #include #include #include #include #include #include #include

阳光照耀圣西罗 1100

UVa 10692 Huge Mods(快速幂)(欧拉定理)

题目链接: https://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&page=show_problem&problem=1633

ACM签到中... 653

UVa10692 Huge Mods

1.题目描述:点击打开链接 2.解题思路:本题利用欧拉定理解决。根据题意,我们需要递归地计算每一个取模后的指数,然后再进行快速幂得到最终的答案。由欧拉定理知,我们只需要计算指数模phi(MOD)的结果即可,但是有一个细节需要注意,如果gcd(a,MOD)>1,那么需要先计算出a^phi(MOD)%MOD的结果r,那么可以知道,r^k%MOD恒等于r,这样,再把结果乘上r,就是最终的答案。本题即可

Skyline 811
上一篇: uva1069 - Always an integer
下一篇: hdu3466 Proud Merchants 0-1背包变形
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址