HDU 4704 Sum 超大数幂取模
很容易得出答案就是2^(n-1)
但是N暴大,所以不可以直接用幂取模,因为除法操作至少O(len)了,总时间会达到O(len*log(N)) 显然爆的一塌糊涂
套用FZU1759的模板+顺手写一个大数-1
http://acm.fzu.edu.cn/problem.php?pid=1759
标程的做法是用费马小定理 , ap-1≡1(mod p)
那么2(1e9+6)%(1e9+7) = 1
很容易得出 2k%(10e+7) = 2k%(10e+6)%(10e+7)
然后就能用快速幂了 但FZU那题显然不是这么水的...我暂时也没看懂是怎么做的
现在看懂这个意思了,根据著名的欧拉公式:A^PHI(C)=1(MOD C) 条件:(A,C)=1
我们可以得出以下公式
那么这题就能做了..显然是以Phi(c)作为循环节 ... 但我还是没理解当(A,C)!=1时这个公式的成立性 ....prpr
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define LL long long
#define nnum 100005
#define nmax 131625
int flag[nmax], prime[nmax];
char ch[nnum];
int plen;
void mkprime() {
int i, j;
memset(flag, -, sizeof(flag));
for (i = , plen = ; i < nmax; i++) {
if (flag[i]) {
prime[plen++] = i;
}
for (j = ; (j < plen) && (i * prime[j] < nmax); j++) {
flag[i * prime[j]] = ;
if (i % prime[j] == ) {
break;
}
}
}
}
int getPhi(int n) {
int i, te, phi;
te = (int) sqrt(n * 1.0);
for (i = , phi = n; (i < plen) && (prime[i] <= te); i++) {
if (n % prime[i] == ) {
phi = phi / prime[i] * (prime[i] - );
while (n % prime[i] == ) {
n /= prime[i];
}
}
}
if (n > ) {
phi = phi / n * (n - );
}
return phi;
}
int cmpCphi(int p, char *ch) {
int i, len;
LL res;
len = strlen(ch);
for (i = , res = ; i < len; i++) {
res = (res * + (ch[i] - ''));
if (res > p) {
return ;
}
}
return ;
}
int getCP(int p, char *ch) {
int i, len;
LL res;
len = strlen(ch);
for (i = , res = ; i < len; i++) {
res = (res * + (ch[i] - '')) % p;
}
return (int) res;
}
int modular_exp(int a, int b, int c) {
LL res, temp;
res = % c, temp = a % c;
while (b) {
if (b & ) {
res = res * temp % c;
}
temp = temp * temp % c;
b >>= ;
}
return (int) res;
}
void solve(int a, int c, char *ch) {
int phi, res, b;
phi = getPhi(c);
if (cmpCphi(phi, ch)) {
b = getCP(phi, ch) + phi;
} else {
b = atoi(ch);
}
res = modular_exp(a, b, c);
printf("%d\n", res);
}
char* sub(char ch[]){
int len = strlen(ch);
int fl = ;
char x[nnum];
for(int i = len- ; i >= ; i--){
x[i] = ((ch[i] - fl - '') + ) % + '';
if((ch[i] - fl - '') < ) fl = ;
else fl = ;
}
x[len] = '\0';
if(x[] == '') return x+;
return x;
}
int main() {
int a = , c = ;
mkprime();
while (~scanf("%s",ch)) {
char x[nmax];
strcpy(x,sub(ch));
//puts(x);
solve(a % c, c, x);
}
return ;
}
HDU 4704 Sum 超大数幂取模的更多相关文章
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)
Sum Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...
- hdu 4506 小明系列故事——师兄帮帮忙【幂取模乱搞】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4506 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 1097 A hard puzzle 快速幂取模
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...
- 快速幂取模(当数很大时,相乘long long也会超出的解决办法)
当几个数连续乘最后取模时,可以将每个数字先取模,最后再取模,即%对于*具有结合律.但是如果当用来取模的数本身就很大,采取上述方法就不行了.这个时候可以借鉴快速幂取模的方法,来达到大数相乘取模的效果. ...
- 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 ...
- HDU 1061.Rightmost Digit-规律题 or 快速幂取模
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 题解报告:hdu 1061 Rightmost Digit(快速幂取模)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
随机推荐
- navigator.mediaDevices.getUserMedia
navigator.mediaDevices.getUserMedia: 作用:为用户提供直接连接摄像头.麦克风的硬件设备的接口 语法: navigator.mediaDevices.getUserM ...
- 洛谷P1280 && caioj 1085 动态规划入门(非常规DP9:尼克的任务)
这道题我一直按照往常的思路想 f[i]为前i个任务的最大空暇时间 然后想不出来怎么做-- 后来看了题解 发现这里设的状态是时间,不是任务 自己思维还是太局限了,题做得太少. 很多网上题解都反着做,那么 ...
- 今日SGU 5.29
sgu 299 题意:给你n个线段,然后问你能不能选出其中三个组成一个三角形,数字很大 收获:另一个大整数模板 那么考虑下为什么如果连续三个不可以的话,一定是不存在呢? 连续上个不合法的话,一定是 a ...
- [BJOI2018]求和(树链剖分)
题目描述 master 对树上的求和非常感兴趣.他生成了一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的 kkk 次方和,而且每次的 kkk 可能是不同的.此处节点深度的定义是这个节点到根 ...
- HDFS 断点续传,写文件功能
实际上这是个 HDFS 的工具类部分代码. 首先 public static Configuration configuration = null;public static FileSystem f ...
- MKVToolNix v8.2
32位版:http://pan.baidu.com/s/1i3s4gGd 64位版: http://pan.baidu.com/s/1gdvqbpp
- 【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】
[063-Unique Paths II(唯一路径问题II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Follow up for "Unique Pa ...
- Android程序猿自己动手制作.9.png图片
1:怎样制作9.png图片素材: 打开SDK工具文件夹下: draw9patch.zip 解压执行draw9patch.bat.有的直接搜索会有:draw9patch.bat. 双击执行后,例如以下 ...
- 99.重载[] * -> ->*
#include "mainwindow.h" #include <QApplication> #include <QPushButton>> //重 ...
- Gym - 100625F Count Ways 快速幂+容斥原理
题意:n*m的格子,中间有若干点不能走,问从左上角到右下角有多少种走法. 思路:CountWay(i,j) 表示从 i 点到 j 点的种数.然后用容斥原理加加减减解决 #pragma comment( ...