zoukankan      html  css  js  c++  java
  • HDU3524 数论

    Perfect Squares

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


    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
    题意:
    求i^2%2^n有多少个不同的结果(i>=0,n是给出的数),最后总数%10007;
    代码:
    /*
    就是打表+找规律 
        n为偶数,f[n]=(2^(n-1)-2)/3+2; 
        n为奇数,f[n]=(2^(n-1)-1)/3+2; 
        这里需要解决的只有除法取余: 
        
        费马小定理(Fermat Theory)是数论中的一个重要定理,其内容为: 假如p是质数,且gcd(a,p)=1,那么 
        a(p-1)≡1(mod p)。即:假如a是整数,p是质数,且a,p互质(即两者只有一个公约数1),那么a的
        (p-1)次方除以p的余数恒等于1。
        
        1.可以用乘法的逆来解决,当然可知当成定理来用(a/b)%mod=(a*b^(mod-2))%mod,mod为素数 
        原理是费马小定理:a^(mod-1)%mod=1,又a^0%mod=1,所以a^(-1)=a^(mod-2),推出a/b=a*b^(-1)=a*b^(mod-2) 
    */
    #include<iostream>
    #include<cmath>
    using namespace std;
    const int mod=10007;
    int solve(int a,int n)
    {
        if(n==0) return 1;
        int x=solve(a,n/2);
        long long ans=(long long)x*x%mod;
        if(n&1) ans=ans*a%mod;
        return (int)ans;
    }
    int main()
    {
        int t,n;
        cin>>t;
        for(int i=1;i<=t;i++){
            cin>>n;
            cout<<"Case #"<<i<<": ";
            int ans;
            if(n&1) ans=((solve(2,n-1)-1)*solve(3,mod-2))%mod+2;
            else ans=((solve(2,n-1)-2)*solve(3,mod-2))%mod+2;
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    基本类型
    匿名对象和匿名方法
    定时任务@SChedule详解
    docker的配置和安装
    数据库的学习
    docker的学习
    nginx的学习
    yyyy-MM-dd HH:mm:ss.SS的大小写的含义
    单例模式的学习
    layer的学习
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6281773.html
Copyright © 2011-2022 走看看