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. Fragment与Radiogroup联动,经典的主界面布局。使用show和hide的方式实现;

    Fragment+RadioGroup经典的主界面布局,方便实用: 1.使用replace方式: 直接上代码,先是布局文件: <?xml version="1.0" enco ...

  2. Dom4j解析、生成Xml

    1以下代码未Xml的解析和生成代码 <?xml version="1.0" encoding="UTF-8"?> <users> < ...

  3. python-ddt 数据驱动测试

    # @File : learn_ddt.py #-*- coding:utf-8 -*- #本次学习:ddt ---data drive test--数据驱动测试 #1.安装 pip install ...

  4. android 开发 实现一个ListView套嵌GirdView的滚动布局

    效果图 实现思维: 首先要处理管理好需要导入的数据,我们这里创建class来处理这些数据并且便于管理它们. 创建一个主activity的布局,里面需要一个ListView控件. 创建一个class继承 ...

  5. android 开发 实现一个app的引导页面,使用ViewPager组件(此引导的最后一页的Button会直接写在最后一页布局里,跟随布局滑进滑出)

    基本ViewPager组件使用方式与我之前写的https://blog.csdn.net/qq_37217804/article/details/80332634 这篇博客一致. 下面我们将重点详细解 ...

  6. python中len 小练习:获取并输出集合中的索引及对应元素

    len()用来获取长度.字节等数值 1 a = ["hello", "world", "dlrb"] 2 b = len(a) 3 for ...

  7. django 模板context的理解

    context作为view与template之间的桥梁,理解它的工作原理对于djagno的模板工作机制至关重要. class ContextDict(dict):#上下文词典,由词典可以通过conte ...

  8. 使用子查询创建表(oracle)

    转自:https://blog.csdn.net/lxh123456789asd/article/details/81164321 语句: CREATE TABLE tablename[(column ...

  9. J2SE 8的流库 --- 转换流, 得到的还是流

    流的转换, 按照条件过滤/映射/摊平/截取/丢弃/连接/去重/排序. 辅助方法 public static int myCompare(String x, String y) { if(x.lengt ...

  10. JEECG前后端分离UI框架实战抢先体验(ng2-admin+Angular4+AdminLTE+WebStorm)

    JEECG前后端分离UI框架 (ng2-admin+Angular4+AdminLTE) 关键词: ng2-admin.Angular4.AdminLTE.Nodejs.Jeecg JEECG紧跟技术 ...