codeforces 414A A. Mashmokh and Numbers(素数筛)
题目链接:
1 second
256 megabytes
standard input
standard output
It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh.
In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x, y) points. At the beginning of the game Bimokh has zero points.
Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn't know how choose the initial sequence in the right way.
Please, help him. Find n distinct integers a1, a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 10^9.
The first line of input contains two space-separated integers n, k (1 ≤ n ≤ 10^5; 0 ≤ k ≤ 10^8).
If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 10^9).
5 2
1 2 3 4 5
5 3
2 4 3 7 1
7 2
-1
gcd(x, y) is greatest common divisor of x and y.
题意:
一个数列的数各不相同,每次取走前两个,得到gcd(x,y)的分数,问是否存在这样的长度为n,最后得分为k的数列;
思路:
不存在的就不说了;
存在的时候可以第一次把k-n/2+1的分数得到,剩下的全都是gcd(x,y)==1的情况,需要用素数筛先处理出素数;
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e6+5e5;
int a[N],flag[N];
int cnt=;
int getprime()
{ mst(flag,);
for(int i=;i<N;i++)
{
if(!flag[i])
{
a[cnt++]=i;
for(int j=;j*i<N;j++)
{
flag[i*j]=;
}
}
}
return cnt;
} int main()
{
int n,k;
scanf("%d%d",&n,&k);
getprime();
if(n==)
{
if(k==)cout<<""<<endl;
else cout<<"-1"<<endl;
}
else
{
if(n/>k)cout<<"-1"<<endl;
else
{
int m=k-n/+;
printf("%d %d ",m,*m);
int num=;
for(int i=;i<cnt&&num<n-;i++)
{
if(a[i]==m||a[i]==*m)continue;
else
{
printf("%d ",a[i]);
num++;
}
}
}
} return ;
}
最新文章
- 【转载】VGA时序与原理
- C语言中格式化输出的转换说明的fldwidth和precision解析
- Delphi在StatusBar上绘制ProgressBar
- vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)
- 浅析Java中synchronized与static synchronized
- LOJ #2533. 「CTSC2018」暴力写挂(边分治合并)
- 第四次:渗透练习,xss学习
- week_one-python用户登录
- UDP广播包
- html常用标签整理
- explan各项说明
- 在Editplus直接运行程序的步骤
- Oracle 11G Client客户端安装
- 更新pip10后 ImportError: cannot import name ‘main&#39;
- Redis五大类型操作使用以及订阅发布功能
- 原生js(一)
- 兼容安卓和ios实现一键复制内容到剪切板
- UVa 10055
- 014 Java的反射机制
- Tomcat中部署Java Web应用程序的方式