# Codeforces Round #137 (Div. 2)

C：

#include
#include
#include
#include
using namespace std;
#define pb push_back

const int maxp = 10000010;
const int maxn = 100000 + 10;
int pri[777777], pnum, minp[maxp];
bool vis[maxp];
int a[maxn], b[maxn], cnt1[maxp], cnt2[maxp];

// O(n)筛素数法，minp[i]表示i的最小的素因子
void get_prime(int n) {
vis[1] = 1; pnum = 0;
for(int i = 2;i <= n; i++) {
if(!vis[i]) {
pri[pnum++] = i;
minp[i] = i;
}
for(int j = 0;j < pnum; j++) {
if(pri[j] * i > n)  break;
vis[pri[j] * i] = 1;
minp[pri[j] * i] = pri[j];
if(i % pri[j] == 0) break;
}
}
}

vector pr1, pr2;
int main() {
get_prime(10000000);

int n, m, mx = 0;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
mx = max(mx, a[i]);
int cur = a[i];
while(cur > 1) {
cnt1[minp[cur]] ++;
cur /= minp[cur];
}
}
for(int i = 0;i < m; i++) {
scanf("%d", &b[i]);
mx = max(mx, b[i]);
int cur = b[i];
while(cur > 1) {
cnt2[minp[cur]] ++;
cur /= minp[cur];
}
}
for(int i = 1;i <= mx; i++)
cnt1[i] = cnt2[i] = min(cnt1[i], cnt2[i]);
for(int i = 0;i < n; i++) {
int cur = a[i], now = cur;
while(cur > 1) {
int tmp = minp[cur];
if(cnt1[tmp]) now /= tmp, cnt1[tmp]--;
cur /= tmp;
}
pr1.pb(now);
}
for(int i = 0;i < m; i++) {
int cur = b[i], now = cur;
while(cur > 1) {
int tmp = minp[cur];
if(cnt2[tmp])   now /= tmp, cnt2[tmp]--;
cur /= tmp;
}
pr2.pb(now);
}
sort(pr1.begin(), pr1.end());
sort(pr2.begin(), pr2.end());
printf("%d %d\n", n, m);
for(int i = 0;i < n; i++)   printf("%d ", pr1[i]); puts("");
for(int i = 0;i < m; i++)   printf("%d ", pr2[i]); puts("");
return 0;
}



D：

E：

#include
#include
#include
using namespace std;
typedef __int64 ll;

const int mod = 1000000007 ;

char s[11];

int pow_mod(int q[55][55], ll n, int m) {
int a[55][55], aa[55][55], qq[55][55];
for(int i = 0;i < m; i++)
for(int j = 0;j < m; j++)
a[i][j] = i == j ? 1 : 0;
while(n) {
if(n & 1) {
for(int i = 0;i < m; i++)
for(int j = 0;j < m; j++) {
aa[i][j] = 0;
for(int k = 0;k < m; k++)
aa[i][j] = (aa[i][j] + (ll)a[i][k]*q[k][j])%mod;
}
for(int i = 0;i < m; i++)
for(int j = 0;j < m; j++)
a[i][j] = aa[i][j];
}
for(int i = 0;i < m; i++)
for(int j = 0;j < m; j++) {
qq[i][j] = 0;
for(int k = 0;k < m; k++)
qq[i][j] = (qq[i][j] + (ll)q[i][k]*q[k][j])%mod;
}
for(int i = 0;i < m; i++)
for(int j = 0;j < m; j++)
q[i][j] = qq[i][j];
n /= 2;
}
ll ret = 0;
for(int i = 0;i < m; i++)
for(int j = 0;j < m; j++)
ret += a[i][j];
return ret%mod;
}

int a[55][55];
int main() {
int  m, k;
ll n;
scanf("%I64d%d%d", &n, &m, &k);
for(int i = 0;i < m; i++)
for(int j = 0;j < m; j++)
a[i][j] = 1;
for(int i = 0;i < k; i++) {
scanf("%s", s);
int x, y;
if(s[0] <= 'Z')
x = 26 + s[0] - 'A';
else
x = s[0] - 'a';
if(s[1] <= 'Z')
y = 26 + s[1] - 'A';
else
y = s[1] - 'a';
a[x][y] = 0;
}
printf("%d\n", pow_mod(a, n-1, m));
return 0;
}


• 本文已收录于以下专栏：

## 【优化&大师级代码欣赏】Codeforces Round #137 (Div. 2) / 222B Cosmic Tables (矩阵)

B. Cosmic Tables http://codeforces.com/problemset/problem/222/B time limit per test 3 seconds m...

## Codeforces Beta Round #98 (Div. 2) / 137A Postcards and photos (模拟)

A. Postcards and photos http://codeforces.com/problemset/problem/137/A time limit per test 2 seco...

## 【Codeforces Round 263 (Div 2)A】【水题 认真读题】Appleman and Easy Task 每个点的相邻o是否都为偶数

Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes ...

## Codeforces Round #350 (Div. 2) F. Restore a Number(有一位数为n的数,在后面添上n这个数,然后将所有的数字打乱输出,再给你一串原数字的子串,输出最小)

F. Restore a Number time limit per test 2 seconds memory limit per test 256 megabytes ...

## 【Codeforces Round 271 (Div 2)F】【贪心 线段树】Ant colony 区间段内是其他所有数因子的数的个数

Ant colony time limit per test 1 second memory limit per test 256 megabytes input ...

## 【Codeforces Round 328 (Div 2)A】【水题】PawnChess 棋子向上向下走

#include #include #include #include #include #include #include #include #include #include #include #...

## 【Codeforces Round 335 (Div 2) C】【贪心 脑洞 有趣排序】Sorting Railway Cars 全排列排序可以提前后提后的最小操作数

C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes inpu...

举报原因： 您举报文章：Codeforces Round #137 (Div. 2) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)