hdu 3524 Perfect Squares 快速幂+ 除法取余

 

Perfect Squares

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 510    Accepted Submission(s): 278


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   |   We have carefully selected several similar problems for you:   3519  2294  3509  3521  2842 
 


 f(x)/fm  %mod =f(x)%(fm*mod)/fm;

<span style="font-size:18px;">int even(int  k    )
{

    k--;
    int a=4,ans=1;
    while(k)
    {
        if(k&1)
         ans=ans*a%mod;

         a=a*a%mod;

        k>>=1;

    }
    ans=(2*ans+4)%mod;
    ans=ans/3;    //     f(x)/fm  %mod =f(x)%(fm*mod)/fm;
    return ans;

}

int odd(int k)
{
    k--;
    int a=4,ans=1;
    while(k)
    {
        if(k&1)
         ans=ans*a%mod;

         a=a*a%mod;

        k>>=1;

    }
    ans=(ans+5)%mod;
     ans=ans/3;
     return ans;
}
int main()
{
    int T,x,ans,kase=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&x);
        if(x%2)  ans=odd( (x+1)/2 );
        else ans=even( x/2 );
        printf("Case #%d: %d\n",++kase,ans);
    }


    return 0;
}
</span>



头文件:

<span style="font-size:18px;">#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second

using namespace std;
const int INF =0x3f3f3f3f;
//const int maxn=    ;
const int mod=3*10007;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123</span>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值