hdu 3524 Perfect Squares

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.
 

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
 

Source
 

Recommend
zhengfeng
 

题意:给你一个n,把所有的平方数对n^2取模,得到的值放在一个集合中,求集合的元素个数。。。
感觉没什么可以做的方法,打表找规律吧~
思路:公式+快速幂

//  打表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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值