zoukankan      html  css  js  c++  java
  • hdu2421-Deciphering Password-(欧拉筛+唯一分解定理+积性函数+立方求和公式)

    Deciphering Password

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2357    Accepted Submission(s): 670


    Problem Description
    Xiaoming has just come up with a new way for encryption, by calculating the key from a publicly viewable number in the following way:
    Let the public key N = AB, where 1 <= A, B <= 1000000, and a0, a1, a2, …, ak-1 be the factors of N, then the private key M is calculated by summing the cube of number of factors of all ais. For example, if A is 2 and B is 3, then N = AB = 8, a0 = 1, a1 = 2, a2 = 4, a3 = 8, so the value of M is 1 + 8 + 27 + 64 = 100.
    However, contrary to what Xiaoming believes, this encryption scheme is extremely vulnerable. Can you write a program to prove it?
     
    Input
    There are multiple test cases in the input file. Each test case starts with two integers A, and B. (1 <= A, B <= 1000000). Input ends with End-of-File.
    Note: There are about 50000 test cases in the input file. Please optimize your algorithm to ensure that it can finish within the given time limit.
    Output
    For each test case, output the value of M (mod 10007) in the format as indicated in the sample output.
     
    Sample Input
    2 2 1 1 4 7
     
    Sample Output
    Case 1: 36 Case 2: 1 Case 3: 4393
     
    翻译:输入a和b,n=a^b,求n的因子有哪些,这些因子又有多少个因子数,对于这些因子数的立方和累加
     
    唯一分解定理
    定义:任何一个数可以表示成 素数的次方 的乘积
    分解公式: 
    求N的因子个数公式 num=(a1+1)*(a2+1)*......*(an+1);
     
    积性函数
    对于任意互质的整数a和b有性质f(ab)=f(a)f(b)的数论函数
     
    素数的之间互质,素数的幂次方也互质
    对于求因子个数,也是积性函数
    素数的n次方有(n+1)个因子,并且这些因子的因子个数从1到n+1递增,这就可以套用1到n的立方和公式
    比如3^4=81,因子有1,3,9,27,81
     1-1
     3-1,3
     9-1,3,9
    27-1,3,9,27
    81-1,3,9,27,81
    AC代码:
    注:这段代码比较玄,用c++提交WA,用g++提交能AC,中间有一句避免多次循环找大素数没有加上的话,c++提交WA,g++提交TLE
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<iostream>
    #include<cstring>
    #define inf 0x3f3f3f3f
    using namespace std;
    #define ll long long
    const int p=10007;
    const int maxx=1e6+5;
    ll a,b;
    int prime[maxx];
    bool vis[maxx];
    int cnt;
    void init()///欧拉筛
    {
        cnt=0;
        memset(vis,true,sizeof(vis));
        vis[0]=vis[1]=false;
        for(int i=2;i<=maxx;i++)
        {
            if(vis[i])
                prime[cnt++]=i;
            for(int j=0;j<cnt && prime[j]*i<=maxx;j++)
            {
                vis[ i*prime[j] ]=false;
                if( i%prime[j]==0 ) break;///prime[j]必定是prime[j]*i和i的最小质因子
            }
        }
    }
    ll sum(ll n)///立方和公式
    {
       return ( (n*n+n)/2 )%p * ( (n*n+n)/2 )%p;
    }
    
    int main()
    {
        init();
        int x=0;
        while(scanf("%lld%lld",&a,&b)!=EOF)
        {
            ll ans=1;
            if(vis[a])//a是素数,则直接求,节省时间
                ans=sum(1+b);
            else
            {//prime[i]*prime[i]<=a;不加就会超时,大素数的因子一般只有一个,没必要一个一个找,也可以改成!vis[a],都是避免多次循环找大素数
                for(int i=0; i<cnt && prime[i]*prime[i]<=a; i++)
                {
                    int num=0;
                    if(a==1) break;
                    while(a%prime[i]==0)
                    {
                        a=a/prime[i];
                        num++;
                    }
                    if(num!=0)
                    {
                        ans*=sum(num*b+1);//积性函数
                        ans%=p;
                    }
                }
                if(a!=1)///应对大素数的情况
                {
                    ans*=sum(b+1);
                    ans%=p;
                }
            }
            printf("Case %d: %lld
    ",++x,ans%p);
        }
        return 0;
    }
    /*
    手撸36=2^2 * 3^2  ans=(1^3 + 2^3 +3^3)^2=1296
    36的因子有    1  2  3  4  6  9  12  18  36
    对应的因子数  1  2  2  3  4  3  6   6   9
    累加后也是1296
    */
     
  • 相关阅读:
    250 浅拷贝Object.assign(target, ...sources),深拷贝
    249 递归:概念,利用递归求1~n的阶乘,利用递归求斐波那契数列,利用递归遍历数据
    248 闭包:概念,作用,案例,思考题案例,chrome 中调试闭包
    247 高阶函数 之 函数可以作为参数传递
    246 JavaScript严格模式
    245 改变函数内部 this 指向:call,apply,bind,call、apply、bind 三者的异同
    244 函数内部的this指向:6种
    243 函数:函数的3种定义方式,函数的6种调用方式
    242 Object.defineProperty
    241 获取对象的属性名:Object.keys(对象)
  • 原文地址:https://www.cnblogs.com/shoulinniao/p/10356459.html
Copyright © 2011-2022 走看看