Perfect Squares
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 377 Accepted Submission(s): 218
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
2 1 2
Sample Output
Case #1: 2 Case #2: 2
Source
Recommend
zhengfeng
// 打表ing 找规律
// 得到的前十五项是:
// 2 2 3 4 7 12 23 44 87 172 343 684 1367 1824
// 发现奇数项的差分别是 1 4 16 64 256 1024
// 发现偶数项的差分别是 2 8 32 128 512
// 高中的时候学的说是,差为常数的等差数列,差为某个数幂的形式的是等比数列
// 所以这个规律是等比数列
// 奇数项 A(n)=4*A(n-1)-5 => (4^(n-1)+5)/3
// 偶数项 B(n)=4*B(n-1)-4 => (2*4^(n-1)+4)/3
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
#define mod 10007
using namespace std;
int Pow(int a,int b)
{
int ans=1;
while(b)
{
if(b&1)
{
b--;
ans*=a;
ans%=mod;
}
else
{
b/=2;
a*=a;
a%=mod;
}
}
return ans;
}
void solve(int n)
{
int a[10008];
int t=0;
for(long long i=1;i<=1000000;i++)
{
a[++t]=(i*i)%(Pow(2,n));
}
sort(a+1,a+t+1);
stack<int>q;
for(int i=1;i<=t;i++)
{
if(!q.size())
q.push(a[i]);
else
{
if(a[i]==q.top())
continue;
else
q.push(a[i]);
}
}
printf("%d\n",q.size());
}
int main()
{
for(int i=1;i<=15;i++)
{
printf("===================\n");
printf("%d.....",i);
solve(i);
printf("===================\n\n");
}
return 0;
}
// AC
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#define mod 10007
int Pow(int a,int b)
{
int ans=1;
while(b)
{
if(b&1)
{
b--;
ans*=a;
ans%=(3*mod);
}
else
{
b/=2;
a*=a;
a%=(3*mod);
}
}
return ans;
}
int solve(int n)
{
if(n&1)
{
n++;
n/=2;
int ans=Pow(4,n-1);
ans+=5;
ans/=3;
return ans%mod;
}
else
{
n/=2;
int ans=2*Pow(4,n-1);
ans+=4;
ans/=3;
return ans%mod;
}
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
int n;
scanf("%d",&n);
printf("Case #%d: %d\n",i,solve(n));
}
return 0;
}