题目大意:给出一个正整数n(n为合数),求n的一个划分(a1,a2,...,ak,...)(k>=2)。使得其在存在最大的最大公约数之下,存在最大的最小公倍数。

Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

数据规模:200<=n<=10^9。

理论基础:关于LCM的一个定理:LCM(a1,a2,a3,...,ai)为各个数分解质因数以后,所有存在的素数的最高幂次之积。具体参见链接1。

题目分析:

首先,要想有最大公公约数,那么假设它们的最大公约数为G,那么:G*(a1/G,a2/G,a3,...)=n,所以要想让G最大,那么只需要a1/G+a2/G+a3/G+...最小记为M,那么取M为n的最小素数因子即可。这样一来:M<=sqrt(10^9)<31625。问题转换为求素数M的一个划分,使划分中的数的嘴小公倍数最大。

根据定理,我们可以断言:将拆分成M=若干个1+p1^k1+p2^k2+p3^k3+...(其中p1,p2,p3,...等数两两互质且为素数)时,最小公倍数最大。

证明:首先,我们证明两两互质时最大。假设LCM最大时,仍有a1<=a2存在最大公约数k,那么a1=k*p,a2=k*q,这样一来a1与a2的最小公倍数为:k*p*q。那么我们进行如下操作。因为:a1,a2不互质,所以k>=2,那么我们将:a2拆成:a2=q+(k-1)*q,这样q,q(k-1),k*p,三个数两两互质且最小公倍数大于原来的最小公倍数,所以必然两两互质。

其次,我们证明,每个数都可以表示为:若干个1+p^k(p为素数),假设:存在合数(k*q)^i(k,p均为质数,且>=2),那么此数可以证明:k^i*p^i>k^i+p^i。(利用二元函数f(x,y)=(xy)^i-x^i-y^i可以证明,这里不多展开了)。所以,最后变为对各个素数的指数的动态规划问题。那么指标该怎么定呢?因为每两个数都是互质的,所以其最小公倍数即为已经存在的各个数之积,因为不需要求解LCM,所以大可用double存储。如果害怕double过大时,存在前十六位相同,导致状态转移错无,那我们可以用log来表示,因为:log是单增函数,且log(a*b)=loga+logb,便于状态转移。

double dp[N][M];

int pre[i][j];

我们用dp[i][j]表示用前i个数表示j时最优解,即log的最大值。

用pre[i][j]表示取dp[i][j]为最优解表示j时,j剩余的数。

状态转移方程:dp[i][j]=max(dp[i-1][j-p[i]]+logp[i],dp[i][j]);这样我们最后的最优解即为:dp[N][j]里最大的那一个然后:如果:j-pre[N][j]不为零的话就要输出(j-pre[N][j])*G,下来搜索dp[N-11][j-prime[N]]。最后dp[1][j]剩下来的值即为要输出的1*G的个数了。

