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 ...
随机推荐
- rem自适应布局-移动端自适应必备:flexible.js
http://caibaojian.com/flexible-js.html
- Futures and promises
In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing ...
- 函数式JS: 原来promise是这样的monad
转载请注明出处: http://hai.li/2017/03/27/prom... 背景 上篇文章 函数式JS: 一种continuation monad推导 得到了一个类似promise的链式调用, ...
- 因子问题 I - Ugly Numbers
题目: Ugly numbers are numbers whose only prime factors are 2, 3 or 5 . The sequence 1, 2, 3, 4, 5, 6, ...
- 图像处理是用的数据类型uint8,double
将原图像的灰度值转换成double的作用主要是考虑计算过程中的精度的问题,double 的数据是有小数点的,而uint8是0-255的整数,如果直接用uint8计算,会在计算过程中产生舍入误差,这种误 ...
- 紫书 例题 10-22 UVa 1640(数位统计)
这道题的题解有几个亮点 一个是每次只统计一个数字来简化思维 一个是统计当前位数的时候分三个部分来更新答案 具体看代码,有注释 #include<cstdio> #include<cs ...
- 彻底解决lazarus安装组件后烦人的编译时单元找不到的问题!
以安装indy为例 1/下载组件包, http://www.indyproject.org/Sockets/fpc/indy-10.2.0.3.zip 2/爆开放于C:\lazarus\compone ...
- vue2.0-elementUI
main.js import Vue from 'vue' import App from './App.vue' import ElementUI from 'element-ui' import ...
- lightoj--1043-- Triangle Partitioning (水题)
Triangle Partitioning Time Limit: 500MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...
- 流量数据iftop命令
yum install flex byacc libpcap ncurses ncurses-devel libpcap-devel tar zxvf iftop-0.17.tar.gz cd ift ...