hdu 3524 Perfect Squares 推公式求逆元

Perfect Squares

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 501    Accepted Submission(s): 272

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

告诉一个数字n,求完全平方数摸2^n有多少不同的结果。没有说完全平方数的范围,想的话应该是循环着的,打表看一看,会发现n分奇数偶数时是有规律的。

n为奇数  2+(4^n-1)/3

n为偶数 2+2/3*(4^(n/2-1)-1)

算除法时分子很大,所以要用到逆元,求逆元的可以参看这篇 点击打开链接

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
typedef long long ll;
const int mod=10007;

using namespace std;

ll fun(ll n,ll m)
{
    ll b=1;
    while(m)
    {
        if(m&1) b=b*n%mod;
        n=n*n%mod;
        m>>=1;
    }
    return b;
}

int main()
{
    int T;
    ll n;

    ll tmp=fun(3,mod-2);

    scanf("%d",&T);
    for(int ca=1;ca<=T;ca++)
    {
        scanf("%lld",&n);

        ll ans;
        if(n%2)
        {
            ll t=(fun(4,n/2)-1)%mod;
            ans=(2+t*tmp%mod)%mod;
        }

        else
        {
            ll t=(fun(4,n/2-1)-1)%mod;
             ans=(2+2*t*tmp%mod)%mod;
        }
        printf("Case #%d: %lld\n",ca,ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-16 16:44:04

hdu 3524 Perfect Squares 推公式求逆元的相关文章

LightOJ - 1050 (唯一分解+推公式+乘法逆元)

题意:求a^b的所有约数和对1e9+7取模的结果 思路:对于一个数p,进行唯一分解,则p=P1^M1*P2^M2*...*Pn^Mn,则p的所有约数之和等于(P1^0+P1^1+...+P1^M1)*(P2^0+P2^1+...+P2^M2)*...*(Pn^0+Pn^1+...+Pn^Mn), p^t=P1^(M1*t)*P2^(M2*t)*...*Pn^(Mn*t),每一个(Pn^0+Pn^1+...+Pn^Mn)利用等比数列可以直接推出公式为(Pn^(Mn*t+1)-1)/(Pn-1),用

HDU 1018 -- Big Number (Stirling公式求n!的位数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 40551    Accepted Submission(s): 19817 Problem Description In many applications ver

zoj 3903 Ant(推公式,逆元)

Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going hiking very much. Today, she wants to climb a cuboid. The length of cuboid's longest edge is n, and the other edges are all positive integers. Alice's

HDU-1852-Beijing 2008-一个神奇的公式求逆元

As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a little special somehow. You are looking forward to it, too, aren't you? Unfortunately there still are months to go. Take it easy. Luckily you meet me. I

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[

HDU 4870 Rating(概率、期望、推公式) &amp;&amp; ZOJ 3415 Zhou Yu

其实zoj 3415不是应该叫Yu Zhou吗...碰到ZOJ 3415之后用了第二个参考网址的方法去求通项,然后这次碰到4870不会搞.参考了chanme的,然后重新把周瑜跟排名都反复推导(不是推倒)四五次才上来写这份有抄袭嫌疑的题解... 这2题很类似,多校的rating相当于强化版,不过原理都一样.好像是可以用高斯消元做,但我不会.默默推公式了. 公式推导参考http://www.cnblogs.com/chanme/p/3861766.html#2993306 http://www.cn

HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法

地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 754    Accepted Submission(s): 279 Problem Description When ?? was born, seven crows flew

HDU 1165 Eddy&#39;s research II (推公式)

Eddy's research II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3122    Accepted Submission(s): 1137 Problem Description As is known, Ackermann function plays an important role in the sphere

hdu 6128 Inverse of sum(推公式)

题目链接:hdu 6128 Inverse of sum 题意: 给你n个数,问你有多少对i,j,满足i<j,并且1/(ai+aj)=1/ai+1/aj 在%p意义下. 题解: 不愧是高中生,推公式神题. 将式子通分化简后可得(ai2+aj2+ai*aj)%p=0. 然后两边同时将两边乘(ai-aj),化简可得(ai3-aj3)%p=0. 然后就可以用map记录一下个数,并且减掉ai==aj时不合法的情况就行了. 1 #include<bits/stdc++.h> 2 #define F