题意:给定一个字符串,问你能不能通过重排,使得任意一个素数p <= 字符串长度n,并且 任意的 i <= 长度n/素数p,满足s[p] == s[p*i]。

析:很容易能够看出来,只要是某个素数的小于等于该素数的倍数都是一样的,然后如果他和其他素数也有倍数,那么这些位置也是一样的,

所以我们只要找到任意一个小于等于 n 的素数与该素数相乘都大于 n的,然后用把数目最少的字符种给它,然后剩下的给那些。

所以能够看出lcm(2, i) (i是素数) <= n的那么这些位置包括小于等于该素数的倍都应该是一样的,然后容易解决了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 50;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int num;
char ch;
bool operator < (const Node &p) const{
return num < p.num;
}
};
Node a[30];
char s[maxn];
bool is_prime(int n){
int m = sqrt(n+0.5);
for(int i = 2; i <= m; ++i) if(n % i == 0) return false;
return true;
} int main(){
while(scanf("%s", s) == 1){
int n = strlen(s);
memset(a, 0, sizeof a);
for(int i = 0; i < n; ++i) ++a[s[i]-'a'].num, a[s[i]-'a'].ch = s[i];
sort(a, a+26);
int cnt = 0;
memset(s, 0, sizeof s);
for(int i = 0; i < 26; ++i) if(a[i].num){ cnt = i; s[0] = a[i].ch; --a[i].num; break; }
int num = 1;
for(int i = 2; i <= n; ++i) if(is_prime(i)){
if(2 * i <= n) continue;
++num;
while(!a[cnt].num) ++cnt;
s[i-1] = a[cnt].ch;
--a[cnt].num;
} while(!a[cnt].num) ++cnt;
bool ok = true;
if(a[cnt].num == n-num){ for(int i = 1; i < n; ++i) if(s[i] == 0) s[i] = a[cnt].ch; }
else if(n > 1) ok = false;
printf("%s\n", ok ? "YES" : "NO");
if(ok) puts(s);
} return 0;
}

CodeForces 124C Prime Permutation (数论+贪心)的更多相关文章

  1. Codefoces 432C Prime Swaps(数论+贪心)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...

  2. 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 ...

  3. Prime Permutation

    Prime Permutation 原题地址: http://codeforces.com/problemset/problem/123/A 题目大意: 给你一个字符串(只包含小写字母),从1开始存放 ...

  4. Tsinsen A1504. Book(王迪) 数论,贪心

    题目:http://www.tsinsen.com/A1504 A1504. Book(王迪) 时间限制:1.0s   内存限制:256.0MB   Special Judge 总提交次数:359   ...

  5. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  6. [CodeForces - 1225C]p-binary 【数论】【二进制】

    [CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...

  7. Codeforces H. Prime Gift(折半枚举二分)

    题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...

  8. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  9. [Codeforces 1208D]Restore Permutation (树状数组)

    [Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...

随机推荐

  1. 初学数位DP

    所谓数位dp,字面意思就是在数位上进行dp,数位的含义:一个数有个位.十位.百位.千位.等等,数的每一位就是数位. 数位DP一般应用于: 求出给定区间[A,B]内,符合条件P[i]的数 i 的个数. ...

  2. Java面向对象学习-----类的成员变量2

    请定义一个交通工具(Vehicle)的类,其中有: 属性:速度(speed),体积(size)等等 方法:移动(move()),设置速度(setSpeed(int speed)),加速speedUp( ...

  3. POJ 1191 DP+DFS棋盘分割问题

    题目大意: Description 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形 ...

  4. restful(1):序列化

    restful协议中,一切皆是资源,操作只是请求方式 model_to_dict()方法: from django.forms.models import model_to_dict obj = Pu ...

  5. NOIP2012提高组D1T3 开车旅行

    n<=100000个山,每个山有高度,从一个山到另一个山代价为高度差,有A和B两人一起开车,A每次选前进方向的次近山,B选最近,保证山高度不同且如果代价相同的山低的代价算小,每次旅行先A走,然后 ...

  6. sql语句在Mysql中如何执行?

    1.MySQL 主要分为 Server 层和引擎层,Server 层主要包括连接器.查询缓存.分析器.优化器.执行器,同时还有一个日志模块(binlog),这个日志模块所有执行引擎都可以共用,redo ...

  7. linux 用 rsync 快速删除大量小文件

    假设我们在目录 /tmp/to_delete 下有很多小文件 a1 a2 a3 f1 f2 f3 现在我们想快速的删除f 开头的文件. 如果文件量大,用rm 可能会失败,而且会很慢, 所以用rsync ...

  8. js获取上传的文件名称

    <input name="file_" type="file" id="file_" size="100" /&g ...

  9. MapReduce Cross 示例

    MapReduce Cross 示例 package com.bsr.cross; import java.io.IOException; import org.apache.hadoop.conf. ...

  10. Windows 2008 R2 SP1部署WSUS 3.0 SP2

    1 实验环境 1)域: 域名为fengxja.com: 网段:192.168.0网段,不连接外网. 域功能级别和林功能级别为Windows server 2003模式. 2)DC服务器: 域控制器: ...