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.
InputThe 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).
OutputFor 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都会有循环节,给你n,计算循环节里有多少个不同的数字;
解题思路:这暴力道题我是没有发现有什么解决的办法,毕竟暴力解题肯定会超时,所以查看了大佬门的博客,看他们是怎么解题的,首先他们先进行打表,打一部分答案的表,然后找规律,进行解题;看着他们的表,然后根据他们的总结规律,得出f(n)是有规律的,得分奇偶考虑;
奇数:f (i )=( 2 ^(2i-2 )+5) /3 (i = n/2 + 1);
偶数:f ( i)=(2 ^(2i - 1)+4) / 3 (i= n/2 );
然后利用快速幂解决问题。
#include <iostream>
#include <cstdio>
using namespace std;
#define mod 10007
long long sove(long long a,long long b)
{
long long ans=1;
while(b)
{
if(b&1) ans=(ans*a)%mod;
b/=2;
a=a*a%mod;
}
return ans;
}
int main()
{
int t,s=1;
long long ans;
cin>>t;
long long mod2=sove(3,mod-2);
while(t--)
{
long long n;
cin>>n;
if(n%2==1) ans=(sove(2,n-1)+5)%mod*mod2%mod;
else ans=(sove(2,n-1)+4)%mod*mod2%mod;
cout<<"Case #"<<s<<": "<<ans<<endl;
s++;
}
return 0;
}