hdu_2837_Calculation(欧拉函数,快速幂求指数循环节)
InputThe first line contains a single positive integer T. which is the number of test cases. T lines follows.Each case consists of one line containing two positive integers n and m.OutputOne integer indicating the value of f(n)%m.Sample Input
2
24 20
25 20
Sample Output
16
5 指数循环节题目就是欧拉函数加快速幂。
根据欧拉定理
A^x mod C =A^(x mod phi(C)+ phi(C)) (x>=C)
#include <iostream>
#include<cstdio>
using namespace std;
#define ll long long
ll phi(ll c)
{
ll ans = c;
for(int i = 2; i*i <= c; i++) {
if(c%i == 0){
ans -= ans/i;
while(c%i == 0) c /= i;
}
}
if(c > 1) ans -= ans/c;
return ans;
}
ll quick_mod(ll a, ll b, ll mod)
{
if(a >= mod) a = a%mod + mod; // 并不是直接%mod
ll ans = 1;
while(b){
if(b&1){
ans = ans*a;
if(ans >= mod) ans = ans%mod + mod;//**
}
a *= a;
if(a >= mod) a = a%mod + mod;//**
b >>= 1;
}
return ans;
}
ll solve(ll n, ll m)
{
ll p = phi(m);
if(n == 0) return 1;
ll index = solve(n/10, p); return quick_mod(n%10, index, m);
}
int main()
{
ll n, m, T;
cin >> T;
while(T--){
scanf("%I64d%I64d", &n, &m);
printf("%I64d\n", solve(n,m)%m);
}
return 0;
}
hdu_2837_Calculation(欧拉函数,快速幂求指数循环节)的更多相关文章
- XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】
1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 45 Solved: 8[Submit][Status][W ...
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 数学知识-欧拉函数&快速幂
欧拉函数 定义 对于正整数n,欧拉函数是小于或等于n的正整数中与n互质的数的数目,记作φ(n). 算法思路 既然求解每个数的欧拉函数,都需要知道他的质因子,而不需要个数 因此,我们只需求出他的质因子, ...
- 牛客训练:小a与黄金街道(欧拉函数+快速幂)
题目链接:传送门 思路:欧拉函数的性质:前n个数的欧拉函数之和为φ(n)*n/2,由此求出结果. 参考文章:传送门 #include<iostream> #include<cmath ...
- Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...
- 小a与黄金街道(欧拉函数+快速幂)
链接:https://ac.nowcoder.com/acm/contest/317/D 来源:牛客网 题目描述 小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们 ...
- 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...
- 【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)
读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用 ...
- (hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)
题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
随机推荐
- (转)python 集合,列表,元组,字符串,文件等操作总结
原文:http://www.cnblogs.com/songqingbo/tag/python%E5%87%BD%E6%95%B0/
- BoostrapTable-本地模式(一次性加在所有数据)
直接上代码 数据: [ { "id": "1001", "name": "yyq", "isAdmin&quo ...
- php中配置伪静态
mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面.下面我详细说说它的使用方法 1.检测Apache是否支持mod_rewrite 通过php提供的phpinfo()函数查 ...
- psd图片不能在网页上显示
原因:web上不支持psd图片,web支持JPG,PNG等. 解决:打开ps点击文件--储存为web所用格式(选择转换成哪种格式).
- Swift中as as! as?的区别
as :类型一致或者子类 仅当一个值的类型在运行时(runtime)和as模式右边的指定类型一致 - 或者是该类型的子类 - 的情况下,才会匹配这个值.如果匹配成功,被匹配的值的类型被转换成as模 ...
- <Android 基础(十二)> TextInputLayout,让输入框更有灵性
介绍 Layout which wraps an {@link android.widget.EditText} (or descendant) to show a floating label wh ...
- spring-cloud构架微服务(1)-全局配置
使用spring-cloud是基于熟悉springboot基础上进行的.本篇介绍全局配置,spring-boot版本就以1.4.0来做吧.项目地址: https://git.oschina.net/b ...
- 【Android学习入门】Android studio基本设置
1.背景设置 依次选择File->Settings-->Appearance & Behaviour->Apprearance,然后勾选 show line number. ...
- Struts2_用Action的属性接收参数
先在 Action 中定义要接收的属性,需要编写属性的getter 和 setter 方法 struts2 会自动帮我们把 String 类型的参数转为 Action 中相对应的数据类型. priva ...
- Sigrity PowerDC是如何计算IR Drop Margin?
IR Drop仿真是一个系统层面的问题,需要考虑完整的Power Distribution System(PDS)链路上所有压降,并以此来优化每颗器件所接收到的供电电压. 在设计设计中所有的电源供电芯 ...