【贪心】【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 ...
随机推荐
- centos网速特别慢的最佳解决的方法 - 关闭ipv6
我使用了centOS,可是发现网速实在是卡得差点儿不能上网,连百度都打不开,可是win却飞快. 后来想到偶然记得有一次看过一段话,说到关闭ipv6,測试来一下,果然有效,关闭来ipv6打开网速飞快. ...
- 基于 koajs 的前后端分离实践
一.什么是前后端分离? 前后端分离的概念和优势在这里不再赘述,有兴趣的同学可以看各个前辈们一系列总结和讨论: 系列文章:前后端分离的思考与实践(1-6) slider: 淘宝前后端分离实践 知乎提问: ...
- react-native 环境配置及hello world
一.前言 最近手头的工作繁多,有研究性的项目和系统研发,正好遇到同事离职,接手了框架的UI组件,不仅需要维护和填坑,还需要开发新的功能组件.因为身在H5-Hybird的框架部门,最近团队开始尝试使用R ...
- NPOI兼容 excel2003,2007版本
根据项目需要,需要对excel进行导入导出,所以选择NPOI,优点在这里就不详细介绍了,下面进入正题. public int Import(string path) { IList<Studen ...
- sublime 快键
Keyboard Shortcuts - Windows/Linux Warning This topic is a draft and may contain wrong information. ...
- css系列教程--margin padding column(完结)
margin/margin-left/margin-right/margin-top/margin-bottom设置边距属性margin:0;--所有外边距0margin:0 1px;--margin ...
- jq操作cookie
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C++中把string转成char
c_str函数的返回值是const char*的,不能直接赋值给char*, c++语言提供了两种字符串实现,其中较原始的一种只是字符串的c语言实现. 与C语言的其他部分一样,它在c+的所有实现中可用 ...
- 使用Toad导入导出dmp数据
进入Toad,选择Database->Import->Import Utility Wizard(导入则选择:Export->Export Utility Wizard与导入类似故不 ...
- OC——网络解析获取图片的应用
headimageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, DEVW, DEVW/2)]; headimageView.cont ...