Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
A. Prime Permutation
题目连接:
http://www.codeforces.com/contest/123/problem/A
Description
You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1.
Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters.
Input
The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000).
Output
If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO".
Sample Input
abc
Sample Output
YES
abc
Hint
题意
给你一个字符串,然后n是这个字符串的长度。
你可以重排,然后要求满足s[p] = s[ip],p是一个小于等于n的素数。
问你行不行。
题解:
这道题唯一的问题就是,2和3这种素数,他们的公因数是6,他们俩所需要的字母就应该是一样的
数据范围看了一下,才1000,所以直接暴力就好了
其实数据范围是100000也可以做,直接拿个并查集,把一些公因数存在的数直接缩在一起就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1200;
vector<int> p;
int n;
string s;
int vis[maxn];
int ans[maxn];
int num[30];
int getpri()
{
for(int i=2;i<=n;i++)
{
if(vis[i])continue;
p.push_back(i);
for(int j=i;j<=n;j+=i)
vis[j]=1;
}
}
int main()
{
cin>>s;
n=s.size();
getpri();
for(int i=0;i<s.size();i++)
num[s[i]-'a']++;
for(int i=0;i<=n;i++)
ans[i]=-1;
for(int i=0;i<p.size();i++)
{
int k = -1,t = -1;
for(int j=1;j<=n;j++)
if(j%p[i]==0&&ans[j]!=-1)t=ans[j];
if(t==-1)
{
for(int j=0;j<26;j++)
if(num[j]>k)k=num[j],t=j;
}
for(int j=p[i];j<=n;j+=p[i])
{
if(ans[j]!=-1)continue;
if(num[t]==0)return puts("NO"),0;
ans[j]=t;
num[t]--;
}
}
for(int i=0;i<26;i++)
if(num[i]>0)ans[1]=i;
cout<<"YES"<<endl;
for(int i=1;i<=n;i++)
printf("%c",ans[i]+'a');
printf("\n");
}
Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力的更多相关文章
- Codeforces Beta Round #92 (Div. 2 Only) B. Permutations
You are given n k-digit integers. You have to rearrange the digits in the integers so that the diffe ...
- Codeforces Beta Round #97 (Div. 1) B. Rectangle and Square 暴力
B. Rectangle and Square 题目连接: http://codeforces.com/contest/135/problem/B Description Little Petya v ...
- Codeforces Beta Round #4 (Div. 2 Only) A. Watermelon【暴力/数学/只有偶数才能分解为两个偶数】
time limit per test 1 second memory limit per test 64 megabytes input standard input output standard ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
随机推荐
- React Native DEMO for Android
Demo1: 主要知识:navigator,fecth 地址:https://github.com/hongguangKim/ReactNativeDEMO1 Demo2: 主要知识:navigato ...
- OGG生成数据定义文件的参数NOEXTATTR
./defgen paramfile ./dirprm/jzjj.prm NOEXTATTR In OGG 11.2, there is a new parameter NOEXTATTR. This ...
- Mac nginx 配置
nginx 安装: 在苹果系统下如果要安装nginx,首先要安装brew.安装brew可以查看网站:https://brew.sh: 一条命令即可搞定:/usr/bin/ruby -e "$ ...
- Python3 反射及常用的方法
反射就是通过字符串映射或修改程序运行时的状态.属性.方法 有四个常用方法: hasattr(obj,name_str) 判断一个obj对象是否有对应name_str的方法 getattr(obj,na ...
- Linux内存高,触发oom-killer问题解决
最近遇到两起Linux的内存问题,其一是触发了oom-killer导致系统挂 1. 首先确认该系统的版本是32位 ? #uname -a Linux alarm 2.6.9-67.ELsmp #1 S ...
- SPOJ JZPLIT
Problem SPOJ Solution 考虑任意一个作为矩阵四个角的位置 \(r_i \oplus c_j\oplus a_{i,j}\oplus x_{i,j}=0\) \(r_i \oplus ...
- 最小的Django应用
创建一个hello.py 内容如下: import sys from django.conf import settings # 设置 settings.configure( DEBUG = Tr ...
- 学习笔记----float后不与前面元素同行解决办法。
<li>文本<span> 16-08-17</span></li> 当非float的元素和float的元素在一起的时候(如上代码), 如果非float元 ...
- ISSCC 2017论文导读 Session 14:A 288μW Programmable Deep-Learning Processor with 270KB On-Chip Weight
A 288μW Programmable Deep-Learning Processor with 270KB On-Chip Weight Storage Using Non-Uniform Mem ...
- Linux 用户篇——用户管理命令之id、whoami、su、chage
一.浅谈id.whoami.su.chage 本篇是续写上一篇<Linux 用户篇——用户管理命令之useradd.passwd.userdel.usermod>. (1)id命令 命令格 ...