1. 程式人生 > >HDOJ 4704 Sum(費馬小定理+快速冪)

HDOJ 4704 Sum(費馬小定理+快速冪)

Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1940    Accepted Submission(s): 809


Problem Description

Sample Input 2
Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases. 分離整數:總和為2^(n-1); 由於n非常大,所以這裡要用到費馬小定理:a^(p-1)%p == 1%p == 1;//p為素數 所以2^n%m == ( 2^(n%(m-1))*2^(n/(m-1)*(m-1)) )%m == (2^(n%(m-1)))%m * ((2^k)^(m-1))%m == (2^(n%(m-1)))%m;//k=n/(m-1) ac程式碼:  
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define MAXN 1000100
#define MOD 1000000007
#define LL long long
using namespace std;
char s[MAXN];
LL fun(LL a,LL b)//資料大,全程longlong吧
{
	LL ans=1;
	while(b)
	{
		if(b%2)
		ans=ans*a%MOD;
		a=a*a%MOD;
		b/=2;
	}
	return ans;
}
int main()
{
	int i;
	while(scanf("%s",s)!=EOF)
	{
		int len=strlen(s);
		LL num=0;
		for(i=0;i<len;i++)
		{
			num=(num*10+s[i]-'0')%(MOD-1);
		}
	    LL ans=fun(2,num-1);
	    printf("%lld\n",ans);
	}
	return 0;
}

相關推薦

HDOJ 4704 Sum定理+快速

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm

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     

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

HDOJ 題目4704 Sum定理快速

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm

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

HDU 4704 Sum 定理

題意: 不知道為什麼java超時: import java.math.BigInteger; import java.util.Scanner; public class Main {

hdu 4704 Sum定理

數論,費馬小定理 a^(p-1) % p == 1,長見識了 #include <cstdio> #include <algorithm> #include <vector> using namespace std; typedef

hdu 4704 Sum定理解題報告

Problem Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multip

HDU4704:Sum定理 & 隔板法

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Su

[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 Sum(隔板+定理·大數取模)

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1907    Accepted Submis

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

HDU 4704 Sum 定理+快速

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 18    Accepted Submission

牛客網-F-發電定理+線段樹

連結:https://www.nowcoder.com/acm/contest/136/F 來源:牛客網   時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 262144K,其他語言524288K 64bit IO Format: %lld 題目描述  

HDU 4704 [定理+快速] ---狗眼不識多校

Description Input 2 Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file c

白兔的式子定理+逆元

題目描述 已知f[1][1]=1,f[i][j]=a*f[i-1][j]+b*f[i-1][j-1] (i>=2,1<=j<=i)。 對於其他情況f[i][j]=0 有T組

定理+快速取模】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) %