CodeForces 124C Prime Permutation (数论+贪心)

题意:给定一个字符串,问你能不能通过重排,使得任意一个素数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
", ok ? "YES" : "NO");
        if(ok)  puts(s);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5966206.html