快速幂 HDU-1021 Fibonacci Again , HDU-1061 Rightmost Digit , HDU-2674 N!Again

1.   Fibonacci Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 77116    Accepted Submission(s): 34896

Problem Description

There are another kind of Fibonacci
numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).

Input

Input consists of a sequence
of lines, each containing an integer n. (n < 1,000,000).

Output

Print the word "yes"
if 3 divide evenly into F(n).

Print the word "no" if not.

Sample Input

0
1
2
3
4
5

Sample Output

no
no
yes
no
no
no

数据级别在1,000,000,就正常做不会超时,最简单的快速幂,根据 (a * b) % p = (a % p * b % p) % p 。
#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		int f0=7,f1=11;
		int fn;
		for(int i=2;i<=n;i++){
			fn=(f0+f1)%3;
			f0=f1%3;
			f1=fn%3;
		}
		if(n<=1){
			cout<<"no"<<endl;
		}
		else{
			if(fn%3==0)
			cout<<"yes"<<endl;
			else
			cout<<"no"<<endl;
		}
	}
	return 0;
}

  

2.  Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 71510    Accepted Submission(s): 26554

Problem Description

Given a positive
integer N, you should output the most right digit of N^N.

Input

The input contains
several test cases. The first line of the input is a single integer T which is
the number of test cases. T test cases follow.

Each test case contains a single positive integer N(1<=N<=1,000,000,000).

Output

For each test
case, you should output the rightmost digit of N^N.

Sample Input

2

3

4

Sample Output

7

6



数据级别10亿,上一种就无法完成了,需要用另一种方法降低n的规模。

#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
	int k;
	cin>>k;
	while(k--){
	long long n;
		cin>>n;
		long long res=1;long long a=n,b=n;
		while(b){
			if(b&1)
				res=(res*a)%10;
			a=(a*a)%10;
			b/=2;
		}
		cout<<res<<endl;
	}
	return 0;
}

3.  NAgain

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6338    Accepted Submission(s): 3325

Problem Description

WhereIsHeroFrom:
            Zty,
what are you doing ?

Zty:                                     I
want to calculate N!......

WhereIsHeroFrom:
            So
easy! How big N is ?

Zty:                                    1
<=N <=1000000000000000000000000000000000000000000000…

WhereIsHeroFrom:
            Oh! You
must be crazy! Are you Fa Shao?

Zty:
                                    No.
I haven‘s finished my saying. I just said I want to calculate N! mod 2009

Hint : 0! = 1, N! = N*(N-1)!

Input

Each line will
contain one integer N(0 <= N<=10^9). Process to end of file.

Output

For each case,
output N! mod 2009

Sample Input

4

5

Sample Output

24

120



10^9这种数据级别已经不是正常做法能解出来的了,一定有特殊数据,试一下就能找到。

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
	long long n;
	while(scanf("%lld",&n)!=EOF){
		long long res=1;
		if(n>=41)
		cout<<0<<endl;
		else{
		  for(int i=1;i<=n;i++){
			res=(res*i)%2009;
		}
		cout<<res<<endl;
		}
	}
	return 0;
}

各种快速幂很详细:https://yq.aliyun.com/wenji/254837

原文地址:https://www.cnblogs.com/czc1999/p/10111573.html

时间: 2024-11-10 10:56:17

快速幂 HDU-1021 Fibonacci Again , HDU-1061 Rightmost Digit , HDU-2674 N!Again的相关文章

HDU 1061 Rightmost Digit题解

求一个大数N^N的值的最右边的数字,即最低位数字. 简单二分法求解就可以了. 不过注意会溢出,只要把N % 10之后,就不会溢出了,不用使用long long. #include <stdio.h> int rightMost(int n, int N) { if (n == 0) return 1; int t = rightMost(n / 2, N); t = t * t % 10;; if (n % 2) t *= N; return t % 10; } int main() { in

HDOJ 1061 Rightmost Digit

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30543    Accepted Submission(s): 11624 Problem Description Given a positive integer N, you should output the most right digit of N

HDU - 1061 - Rightmost Digit (快速幂取模!)

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34329    Accepted Submission(s): 13091 Problem Description Given a positive integer N, you should output the most right digit of

hdu oj 1061 Rightmost Digit (快速幂算法)

这里首先要讲解一下快速幂算法: 快速幂取模算法 在网站上一直没有找到有关于快速幂算法的一个详细的描述和解释,这里,我给出快速幂算法的完整解释,用的是C语言,不同语言的读者只好换个位啦,毕竟读C的人较多~ 所谓的快速幂,实际上是快速幂取模的缩写,简单的说,就是快速的求一个幂式的模(余).在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快.计算范围更大的算法,产生了快速幂取模算法.[有读者反映在讲快速幂部分时有点含糊,所以在这里对本文进行了修改,作了更详细的补充,争取让更多的读者一目

hdu 1021 Fibonacci Again(变形的斐波那契)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1021 Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 70782    Accepted Submission(s): 32417 Problem Description There are another ki

矩阵快速幂 POJ 3070 Fibonacci

题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e3 + 10; 11 const int INF = 0x3f3f3f3f;

矩阵乘法快速幂 codevs 1732 Fibonacci数列 2

1732 Fibonacci数列 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 在“1250 Fibonacci数列”中,我们求出了第n个Fibonacci数列的值.但是1250中,n<=109.现在,你的任务仍然是求出第n个Fibonacci数列的值,但是注意:n为整数,且1 <= n <= 100000000000000 输入描述 Input Description 输入有多组数据,每

HDU ACM 1061 Rightmost Digit

1.每次计算只要取最后一位即可. 2.使用快速幂提高效率. #include<iostream> using namespace std; int pow(int a,int n) { if(n==1) return a; else if(n%2==1) return (a*pow(a,n-1))%10; else { int tmp; tmp=pow(a,n>>1); return (tmp*tmp)%10; } } int main() { int T,n; cin>&g

HDU 1061.Rightmost Digit【数论及方法】【8月30】

Rightmost Digit Problem Description Given a positive integer N, you should output the most right digit of N^N. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test ca

杭电 HDU ACM 1061 Rightmost Digit

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37888    Accepted Submission(s): 14268 Problem Description Given a positive integer N, you should output the most right digit of