8.28 吉比特 笔试
一、前言
最后一题大题看不懂,前两道模板题。前面写完还剩一个多小时,最后一题实在看不懂,提前出来了。有没有大佬最后一题做出来的分享一下思路。
二、大题
1、前缀和
给定一个长度为n的序列a和一个整数q,其中整数q表示共q次询问,每次询问给定两个整数l, r,计算表达式al - a+1 - ... - ar的值
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define P pair<int, int>
#define x first
#define y second
const int maxl = 1e5 + 7;
int n, q;
int a[maxl];
int sum[maxl];
void solve() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
int l, r;
while (q--) {
cin >> l >> r;
cout << a[l] - (sum[r] - sum[l]) << endl;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t = 1;
/*cin >> t;*/
while(t--) solve();
return 0;
}
2、快速幂
给定数字a,b,mod,求a的b次方对mod取余数。
#include<bits/stdc++.h>
using namespace std;
/*#define int long long*/
#define endl '\n'
#define P pair<int, int>
#define x first
#define y second
const int maxl = 1e6 + 7;
int a, b, mod;
int f() {
int res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b = b >> 1;
}
return res;
}
void solve() {
cin >> a >> b >> mod;
cout << f() << endl;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t = 1;
/*cin >> t;*/
while(t--) solve();
return 0;
}
#笔试##吉比特##吉比特校招#

查看8道真题和解析