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=;
const int maxx=1e6+;
ll a,b;
int prime[maxx];
bool vis[maxx];
int cnt;
void init()///欧拉筛
{
cnt=;
memset(vis,true,sizeof(vis));
vis[]=vis[]=false;
for(int i=;i<=maxx;i++)
{
if(vis[i])
prime[cnt++]=i;
for(int j=;j<cnt && prime[j]*i<=maxx;j++)
{
vis[ i*prime[j] ]=false;
if( i%prime[j]== ) break;///prime[j]必定是prime[j]*i和i的最小质因子
}
}
}
ll sum(ll n)///立方和公式
{
return ( (n*n+n)/ )%p * ( (n*n+n)/ )%p;
} int main()
{
init();
int x=;
while(scanf("%lld%lld",&a,&b)!=EOF)
{
ll ans=;
if(vis[a])//a是素数,则直接求,节省时间
ans=sum(+b);
else
{//prime[i]*prime[i]<=a;不加就会超时,大素数的因子一般只有一个,没必要一个一个找,也可以改成!vis[a],都是避免多次循环找大素数
for(int i=; i<cnt && prime[i]*prime[i]<=a; i++)
{
int num=;
if(a==) break;
while(a%prime[i]==)
{
a=a/prime[i];
num++;
}
if(num!=)
{
ans*=sum(num*b+);//积性函数
ans%=p;
}
}
if(a!=)///应对大素数的情况
{
ans*=sum(b+);
ans%=p;
}
}
printf("Case %d: %lld\n",++x,ans%p);
}
return ;
}
/*
手撸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
*/
 

hdu2421-Deciphering Password-(欧拉筛+唯一分解定理+积性函数+立方求和公式)的更多相关文章

  1. 2018南京icpc-J-Prime Game (欧拉筛+唯一分解定理)

    题意:给定n个数ai(n<=1e6,ai<=1e6),定义,并且fac(l,r)为mul(l,r)的不同质因数的个数,求 思路:可以先用欧拉筛求出1e6以内的所有质数,然后对所有ai判断, ...

  2. hdu3826-Squarefree number-(欧拉筛+唯一分解定理)

    Squarefree number Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. 【BZOJ 2749】 2749: [HAOI2012]外星人 (数论-线性筛?类积性函数)

    2749: [HAOI2012]外星人 Description Input Output 输出test行,每行一个整数,表示答案. Sample Input 1 2 2 2 3 1 Sample Ou ...

  4. hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  5. noip复习——线性筛(欧拉筛)

    整数的唯一分解定理: \(\forall A\in \mathbb {N} ,\,A>1\quad \exists \prod\limits _{i=1}^{s}p_{i}^{a_{i}}=A\ ...

  6. [模板] 积性函数 && 线性筛

    积性函数 数论函数指的是定义在正整数集上的实或复函数. 积性函数指的是当 \((a,b)=1\) 时, 满足 \(f(a*b)=f(a)*f(b)\) 的数论函数. 完全积性函数指的是在任何情况下, ...

  7. 欧拉筛,线性筛,洛谷P2158仪仗队

    题目 首先我们先把题目分析一下. emmmm,这应该是一个找规律,应该可以打表,然后我们再分析一下图片,发现如果这个点可以被看到,那它的横坐标和纵坐标应该互质,而互质的条件就是它的横坐标和纵坐标的最大 ...

  8. 【BZOJ 2190】【SDOI 2008】仪仗队 欧拉筛

    欧拉筛模板题 #include<cstdio> using namespace std; const int N=40003; int num=0,prime[N],phi[N]; boo ...

  9. [51NOD1181]质数中的质数(质数筛法)(欧拉筛)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181 思路:欧拉筛出所有素数和一个数的判定,找到大于n的最小质 ...

随机推荐

  1. Java 13 - Java 数组

    Java 数组 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java语言中提供的数组是用来存储固定大小的同类型元素. 你可以声明一个数组变量,如num ...

  2. Java方法通过RestTemplate调用restful接口

    背景:项目A需要在代码内部调用项目B的一个restful接口,该接口是POST方式,header中 Authorization为自定义内容,主要传输的内容封装在body中,所以使用到了RestTemp ...

  3. python学习之----lxml库和HTML parser

    lxml 这个库(http://lxml.de/)可以用来解析HTML 和XML 文档,以非常底层的实现而闻名 于世,大部分源代码是用C 语言写的.虽然学习它需要花一些时间(其实学习曲线越 陡峭,表明 ...

  4. IDEA下载Git中项目

     一.             打开idea,点击File>Settings,搜索git(安装系统默认设置即可) 二.        选择git 三.        Git中项目的路径粘贴到ID ...

  5. LeetCode 11. [👁] Container With Most Water & two pointers

    盛最多水的容器 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...

  6. activiti源代码的细节

    由于activiti-explorer-5.14的web演示程序使用的是vaadin服务器端ui组件,程序关键点找起来还是有些麻烦,vaadin的这种web框架,就是不需要专门美术界面人员,只需要程序 ...

  7. 13.App爬取相关库的安装(Charles,Mitmproxy,Appium)

    由于App没有像浏览器一样直观的后台请求工具,主要用一些抓包技术抓取数据.(目前也在学习安装,参考书籍.) 首先呢,一些简单的接口通过Charles或mitmproxy分析,找出规律,直接用程序去抓取 ...

  8. 49.纯 CSS 创作一支诱人的冰棍

    原文地址:https://segmentfault.com/a/1190000015257561 感想:重点在让色彩滚动起来->background-position: 0 1000vh; HT ...

  9. Flex验证器 validate stringvalidate

    1 validate <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns: ...

  10. java reflect反射调用方法invoke

    类定义 package Reflect; public class MyTest { public int a; public static int b; public static final in ...