【贪心】【TOJ4107】【A simple problem】
Given three integers n(1≤n≤1018), m(1≤m≤105), k(1≤k≤1018).
you should find a list of integer A1,A2,…,Am which
satisfies three conditions:
1. A1+A2+⋯+Am=n.
2. 1≤Ai≤k−1 for
each (1≤i≤m).
3. GCD(A1,A2,…,Am)=1.GCD
means the greatest common divisor
4. if i<j then Ai≥Aj.
As the author is too lazy to write a special judge, if there's no answer ouput "I love ACM", And if there's more than one answer, output the one has the minimum A1,
and if there still multiple answer make theA2 as
small as possible, then A3,A4…
m=1 直接 “I love ACM”
m=2
均摊 第二个给第一个不断给1
m=3
均摊 最后一个给第一个1个1
很多边界数据注意下
3 3 1
1 1 1
1 1 4
等等..
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
long long n,m,k;
long long gcd(long long a,long long b)
{
long long r;
while(b>0)
{
r=a%b;
a=b;
b=r;
}
return a;
}
long long A[100000+5];
void do1()
{
if(n%2==1)
{
long long p=n/2+1;
if(p<=k-1) printf("%lld %lld\n",p,p-1);
else printf("I love ACM\n");
}
else
{
int ok=1;
long long a=n/2,b=n/2;
while(a+1<=k-1&&b-1>=1)
{
a++;
b--;
if(gcd(a,b)==1)
{
ok=0;
printf("%lld %lld\n",a,b);
break;
}
}
if(ok)
printf("I love ACM\n");
}
}
void do2()
{
memset(A,0,sizeof(A));
for(int i=1;i<=m;i++)
{
A[i]=n/m;
}
for(int i=1;i<=n%m;i++)
{
A[i]++;
}
if(n%m==0&&n!=m)
{
A[1]++;A[m]--;
}
if(A[1]<=k-1&&A[m]>=1)
{
for(int i=1;i<=m;i++)
{
printf("%lld",A[i]);
if(i!=m) printf(" ");
}
printf("\n");
}
else printf("I love ACM\n");
}
int main()
{
while(cin>>n>>m>>k)
{
if(m==1&&n!=1||k==1) printf("I love ACM\n");
else if(m==1&&n==1) printf("1\n");
else
{
if(m==2) do1();
else if(m>=3) do2();
}
}
}
【贪心】【TOJ4107】【A simple problem】的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers(线段树/区间更新)
题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Description Yo ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77964 Acc ...
随机推荐
- 数据库的优化tips
数据库 TIPS:: 1.用于记录或者是数据分析的表创建时::使用Id作为主键,1,2,3...表示消息条数.用户账号id用于做外键.一个用户相应唯一个accountId ...
- 使用jqMobi开发app基础:弹出内容的设计
设计APP,因为屏幕非常小.在PC网页山能够放在一体的内容.在APP中就不能放在一起了. 比如例如以下.项目出勤人员非常多,须要弹出一个panel.然后让用户选择,怎样设计呢? 项目出勤panel的内 ...
- 有关Oracle cvu和cvuqdisk
有关Oracle cvu和cvuqdisk cvu的下载链接: http://www.oracle.com/technetwork/products/clustering/downloads/cvu- ...
- Unity 4.6 uGUI的点击事件
因为Unity 4.6刚刚发布,自带的uGUI功能的相关资料还不是很完善,今天刚装的Unity 4.6,想看一下uGUI是否好用,那么开始就今天的学习吧啊! 1,新建一个空的工程.
- Hacker(三)之黑客定位目标---IP
IP即Internet Protocol的简称,中文简称"网协",是为计算机网络相互连接进行通信而设计的协议.无论何种操作系统,只要遵守IP协议就可以与Internet互联互通. ...
- eclipse.ini内存设置
这两天用eclipse,突然变得很卡,就上网找了些资料,对eclipse.ini启动参数配置,整理如下: 1.先了解下JVM内存管理机制,JVM内存分为堆内存和非堆内存 2.JVM内存限制 首先JVM ...
- 归并排序java
import java.util.Arrays; public class MergeSort { public static void main(String[] args) { MergeSort ...
- Python和C#基本算法实现对比
最近在学习python,很多入门的例子又写了一遍,基本上是C#和Python都写了一遍,对比发现语言真是相通啊,只是语法不同而已. python开发也是用的VS,很好用,特别是代码段运行,选中一段py ...
- struts1:Struts的中央控制器
在Struts框架中,有多个组件分担控制器的工作,它们分别是ActionServlet类,RequestProcessor类和Action类等,其中ActionServlet是Struts框架中的核心 ...
- 判断字符串解析是JsonObject或者JsonArray
如下,用 JSONTokener 实现: Object json = new JSONTokener(stringData).nextValue(); if(json instanceof JSONO ...