分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

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

发布时间:2023-09-06 02:26责任编辑:胡小海关键词:暂无标签

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 Fibonaccinumbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).

  

Input

Input consists of a sequenceof 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;elsecout<<"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 positiveinteger N, you should output the most right digit of N^N.

 

Input

The input containsseveral test cases. The first line of the input is a single integer T which isthe 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 testcase, 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:                                     Iwant to calculate N!......
WhereIsHeroFrom:            Soeasy! How big N is ?
Zty:                                    1<=N <=1000000000000000000000000000000000000000000000…
WhereIsHeroFrom:            Oh! Youmust 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 willcontain 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

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

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved