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
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?
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.
in the format as indicated in the sample output.
1 1
4 7

#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-(欧拉筛+唯一分解定理+积性函数+立方求和公式)的更多相关文章
- 2018南京icpc-J-Prime Game (欧拉筛+唯一分解定理)
题意:给定n个数ai(n<=1e6,ai<=1e6),定义,并且fac(l,r)为mul(l,r)的不同质因数的个数,求 思路:可以先用欧拉筛求出1e6以内的所有质数,然后对所有ai判断, ...
- hdu3826-Squarefree number-(欧拉筛+唯一分解定理)
Squarefree number Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 【BZOJ 2749】 2749: [HAOI2012]外星人 (数论-线性筛?类积性函数)
2749: [HAOI2012]外星人 Description Input Output 输出test行,每行一个整数,表示答案. Sample Input 1 2 2 2 3 1 Sample Ou ...
- hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)
GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- noip复习——线性筛(欧拉筛)
整数的唯一分解定理: \(\forall A\in \mathbb {N} ,\,A>1\quad \exists \prod\limits _{i=1}^{s}p_{i}^{a_{i}}=A\ ...
- [模板] 积性函数 && 线性筛
积性函数 数论函数指的是定义在正整数集上的实或复函数. 积性函数指的是当 \((a,b)=1\) 时, 满足 \(f(a*b)=f(a)*f(b)\) 的数论函数. 完全积性函数指的是在任何情况下, ...
- 欧拉筛,线性筛,洛谷P2158仪仗队
题目 首先我们先把题目分析一下. emmmm,这应该是一个找规律,应该可以打表,然后我们再分析一下图片,发现如果这个点可以被看到,那它的横坐标和纵坐标应该互质,而互质的条件就是它的横坐标和纵坐标的最大 ...
- 【BZOJ 2190】【SDOI 2008】仪仗队 欧拉筛
欧拉筛模板题 #include<cstdio> using namespace std; const int N=40003; int num=0,prime[N],phi[N]; boo ...
- [51NOD1181]质数中的质数(质数筛法)(欧拉筛)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181 思路:欧拉筛出所有素数和一个数的判定,找到大于n的最小质 ...
随机推荐
- day18包的使用与日志(logging)模块
包的使用与日志(logging)模块1. 什么是包 包就是一个包含有__init__.py文件的文件夹 包本质就是一种模块,即包是用包导入使用的,包内部包含的文件也都是用来被导入使用2 为 ...
- J2EE与EJB
问题及答案来源自<Java程序员面试笔试宝典>第五章 Java Web 5.2 J2EE与EJB 1.什么是J2EE? J2EE是Java平台企业版的简称,是用来开发和部署企业级应用的一个 ...
- Java并发编程:Java Thread 的 sleep() 和 wait() 的区别
1. start 和 run 方法解释: 1) start: 用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码.通过调用Thread类 ...
- HDFS在web端无法访问文件
解决办法1: [root@djt002 hadoop]# vi /etc/selinux/config 改为 SELINUX=disabled 解决办法2: 查看你的$HADOOP_HOME/etc/ ...
- ios自动监测更新
http://blog.csdn.net/davidsph/article/details/8931718
- 《GPU高性能编程CUDA实战》第三章 CUDA设备相关
▶ 这章介绍了与CUDA设备相关的参数,并给出了了若干用于查询参数的函数. ● 代码(已合并) #include <stdio.h> #include "cuda_runtime ...
- SPARK执行流程
RDD运行原理 1.创建 RDD 对象 2.DAGScheduler模块介入运算,计算RDD之间的依赖关系.RDD之间的依赖关系就形成了DAG 3.每一个JOB被分为多个Stage,划分Stage的一 ...
- python 实例属性
>>> class a(object): pass >>> o=a() >>> o.a='ok'#直接给实例添加属性,调用了__setattri_ ...
- C#并口热敏小票打印机打印位图包括芯片的写入
下面是打印所需要调用的代码: class LptControl { private string LptStr = "lpt1"; public LptControl(string ...
- react-native react-navigation StackNavigator android导航栏 标题下居中
navigationOptions:({ navigation }) => ({ , textAlign:'center' }}) 如上设置即可,如果有返回箭头,那么右边也要加一个 占位的或者设 ...