HDU4704 費馬小定理+快速冪
容易看出來每次輸出的結果為,2的n-1次方對10e9+7取模,但是n太大了,這裡利用到了費馬小定理:a是整數,p是素數,且gcd(a,p) = 1,則有 a^(p-1) % p = 1 % p = 1.顯然這裡mod是素數並且與2互質,所以有2^(mod - 1) % mod = 1 % mod = 1,而我們要求的是2^(n-1)%mod,因為2^(mod - 1) % mod = 1,可以得出2^(n-1)%mod = 2^((n-1)%(mod-1))%mod,所以我們只需要求(n-1)%(mod-1),採用大數取模,然後進行快速冪
#include <stdio.h> #include <string.h> typedef long long ll; const ll mod = 1000000000 + 7; char str[100005]; using namespace std; ll q_pow(ll a,ll b){ ll r = 1,base = a; while(b){ if(b & 1){ r = (r * base) % mod; } base = (base * base) % mod; b >>= 1; } return r; } int main(void){ while(scanf("%s",str) != EOF){ ll num = 0; int len = strlen(str); for(int i = 0; i < len; i++){ num = (num * 10 + str[i] - '0') % (mod - 1); num %= (mod - 1); } //應該求的是(n-1)對(mod-1)取模,注意這裡要處理一下 if(num == 0){ printf("%lld\n",q_pow(2,mod - 2)); } else{ num--; printf("%lld\n",q_pow(2,num)); } } return 0; }
相關推薦
HDU4704 費馬小定理+快速冪
容易看出來每次輸出的結果為,2的n-1次方對10e9+7取模,但是n太大了,這裡利用到了費馬小定理:a是整數,p是素數,且gcd(a,p) = 1,則有 a^(p-1) % p = 1 % p = 1.顯然這裡mod是素數並且與2互質,所以有2^(mod - 1) % mod
HDU 4704 Sum(隔板原理+組合數求和公式+費馬小定理+快速冪)
ace php 模板 erl char printf 證明 style ron 題目傳送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sam
【費馬小定理+快速冪取模】ACM-ICPC 2018 焦作賽區網絡預賽 G. Give Candies
print using pri long long ger ssi bit one ive G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To mak
【費馬小定理+快速冪取模】ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies
G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes
費馬小定理+快速冪:ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies
限制時間是1S,試了試Java的大數運算,超時了。發現雖然 N 的值大的可怕但結果是取餘後的,可以通過費馬小定理減小指數大小後快速冪得到結果。 快速冪運算時必須加上取餘, (a * b) %
焦作2018網路賽_Give Candies(費馬小定理+快速冪)
There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more interesting, Miss Li comes up with the rul
[HDU 4704] Sum · 費馬小定理 & 快速冪
題意:給定n,設是將n分成k個數之和的方案數,求 隔板原理:將n個物品分成k組,相當於在n-1個間隔中插入k-1個隔板,方案數為,所以等於,貌似是叫二項式定理來著?反正這個式子的值等於,所以就是要求的
HDU 4704 Sum 【隔板原理+費馬小定理+快速冪】
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2745 Accepted Submissi
HDU 4704 [費馬小定理+快速冪] ---狗眼不識多校
Description Input 2 Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file c
hdu 4704 Sum (費馬小定理+快速冪)
//(2^n-1)%mod //費馬小定理:a^n ≡ a^(n%(m-1)) * a^(m-1)≡ a^(n%(m-1)) (mod m) # include <stdio.h> # include <algorithm> # include &l
HDU4869:Turn the pokers(費馬小定理+快速冪)
Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. She went out and bought m pokers, te
HDOJ 4704 Sum(費馬小定理+快速冪)
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm
HDU 4704 Sum 費馬小定理+快速冪
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 18 Accepted Submission
hdu 4704 sum(費馬小定理+快速冪)
題意: 這題意看了很久。。 s(k)表示的是把n分成k個正整數的和,有多少種分法。 例如: n=4時, s(1)=1 4 s(2)=3 1,3 3,1 2,2 s(3)=3 1,1,2
費馬小定理 + 快速冪 Give Candies
There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more interesting, Miss Li comes up with the rul
hdu 5667 Sequence(矩陣快速冪+費馬小定理+快速冪)
#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <cstdlib> #include <algorithm> #inc
4704 Sum (費馬小定理 + 快速冪)
Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of mul
hdu4549矩陣快速冪+費馬小定理
次方 pla pragma nod 技術分享 gif 矩陣 end eof 轉移矩陣很容易求就是|0 1|,第一項是|0| |1 1| |1| 然後直接矩陣快速冪,要用到費馬小定理 :假如p
ACM-ICPC 2018 焦作賽區網路預賽 G 隔板+費馬小定理 L矩陣快速冪
G 思路:隔板法知道結果是 2 ^ ( n - 1 ),n過大。 費馬小定理為 a^(p-1) ≡ 1 mod p ; a, p 互質,p為質數。 所以2^(p-1)% p 為1,2^k*(p-1
ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies 【快速模冪+費馬小定理】
1000ms 65536K There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes