hdu 3524 Perfect Squares(找规律,循环节,快速幂取模)
来源:互联网 发布:it工作者 编辑:程序博客网 时间:2024/06/05 04:12
Perfect Squares
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 546 Accepted Submission(s): 293
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.
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).
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
212
Sample Output
Case #1: 2Case #2: 2
思路:不知如何入手,先打个表再说。前15项是
2 2 3 4 7 12 23 44 87 172 343 684 1367 2732 5463
可以发现规律:
奇数项:2 3 7 23 87 343 1367 5463
偶数项:2 4 12 44 172 684 2732
奇数项分别相差1,4,16,64,256,1024
偶数项相差2,8,32,128,512,2048
所以奇数项f[n]=f[n-1]+2^(2*n-4),偶数项f[n]=f[n-1]+2^(2*n-3)
所以可以求得通项公式
奇数项f[n]=(2^(2*n-2)+5)/3
偶数项f[n]=(2^(2*n-1)+4)/3
所以对于一个给定的数即可直接代入公式求解了。注意此处除以3不能直接取模会出错。我们可以对3*mod取模,然后最后再除以3.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define LL long long#define mod 30021LL pow_mod(LL a,LL n){ LL ans=1; while(n) { if(n&1) ans=ans*a%mod; a=a*a%mod; n>>=1; } return ans;}int main(){ int T; LL n,ans; scanf("%d",&T); for(int t=1;t<=T;t++) { scanf("%lld",&n); if(n&1) ans=((pow_mod(2,2*(n/2+1)-2)+5)%mod)/3; else ans=(pow_mod(2,2*(n/2)-1)+4)%mod/3; printf("Case #%d: %lld\n",t,ans); } return 0;}
0 0
- hdu 3524 Perfect Squares(找规律,循环节,快速幂取模)
- HDU 3524 Perfect Squares 数学找规律
- hdu 3524 Perfect Squares 打表+找规律+快速幂+除法取余
- hdu 3524 Perfect Squares
- hdu 3524 Perfect Squares 快速幂+ 除法取余
- HDU 1005 循环节(找规律题目)
- hdu 1005(找规律--循环节)
- hdu 1005 找规律(循环点)
- hdu 3524 Perfect Squares 推公式求逆元
- hdu (找规律)
- hdu 3524 Perfect Squares【打表、除法取余、快速幂】
- HDU 4602 Partition (整数拆分&找规律&快速幂取模)
- HDU 4365Palindrome graph(找规律 快速幂取模)
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- hdu 3519 快速幂矩阵(7)+找规律
- HDU 1097 A hard puzzle(找规律,快速幂)
- HDU 4990 (找规律 矩阵快速幂)
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- Multi-class Classification
- N的阶乘HDOJ1042
- 爬虫之路——DAY1
- 一个判断网络是否连接的工具类
- 关于C#窗体中的三类坐标系
- hdu 3524 Perfect Squares(找规律,循环节,快速幂取模)
- JAVA中 byte[]数组如何转为InputStream
- CentOS7 修改网卡名称为eth0 & 在VMWare中添加多网卡配置
- Android 轻松实现仿淘宝地区选择
- Tachyon内存文件系统
- MySQL创建复合主键
- openSUSE vsftpd 搭建
- bootstrap学习总结
- 移动端开发的一些技巧