【HDU - 3524】Perfect Squares(找规律,推公式,除法取模)

本文探讨了数学中完全平方数模2^n的问题,通过观察和分析,找到了奇数项与偶数项间增量的规律,最终推导出了求解完全平方数模2^n不同结果数量的公式。文章提供了AC代码实现,并讨论了解决除法取余问题的两种方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:求完全平方数摸2^n有多少不同的结果。

思路:

尝试了打表,最后还是看的别人的博客找到的规律(博客)。

这道题目的规律是奇数项之间的增量有规律,偶数项之间的增量有规律,发现这个后,能写出通项公式。这里要注意除法取余问题。

自己推出的公式:

这里需要解决的只有除法取余:(转自该博客
    1.可以用乘法的逆来解决,当然可知当成定理来用(a/b)%mod=(a*b^(mod-2))%mod,mod为素数
原理是费马小定理:a^(mod-1)%mod=1,又a^0%mod=1,所以a^(-1)=a^(mod-2),推出a/b=a*b^(-1)=a*b^(mod-2)
    2.上面的方法适合b比较大的时候,这里的b=3.很小,可知直接(a/b)%mod=(a%(mod*b)/b)%mod

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
const int mod=10007;
ll qsm(ll a,ll b,ll mod)
{
	ll t=1;
	while(b)
	{
		if(b&1)
		t=(t*a)%mod;
		a=(a*a)%mod;
		b>>=1; 
	} 
	return t%mod;
}
int main()
{
	int t,cas=1;
	cin>>t;
	ll n;
	while(t--)
	{
		scanf("%lld",&n);
		ll ans=0;
		if(n&1)
		ans=((qsm(4,(n+1)/2-1,3*mod)+5)%(3*mod))/3;
		else
		ans=((qsm(4,(n/2)-1,3*mod)*2+4)%(3*mod))/3;
		printf("Case #%d: %lld\n",cas++,ans);
	}
	
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值