Hdu 3524 Perfect Squares inverse element

Source: Internet
Author: User

Hdu 3524 Perfect Squares inverse element

 

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 too 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 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 brespon. 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
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
212

Sample Output
Case #1: 2Case #2: 2

 

Tell a number n, and find the result of the exact number 2 ^ n. I didn't say the range of the full number of partitions. I thought it would be a loop. If I had a look at the table, I would find that there were regular conditions when n was an odd number.

N is an odd number of 2 + (4 ^ N-1)/3

N is an even number, 2 + 2/3*(4 ^ (n/2-1)-1)

There are a large number of molecules in Division calculation, so we need to use reverse elements. For reverse elements, see this click to open the link.

 

 

#include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 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,ca,ans);    }    return 0;}
       
      
     
    
   

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.