#include<bits/stdc++.h>using namespace std;
typedef vector<vector<int>> Mat;
int N;
Mat operator * (Mat A, Mat B){
int m = A.size(), k = A[0].size(), n = B[0].size();
Mat C(m, vector<int>(n, 0));
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++){
long long sum = 0;
for (int p = 0; p < k; p++)
sum = (sum + 1ll * A[i][p] * B[p][j]) % N;
C[i][j] = (sum + N) % N;
}
return C;
}
Mat qpow(Mat A, long long k){
int n = A.size();
if (k == 0) {
Mat res(n, vector<int> (n, 0));
for (int i = 0; i < n; i++) res[i][i] = 1;
return res;
}
Mat tmp = qpow(A, k / 2);
if (k % 2 == 0) return tmp * tmp;
else return tmp * tmp * A;
}
Mat read(int n){
int r, c;
r = c = n;
Mat A(r, vector<int>(c));
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
cin >> A[i][j];
return A;
}
void write(Mat A) {
for (auto row : A) {
for (auto x : row) cout << x << " ";
cout << '\n';
}
}
int main(){
int k, b;
cin >> N >> k >> b;
Mat A = {{1, 0 ,0}, {1, k + 1, k}, {0, 1, 1}};
int q;
cin >> q;
while(q--){
int s, v; long long t;
cin >> s >> v >> t;
Mat B = {{b}, {v}, {s}};
Mat res = qpow(A, t) * B;
cout << res[2][0] << endl;
}
return 0;
}
D. 互质统计
ϕ(n) 为1∼n 内与 n 互质的数的个数
现在要统计 a 与 b 互质的情况有多少对,如果 (a,b) 互质,(b,a) 也互质。
先考虑 a≤b 的情况
a=b 时,gcd(a,a)=a ,只有当 a=1 时 gcd=1,只有(1,1) 这一对
a<b 时,固定 b,互质的 a 个数为 ϕ(b),所以 a<b 互质对总数为 ∑b=2nϕ(b)
a>b 的情况,利用对称性,和 a<b 的情况一样,是 ∑b=2nϕ(b)
因为 ϕ(1)=1,所以答案为 2(∑b=1nϕ(b))−1
C++
#include<bits/stdc++.h>using namespace std;
const int maxn = 1e7 + 10;
vector<int> primes;
bool vis[maxn];
int phi[maxn];
long long sum_phi[maxn];
void get_prime(){
vis[1] = 1;
phi[1] = 1;
sum_phi[1] = 1;
for (int i = 2; i < maxn; i++){
if (!vis[i]){
primes.push_back(i);
phi[i] = i - 1;
}
for (int p : primes){
if (1LL * i * p > maxn) break;
vis[i * p] = true;
if (i % p == 0) {
phi[i * p] = phi[i] * p; break;
}
else phi[i * p] = phi[i] * (p - 1);
}
sum_phi[i] = sum_phi[i - 1] + phi[i];
}
}
int main(){
int T;
long long n;
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
get_prime();
cin >> T;
while(T--){
cin >> n;
cout << sum_phi[n] * 2 - 1 << endl;
}
return 0;
}
#include<bits/stdc++.h>using namespace std;
void ex_gcd(int a, int b, int &d, long long &x ,long long & y){
if (b == 0){
d = a;
x = 1; y = 0;
return;
}
long long xx, yy;
ex_gcd(b, a % b, d, xx, yy);
x = yy;
y = xx - 1LL * (a / b) * yy;
}
int main(){
int T;
int w, h, x, y;
long long xx, yy;
cin >> T;
while(T--){
cin >> w >> h >> x >> y;
xx = x; yy = y;
w /= 2;
long long p, q;
int d;
ex_gcd(w, h, d, p, q);
x = x % w; if (x > 0) x = w - x;
y = y % h; if (y > 0) y = h - y;
p = p * (y - x) / d;
long long lcm = 1LL * w * h / d;
long long ans = (p * w + x) % lcm;
if (ans < 0) ans += lcm;
xx = (xx + ans) % (w * 4);
if (xx == w || xx == 3 * w)
cout << w << ' ';
else if (xx == 2 * w)
cout << w * 2 << ' ';
else
cout << 0 << ' ';
yy = (yy + ans) % (h * 2);
if (yy == h)
cout << h << endl;
else
cout << 0 << endl;
}
return 0;
}