博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3524 Perfect Squares 循环节+快速幂取模+找规律
阅读量:3905 次
发布时间:2019-05-23

本文共 1974 字,大约阅读时间需要 6 分钟。

Problem Description

A number x is called a perfect square if there exists an integer b

satisfying x=b^2. There are many beautiful theorems about perfect squares in mathematics. Among which, Pythagoras Theorem is the most famous. It says that if the length of three sides of a right triangle is a, b and c respectively(a < b <c), then a^2 + b^2=c^2.
In this problem, we also propose an interesting question about perfect squares. For a given n, we want you to calculate the number of different perfect squares mod 2^n. We call such number f(n) for brevity. For example, when n=2, the sequence of {i^2 mod 2^n} is 0, 1, 0, 1, 0……, so f(2)=2. Since f(n) may be quite large, you only need to output f(n) mod 10007.

 

 

Input

The first line contains a number T<=200, which indicates the number of test case.

Then it follows T lines, each line is a positive number n(0<n<2*10^9).

 

 

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is f(x).

 

 

Sample Input

 

2

1

2

 

 

Sample Output

 

Case #1: 2

Case #2: 2

此题可以用两种解法求解:

方法一:

代码如下:

#include 
#include
#include
#include
#include
using namespace std;const int Mod=10007;typedef long long ll;int t;ll n;ll fastpow (ll a,ll b){ ll sum=1; while (b>0) { if(b&1) sum=sum*a%Mod; a=a*a%Mod; b>>=1; } return sum;}int main(){ scanf("%d",&t); ll inv3=fastpow(3,Mod-2); for (int i=1;i<=t;i++) { scanf("%lld",&n); ll ans; if(n&1) ans=(fastpow(2,n-1)+5)%Mod*inv3%Mod; else ans=(fastpow(2,n-1)+4)%Mod*inv3%Mod; printf("Case #%d: %lld\n",i,ans); } return 0;}

方法二:

用的循环节...

代码如下:

#include 
#include
#include
#include
#include
using namespace std;const int Mod=10007;typedef long long ll;int t;int n;int f[Mod+3];int main(){ f[0]=2; f[1]=2; for (int i=2;i

 

 

 

转载地址:http://baaen.baihongyu.com/

你可能感兴趣的文章
graph slam tutorial :从推导到应用2
查看>>
graph slam tutorial :从推导到应用3
查看>>
texstudio语法检查
查看>>
ComboBox用AddString添加字符显示乱码
查看>>
基于CSerialPort修改类的串口调试助手源代码(支持中文、自动保存等)
查看>>
MFC下边自动寻找串口
查看>>
PID调节经验
查看>>
机器学习经典书籍
查看>>
Latex排版全解
查看>>
2D-slam 激光slam: 开源代码的比较HectorSLAM Gmapping KartoSLAM CoreSLAM LagoSLAM
查看>>
北航王田苗教授:国内外机器人发展热点与趋势(精华版)
查看>>
windows常用软件收集
查看>>
Markdown语法注意借鉴
查看>>
Java 容器Collection(5)
查看>>
Java IO操作(6)
查看>>
Java数组(3)
查看>>
Java线程(7)
查看>>
Java GUI基础(9)
查看>>
Java网络基础(8)
查看>>
Java_正则表达式
查看>>