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 ...
随机推荐
- Axure RP 授权码
Axure RP 8.1.0.3372Licensee:KoshyKey:wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6x ...
- jQuery的validation插件(验证表单插件)
更完整的参考:http://www.runoob.com/jquery/jquery-plugin-validate.html 验证隐藏字段的使用(验证通过后ajax提交表单):http://www. ...
- Git和Github简单教程【转】
转自:https://www.cnblogs.com/schaepher/p/5561193.html#clone 原文链接:Git和Github简单教程 网络上关于Git和GitHub的教程不少,但 ...
- onvif客户端
前言 做开发有8年时间了,ffmpeg和onvif与我是特别有缘的了(说着玩的,我更认为是因为他们确实强大^_^). ffmpeg在毕业设计时就有用到,5年后做windows.linux播放库时又有用 ...
- C中常用格式格式码
一.常用printf格式码 二.常用scanf格式码
- css给奇数行或偶数行添加指定样式
odd表示奇数行,even表示偶数行; tr:nth-child(odd); .table-striped > tbody > tr:nth-child(odd) { background ...
- 4、GitLab 创建、删除、修改项目
一.gitLab创建项目 1.创建用户组 2.填写组信息后单击“Create group” 其中:“Group path”将显示在git路径中 3.选择需要加入该组的“用户”和“角色”后点击“Add ...
- 动手编写TCP服务器系列之一:日志文件
前言 在几个月之前,笔者想自己实现一个性能比较良好的基于tcp的服务器.于是断断续续写了个把月,因为还需要找工,还有论文什么的.拖了这么久.现在开辟这样的一个博客,我想记录下自己的思路,也和大家分享自 ...
- 20165301陈潭飞2017-2018-2 20165301 实验三《Java面向对象程序设计》实验报告
2017-2018-2 20165301 实验三<Java面向对象程序设计>实验报告 一.敏捷开发与XP实践-1 实验要求: 在IDEA中使用工具(Code->Reformate C ...
- 1、量化投资—为什么选择Python?
Python在量化领域的现状 就跟Java在web领域无可撼动的地位一样,Python也已经在金融量化投资领域占据了重要位置,从各个业务链条都能找到相应的框架实现. 在量化投资(证券和比特币)开源项目 ...