1、POJ 3696 The Luckiest number
参考:http://www.cnblogs.com/chenxiwenruo/archive/2014/02/26/3569146.html
注意:需要用到快速乘
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
typedef pair<int, int> Pair;
const int INF = 0x7fffffff;
const int maxn = 5e4;
int prime[maxn];
ll divisor[1000000];
bool vis[maxn];
int prime_cnt = 0, divisor_cnt = 0, L, gcd, Case = 0;
ll mod;
void init(void);
ll euler(ll x);
ll degree(int a, ll phi);
ll my_pow(int x, ll n);
ll my_mul(ll x, ll y);
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
init();
while (scanf("%d", &L) != EOF && L != 0) {
gcd = __gcd(8, L);
int a = 10;
ll n = (ll)L / gcd * 9;
mod = n; divisor_cnt = 0;
if (__gcd(10ll, n) != 1) { printf("Case %d: 0\n", ++Case); continue; }
printf("Case %d: %I64d\n", ++Case, degree(a, euler(n)));
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}
ll degree(int a, ll phi) {
for (ll i = 1; i * i <= phi; ++i) {
if (phi % i == 0) {
if (phi / i == i) {
divisor[divisor_cnt++] = i;
} else {
divisor[divisor_cnt++] = i;
divisor[divisor_cnt++] = phi / i;
}
}
}
sort(divisor, divisor + divisor_cnt);
ll ret = 0;
REP(i, divisor_cnt) {
if (my_pow(a, divisor[i]) == 1) { ret = divisor[i]; break; }
}
return ret;
}
ll my_pow(int x, ll n) {
ll ret = 1, t = x % mod;
while (n) {
if (n & 1) { ret = my_mul(ret, t); }
n >>= 1; t = my_mul(t, t);
}
return ret;
}
ll my_mul(ll x, ll y) {
ll ret = 0, t = x;
while (y) {
if (y & 1) {
ret += t; if (ret > mod) { ret -= mod; }
}
t <<= 1; if (t > mod) { t -= mod; } y >>= 1;
}
return ret;
}
void init(void) {
memset(vis, false, sizeof(vis));
for (int i = 2; i < maxn; ++i) {
if (!vis[i]) {
prime[prime_cnt++] = i;
for (int j = i; j < maxn; j += i) { vis[j] = true; }
}
}
}
ll euler(ll x) {
ll ret = x;
for (ll i = 0; prime[i] * prime[i] <= x; ++i) {
if (x % prime[i] == 0) {
ret = ret / prime[i] * (prime[i] - 1); x /= prime[i];
while (x % prime[i] == 0) { x /= prime[i]; }
}
}
if (x != 1) { ret = ret / x * (x - 1); }
return ret;
}
2、UVa 1635 Irrelevant Elements
注意:容易格式错误
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> Pair;
const int INF = 0x7fffffff;
const int maxn = 4e4;
int prime[maxn];
bool vis[maxn];
int n, m, prime_cnt = 0;
map<int, int> key_m, m_t;
vector<int> v;
void init(void);
void get_m_factor(void);
void get_factor(int x, int y);
bool is_suit(void);
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
init();
while (scanf("%d %d", &n, &m) != EOF) {
key_m.clear(); m_t.clear(); v.clear();
get_m_factor();
n -= 1;
for (int i = 1; i <= n; ++i) {
get_factor(n - i + 1, i);
if (is_suit()) { v.push_back(i + 1); }
}
int Size = v.size();
printf("%d\n", Size);
REP(i, Size - 1) { printf("%d ", v[i]); }
if (Size - 1 >= 0) { printf("%d", v[Size - 1]); }
printf("\n");
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}
bool is_suit(void) {
map<int, int>::iterator itr;
for (itr = key_m.begin(); itr != key_m.end(); ++itr) {
int t_1 = itr->first, t_2 = itr->second;
if (t_2 > m_t[t_1]) { return false; }
}
return true;
}
void get_factor(int x, int y) {
for (int i = 0; prime[i] * prime[i] <= x; ++i) {
while (x % prime[i] == 0) {
++m_t[prime[i]]; x /= prime[i];
}
}
if (x != 1) { ++m_t[x]; }
for (int i = 0; prime[i] * prime[i] <= y; ++i) {
while (y % prime[i] == 0) {
--m_t[prime[i]]; y /= prime[i];
}
}
if (y != 1) { --m_t[y]; }
}
void get_m_factor(void) {
for (int i = 0; prime[i] * prime[i] <= m; ++i) {
while (m % prime[i] == 0) {
++key_m[prime[i]]; m /= prime[i];
}
}
if (m != 1) { ++key_m[m]; }
}
void init(void) {
memset(vis, false, sizeof(vis));
for (int i = 2; i < maxn; ++i) {
if (!vis[i]) {
prime[prime_cnt++] = i;
for (int j = i; j < maxn; j += i) {
vis[j] = true;
}
}
}
}