-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.cpp
More file actions
183 lines (169 loc) · 3.38 KB
/
common.cpp
File metadata and controls
183 lines (169 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Created by 李文博 on 2018-12-20.
//
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
typedef long long ll;
int quick_pow(int base ,int m ,int mod){
int ans = 1;
while(m){
if( m & 1 ){
ans = ( base * ans ) % mod;
}
base = ( base * base) % mod;
m >>= 1;
}
if(ans < 0){
ans = mod + ans;
}
return ans;
}
long long mod_mul(long long base, long long m, long long n) {
long long res = 0;
while (m) {
if(m & 1)
res = (res + base) % n;
base = (base + base) % n;
m >>= 1;
}
return res;
}
long long quick_mod(long long base, long long m, long long mod) {
long long res = 1;
while(m) {
if(m & 1)
res = mod_mul(res, base, mod);
base = mod_mul(base, base, mod);
m >>= 1;
}
return res;
}
/*
ll exgcd(ll a,ll b,ll &x,ll &y){
if(!b){
x=1;
y=0;
return a;
}
ll d=exgcd(b,a%b,x,y);
ll tmp=x;
x=y;
y=tmp-a/b*y;
return d;
}
ll inv(ll a,ll m){
ll x,y;
ll d=exgcd(a,m,x,y);
if(d==1){
//处理负数
return (x%m+m)%m;
}
return -1;
}
*/
int anti_mod(int u, int mod){
int n1 = mod;
int n2 = u;
int b1 = 0,b2 = 1;
int q = n1 / n2;
int r = n1 - q * n2;
while(r!=0){
n1=n2;
n2=r;
int t = b2;
b2 = b1 - q * b2;
b1 = t;
q = n1 / n2;
r = n1 - q * n2;
}
if(n2 != 1)return 0x7FFFFFFF;
return quick_mod(b2,1,mod);
}
bool is_coprime(long long x, long long y){
if(x==1 || y==1)
return true;
else if(x<=0 || y<=0)
return false;
else{
int tmp=0;
while(true){
tmp = x % y;
if(!tmp)break;
else{
x = y;
y = tmp;
}
}
if(y == 1)
return true;
else
return false;
}
}
bool Miller_rabin(int n, int times){
if(n == 2 | n == 1)return true;
if(!(n & 1))return false;
int s,m,temp;
s = 0;
temp = n-1;
while ((temp & 0x1) == 0 && temp){
temp >>= 1;
s++;
}
m = temp;
srandom((unsigned)time(0));
int count = 0;
for(int i=0;i < times;i++){
int b = rand()%(n-2)+2;
int r = 0, z = quick_mod(b,m,n);
if(z == 1 | z == n-1){
count++;
continue;
}
while(z != (n-1) && r<(s-1)){
r++;
z = quick_mod(z,2,n);
}
if(z == n-1){
count++;
continue;
}
break;
}
if(count!=times)return false;
return true;
}
void txt_to_dat(string txt,string dat){
fstream ifile;
ifile.open(txt,ios::in);
fstream ofile;
ofile.open(dat,ios::out|ios::binary);
//不省略空格和换行
ifile>>noskipws;
char c;
while(!ifile.eof()){
ifile>>c;
if(ifile.eof())break;
ofile.write((char*)&c,sizeof(c));
}
ifile.close();
ofile.close();
}
void dat_to_txt(string dat,string txt){
ifstream ifile;
ifile.open(dat,ios::in|ios::binary);
ofstream ofile;
ofile.open(txt,ios::out);
char c;
while(!ifile.eof()){
ifile.read((char*)&c,sizeof(c));
if(ifile.eof())break;
ofile<<c;
}
ifile.close();
ofile.close();
}