HDU4344(大数分解)
题意就是给一个数,然后求这个数的所有因子中组成的最大的一个子集,其中1和本身除外,使得在这个子集中元素两两互素,求最大子集的元素个
数,并且求出和最大的值。
找规律就不难发现其实答案就是先大数分解n,例如,180=2^2*3^2*5,那么就输出3 18 ,这两个数分别是素因子的个数和2^2,3^2,5的和。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream> const int Times=10;
const int N=550; using namespace std;
typedef unsigned __int64 LL; LL ct,cnt;
LL fac[N],num[N]; LL gcd(LL a,LL b)
{
return b? gcd(b,a%b):a;
} LL multi(LL a,LL b,LL m)
{
LL ans=0;
while(b)
{
if(b&1)
{
ans=(ans+a)%m;
b--;
}
b>>=1;
a=(a+a)%m;
}
return ans;
} LL quick_mod(LL a,LL b,LL m)
{
LL ans=1;
a%=m;
while(b)
{
if(b&1)
{
ans=multi(ans,a,m);
b--;
}
b>>=1;
a=multi(a,a,m);
}
return ans;
} bool Miller_Rabin(LL n)
{
if(n==2) return true;
if(n<2||!(n&1)) return false;
LL a,m=n-1,x,y;
int k=0;
while((m&1)==0)
{
k++;
m>>=1;
}
for(int i=0;i<Times;i++)
{
a=rand()%(n-1)+1;
x=quick_mod(a,m,n);
for(int j=0;j<k;j++)
{
y=multi(x,x,n);
if(y==1&&x!=1&&x!=n-1) return false;
x=y;
}
if(y!=1) return false;
}
return true;
} LL Pollard_rho(LL n,LL c)
{
LL x,y,d,i=1,k=2;
y=x=rand()%(n-1)+1;
while(true)
{
i++;
x=(multi(x,x,n)+c)%n;
d=gcd((y-x+n)%n,n);
if(1<d&&d<n) return d;
if(y==x) return n;
if(i==k)
{
y=x;
k<<=1;
}
}
} void find(LL n,int c)
{
if(n==1) return;
if(Miller_Rabin(n))
{
fac[ct++]=n;
return ;
}
LL p=n;
LL k=c;
while(p>=n) p=Pollard_rho(p,c--);
find(p,k);
find(n/p,k);
} int main()
{
int t;
LL n,ans;
scanf("%d",&t);
while(t--)
{
scanf("%I64u",&n);
ct=0;
find(n,120);
sort(fac,fac+ct);
num[0]=1;
int k=1;
for(int i=1;i<ct;i++)
{
if(fac[i]==fac[i-1])
++num[k-1];
else
{
num[k]=1;
fac[k++]=fac[i];
}
}
cnt=k;
LL ret=0;
for(int i=0;i<cnt;i++)
{
LL temp=1;
for(int j=0;j<num[i];j++)
temp*=fac[i];
ret+=temp;
}
if(cnt==1) ret/=fac[0];
printf("%I64u %I64u\n",cnt,ret);
}
return 0;
}
HDU4344(大数分解)的更多相关文章
- poj1181 大数分解
//Accepted 164 KB 422 ms //类似poj2429 大数分解 #include <cstdio> #include <cstring> #include ...
- poj 1811 随机素数和大数分解(模板)
Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...
- Pollard_Rho大数分解模板题 pku-2191
题意:给你一个数n, 定义m=2k-1, {k|1<=k<=n},并且 k为素数; 当m为合数时,求分解为质因数,输出格式如下:47 * 178481 = 8388607 = ( ...
- poj 1811 大数分解
模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...
- poj 2429 Pollard_rho大数分解
先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...
- 模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811
题意:是素数就输出Prime,不是就输出最小因子. #include <cstdio> #include<time.h> #include <algorithm> ...
- 【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho
http://www.spoj.com/problems/FACT0/en/ 给一个小于1e15的数,将他分解. miller-rabin & pollard-rho模板 #include & ...
- 数学--数论---P4718 Pollard-Rho算法 大数分解
P4718 [模板]Pollard-Rho算法 题目描述 MillerRabin算法是一种高效的质数判断方法.虽然是一种不确定的质数判断法,但是在选择多种底数的情况下,正确率是可以接受的.Pollar ...
- poj2429 大数分解+dfs
//Accepted 172 KB 172 ms //该程序为随机性算法,运行时间不定 #include <cstdio> #include <cstring> #includ ...
随机推荐
- Delphi下用API代码创建Form
program PMyWindowClass; uses Windows, Messages, SysUtils; type TMyWindow = class(TObject) priva ...
- ASP.NET - 一般处理程序获取session值
1.要在一般处理程序中获取其他页面的session值,需要引用名空间: using System.Web.SessionState; 2.然后继承一个接口:IRequiresSessionState, ...
- 【ASP.NET Web API教程】2.4 创建Web API的帮助页面
原文:[ASP.NET Web API教程]2.4 创建Web API的帮助页面 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 2.4 ...
- spring mvc接收JSON格式的参数
1.配置spring解析json的库 <dependency> <groupId>org.codehaus.jackson</groupId> ...
- 【IOS实例小计】UIImageView
预备知识: UIImage 是一个专门存储图片数据的对象,默认兼容的图片格式是 PNG,可以通过文件.Quartz image对象或 image Data数据得到一个图片对象. UIImage相关功能 ...
- 基于visual Studio2013解决面试题之0402合并升序链表并去重
题目
- 基于visual Studio2013解决面试题之0209最大堆排序
题目
- javascript 自己主动绑定JS callback 的方法函数
自己写的一个javascript 智能绑定callback 而且调用运行的函数.主要用于异步请求的 ajax中: <!DOCTYPE html> <html> <head ...
- alv行可编辑时带出描述
ALV显示可以编辑的状态下可以带出描述信息等,比如维护表程序输入公司代码时需要带出公司代码的描述,这时就需要通过下面事件来触发 定义一个类: CLASS lcl_event_receiver DEFI ...
- 14.2.5.2 Clustered and Secondary Indexes
14.2.5.2 Clustered and Secondary Indexes : 每个InnoDB 表 有一个特别的索引称为clustered index 行数据存储的地方. 典型的,cluste ...