代码如下:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<ctime>
#include<vector>
using namespace std;
typedef double db;
#define DBG 0
#define maa (1<<31)
#define mii ((1<<31)-1)
#define sl(c) ((int)(c).size()) //取字符串长度;
#define forl(i, a, b) for(int i = (a); i < (b); ++i) //不带边界值循环,正序
#define forle(i, a, b) for(int i = (a); i <= (b); ++i) //带边界值循环,正序
#define forh(i, a, b) for(int i = (a); i > (b); --i) //不带边界值,逆序
#define forhe(i, a, b) for(int i = (a); i >= (b); --i) //带边界值,逆序
#define forlc(i, a, b) for(int i##_b = (b), i = (a); i < i##_b; ++i) //带别名的循环,用于不可改变值
#define forlec(i, a, b) for(int i##_b = (b), i = (a); i <= i##_b; ++i)
#define forgc(i, a, b) for(int i##_b = (b), i = (a); i > i##_b; --i)
#define forgec(i, a, b) for(int i##_b = (b), i = (a); i >= i##_b; --i)
#define forall(i, v ) forl(i, 0, sz(v)) //循环所有
#define forallc(i, v ) forlc(i, 0, sz(v))
#define forlla(i, v ) forhe(i, sz(v)-1, 0)
#define forls(i, n, a, b) for(int i = a; i != b; i = n[i]) //搜表用
#define rep(n) for(int repp = 0; repp < (n); ++repp)
#define repc(n) for(int repp_b = (n), repp = 0; repp < repp_b; ++repp)
#define rst(a, v) memset(a, v, sizeof a) //把字符v填充到a reset 重置
#define cpy(a, b) memcpy(a, b, sizeof a) //copy b 的sizeof(a)个字符到a
#define rstn(a, v, n) memset(a, v, (n)*sizeof((a)[0])) //把字符v填充到a[n]之前的字节
#define cpyn(a, b, n) memcpy(a, b, (n)*sizeof((a)[0])) //copy b 的 n 个字符到a
#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试
#define dout DBG && cout << __LINE__ << ">>| "
#define pr(x) #x"=" << (x) << " | "
#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl
#define pra(arr, a, b) if(DBG) {\
dout<<#arr"[] |" <<endl; \
forlec(i, a, b) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \
if((b-a+1)%8) puts("");\
} //数列查看
#define rd(type, x) type x; cin >> x //读数
inline int rdi() { int d; scanf("%d", &d); return d; }
inline char rdc() { scanf(" "); return getchar(); }
inline string rds() { rd(string, s); return s; }
inline db rddb() { db d; scanf("%lf", &d); return d; }
template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }
template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; } typedef long long LL;
typedef long unsigned LU;
const int N=130;
const int M=40000;
double dp[N][M];
bool nprime[M]={true,true}; vector<int> prim;
int pre[N][M],n,m;
void init()
{
int bor=sqrt((float)M),temp;
forl(i,2,bor)
{
if(!nprime[i])
{
prim.push_back(i);
for(int j=i;(temp=j*i)<=M;j++)
nprime[temp]=true;
}
}
forlec(j,bor+1,M)
{
if(!nprime[j])prim.push_back(j);
}
pra(prim,0,prim.size()-1);
}
void solve(int n)
{
rst(dp,0);
int gcd=0,div;
for(int i=2;prim[i]*prim[i]<=n;i++)
{
if(n%prim[i]) continue;
gcd=n/prim[i];
break;
}
div=n/gcd;
forl(i,1,N)
{
int k=prim[i-1];
forlec(j,1,div)
{
dp[i][j]=dp[i-1][j];
pre[i][j]=j;
}
while(k<=div)
{
forlec(j,k,div)
{
if(dp[i][j]<dp[i-1][j-k]+log((double)k))
{
dp[i][j]=dp[i-1][j-k]+log((double)k);
pre[i][j]=j-k;
}
}
k*=prim[i-1];
}
}
int res[500],cnt=0,state=div,now=N-1;
int lcm=0;
forle(j,0,div)
{
if(lcm<dp[now][j])
{
lcm=dp[now][j];
state=j;
}
}
while(now)
{
int temp=pre[now][state];
if((state-temp)!=0)res[cnt++]=state-temp;
state=temp;
now--;
}
rep(state)res[cnt++]=1;
for(int i=0;i<cnt;i++) printf("%d%c",res[i]*gcd,i==cnt-1?'\n':' ');
}
int main()
{
init();
while(scanf("%d",&n)!=EOF)
{
if(n%2==0)
{
printf("%d %d\n",n/2,n/2);
continue;
}
if(n%3==0)
{
printf("%d %d\n",n/3*2,n/3);
continue;
}
solve(n);
}
return 0;
}

其中,对n%2,n%3的预处理可以省去很多麻烦,而且少了一大半的数。

参考文献:

http://zh.wikipedia.org/wiki/%E6%9C%80%E5%B0%8F%E5%85%AC%E5%80%8D%E6%95%B8

by:Jun_moon http://blog.csdn.net/Jsun_moon

URAL 1807的更多相关文章

  1. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  2. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  3. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  4. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  5. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  6. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  7. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

  8. ural 2066. Simple Expression

    2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...

  9. ural 2065. Different Sums

    2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...

随机推荐

  1. quote函数什么意思,怎么用

    转自: https://blog.csdn.net/qiqiyingse/article/details/70046543 quote函数 属于urllib库里面的一个函数 屏蔽特殊的字符.比如如果u ...

  2. django 动态url 可变

    首先在urls里面改,name=让一个映射敷个名字. 然后到books——list页面让编辑按钮改成这种可变的映射模式.

  3. android studio 添加有趣的注释模板 佛祖保佑无bug等

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...

  4. HTML5区块和大纲算法

    原文链接: Using HTML sections and outlines - Mozilla Developer Network 每集HTML5+CSS3网页布局教程-2大纲算法 HTML5标准带 ...

  5. 【ACM-ICPC 2018 沈阳赛区网络预赛】不太敢自称官方的出题人题解

    A. Gudako and Ritsuka 链接 by Yuki & Asm.Def 期望难度:Hard- 考虑从后往前进行博弈动态规划,在这一过程中维护所有的先手必胜区间.区间不妨采用左开右 ...

  6. Codeforces Round #358 (Div. 2) E. Alyona and Triangles 随机化

    E. Alyona and Triangles 题目连接: http://codeforces.com/contest/682/problem/E Description You are given ...

  7. Codeforces Beta Round #37 A. Towers 水题

    A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...

  8. Vue集成微信开发趟坑:公众号以及JSSDK相关

    首先,类库方面,Vue中引入JSSDK的话,请引入weixin-js-sdk,而不是weixin-jsapi,原因在于weixin-jsapi不是最新版:还要注意JS接口安全域名,不需要http前缀, ...

  9. angularjs鼠标移入移出实现显示隐藏

    <tr ng-repeat="item in items track by $index"> <td data-title="操作" alig ...

  10. composer安装Workerman报错:Installation failed, reverting ./composer.json to its original content.

    今天想在TP5上安装workerman,实现一个后台消息提醒功能. 第一步就卡住了,根据手册里说的首先通过composer安装 $ composer require topthink/think-wo ...