刚打完,过了八题,先扔个代码。等pta上了之后写题解。
A
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e3 + 10;
int a[N][N], b[N][N];
int tot, pr[N], loop[N];
inline void solve(){
int n; cin>>n;
int m=min(n,100ll);
for(int i=1;i<=m;i++)
{
for(int j=1;j<=i;j++)
{
cin>>a[i][j];
}
}
int q; cin>>q;
while(q--)
{
int x; cin>>x;
if(n<=100)
{
int ans=0;
for(int i=1;i<=n;i++)
{
ans=(ans*10+a[n][i])%x;
}
cout<<ans<<"\n";
}else
{
int k=loop[x];
int ans=0;
for(int i=1;i<=k;i++)
{
int dd=n-i+1;
dd=(dd-1+k)%k+1;
(ans+=a[k][i]*b[x][dd]%x)%=x;
}
cout<<ans<<"\n";
}
}
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1;
for(int i=3;i<=100;i++)
{
if(i==5) continue;
int fg=0;
for(int j=2;j*j<=i;j++)
{
if(i%j==0)
{
fg=1;
break;
}
}
if(!fg) pr[++tot]=i;
}
for(int i=1;i<=tot;i++)
{
int cnt=1,base=10%pr[i];
b[pr[i]][cnt]=1;
while(base!=1)
{
cnt++;
b[pr[i]][cnt]=base;
base=base*10%pr[i];
}
loop[pr[i]]=cnt;
}
cin >> t;
while(t--) solve();
return 0;
}
B
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 10;
int a[N];
int d[1005],sum[20005],sum1[200005];
int dp[105][105];
int val[105][105];
inline void solve(){
int n = 0; cin >> n;
for(int i =1;i<=n;i++){
cin>>a[i];
}
int k=n-1;
for(int i=1;i<n;i++){
d[i]=a[i+1]-a[i];
sum[i]=sum[i-1]+d[i];
sum1[i]=sum1[i-1]+d[i]*d[i];
}
for(int l=1;l<=k;l++){
for(int r=l;r<=k;r++){
val[l][r]=(sum[r]-sum[l-1])*(sum[r]-sum[l-1]);
}
}
for(int i=1;i<=k;i++){
dp[i][0]=sum1[i];
dp[i][i-1]=val[1][i];
for(int j=1;j<i;j++){
for(int len=0,t=i;len<=j;len++,t--){
dp[i][j]=max(dp[i][j],dp[t-1][j-len]+val[t][i]);
}
}
}
for(int i=1;i<=n;i++){
if(i*2>=k){
cout<<dp[n-1][k-1]<<endl;
}
else{
cout<<dp[n-1][i*2]<<endl;
}
}
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; // cin >> t;
while(t--) solve();
return 0;
}
E
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
inline void solve(){
int n, k; cin >> n >> k;
int sum = 0, cur = k;
for(int i = 2; i <= n; i++){
int fnd = 2;
for(int j = 2; j < cur; j++){
if(__gcd(j, cur) == 1){
fnd = j;
break;
}
}
if(fnd == 2){
int left = (n - i + 1);
if(left & 1) sum += 5 * (left / 2) + 2;
else sum += 5 * (left / 2);
break;
} else{
sum += fnd;
}
cur = fnd;
}
cout << sum + k << endl;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout << fixed << setprecision(12);
int t = 1; // cin >> t;
while(t--) solve();
return 0;
}
F
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 10;
__int128 sum[N];
map<int, int> fa1, fa2;
inline void solve(){
fa1.clear(), fa2.clear();
int k, x, y; cin >> k >> x >> y;
sum[1] = 1;
for(int i = 2; sum[i - 1] <= max(x, y); i++) sum[i] = sum[i - 1] * (k + 1);
int pos = 0;
for(int i = 1; sum[i - 1] <= max(x, y); i++){
if(sum[i] >= x){
pos = i - 1;
break;
}
}
int dep1 = 0, dep2 = 0;
int nfa = x;
while(nfa != 1){
int now = nfa;
fa1[now] = ((now - sum[pos]) + (k - 1)) / k;
nfa = fa1[now];
while(sum[pos] >= nfa) pos--;
dep1++;
}
pos = 0;
for(int i = 1; sum[i - 1] <= max(x, y); i++){
if(sum[i] >= y){
pos = i - 1;
break;
}
}
nfa = y;
while(nfa != 1){
int now = nfa;
fa2[now] = ((now - sum[pos]) + (k - 1)) / k;
nfa = fa2[now];
while(sum[pos] >= nfa) pos--;
dep2++;
}
if(dep1 < dep2) while(dep1 < dep2) y = fa2[y], dep2--;
if(dep1 > dep2) while(dep1 > dep2) x = fa1[x], dep1--;
if(x == y) {
cout << x << endl;
return;
}
while(fa1[x] != fa2[y]) x = fa1[x], y = fa2[y];
cout << fa1[x] << endl;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}
G
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
#define pii pair<int,int>
using namespace std;
const int N = 1e5 + 10, mod = 1000000007;
int qpow(int x,int y=mod-2,int res=1){
for(;y;y>>=1,(x*=x)%=mod) if(y&1)(res*=x)%=mod;
return res;
}
int jie[1000005],invjie[1000005];
int C(int n,int m){
return jie[n]*invjie[m]%mod*invjie[n-m]%mod;
}
pii b[1000005];
struct node{
int l,r;
bool operator<(const node &t)const {
return l<t.l||l==t.l&&r>t.r;
}
}a[1000005];
int fa[1000005];
int n,m,fg;
vector<int>p[1000005];
int dp[1000005],l[1000005];
void main_init(int n){
jie[0]=1;
for(int i=1;i<=n;i++){
jie[i]=jie[i-1]*i%mod;
}
invjie[n]=qpow(jie[n]);
for(int i=n-1;~i;i--){
invjie[i]=invjie[i+1]*(i+1)%mod;
}
}
void dfs(int u){
int res=1;
l[u]=a[u].r-a[u].l+1;
int len=l[u];
for(auto v:p[u]){
dfs(v);
(res*=dp[v])%=mod;
len-=l[v];
}
len+=p[u].size();
dp[u]=jie[len]*res%mod;
}
inline void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a[i].l>>a[i].r;
if(a[i].l==1&&a[i].r==n)fg=1;
if(a[i].l==a[i].r){i--;m--;}
}
if(!fg){
m++;
a[m].l=1;a[m].r=n;
}
sort(a+1,a+1+m);
for(int i=2,nowfa=1;i<=m;i++){
while(a[nowfa].r<a[i].l){
nowfa=fa[nowfa];
}
fa[i]=nowfa;
p[fa[i]].push_back(i);
nowfa=i;
}
dfs(1);
cout<<dp[1]<<endl;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; //cin >> t;
main_init(1000000);
while(t--) solve();
return 0;
}
J
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
int a[N];
map<pii,int>mp;
inline void solve(){
int n = 0, k = 0; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
int szl=0,szr=0;
for(int i=1;i<=n;i++){
if(a[i]>a[i-1]){
szl++;
}
else break;
}
for(int i=n;i;i--){
if(a[i]>a[i+1]){
szr++;
}
else break;
}
int cnt=0;
for(int l=1,r=n;l<=szl&&(n-r+1)<=szr;){
if(a[l]==a[r]){
if((szl-l+1)%2||(szr-(n-r+1))%2){
cnt++;
}
break;
}
if(a[l]<a[r]){
if((szr-(n-r))%2){
cnt++;
break;
}
else{
l++;
cnt++;
}
}
else if(a[l]>a[r]){
if((szl-l+1)%2){
cnt++;
break;
}
else{
r--;
cnt++;
}
}
}
if(cnt%2){
cout<<"Alice";
}
else{
cout<<"Bob";
}
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout << fixed << setprecision(12);
int t = 1; // cin >> t;
while(t--) solve();
return 0;
}
K
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;
const int mod = 998244353;
const int N = 1e5 + 10;
int qpow(int x,int y=mod-2,int res=1){
for(;y;y>>=1,(x*=x)%=mod) if(y&1)(res*=x)%=mod;
return res;
}
int p2=qpow(2,mod-2),p3=qpow(3,mod-2);
int n;
int mp[300][300][5];
int ansl,ansr;
inline void solve(){
cin>>n;
for(int i=1;i<=n;i++){
int op,x,y;cin>>op>>x>>y;
x+=102;y+=102;
if(op==1){
for(int j=0;j<4;j++){
mp[x][y][j]=1;
mp[x-1][y][j]=1;
mp[x][y-1][j]=1;
mp[x-1][y-1][j]=1;
}
}
else{
mp[x][y][2]=1;mp[x][y][3]=1;
mp[x][y-1][0]=1;mp[x][y-1][3]=1;
mp[x-1][y][2]=1;mp[x-1][y][1]=1;
mp[x-1][y-1][0]=1;mp[x-1][y-1][1]=1;
}
}
for(int i=1;i<=205;i++){
for(int j=1;j<=205;j++){
int res=0;
for(int k=0;k<4;k++){
res+=mp[i][j][k];
}
if(mp[i][j][0]){
if(mp[i][j+1][2]==0){
ansl++;
}
}
if(mp[i][j][1]){
if(mp[i+1][j][3]==0){
ansl++;
}
}
if(mp[i][j][2]){
if(mp[i][j-1][0]==0){
ansl++;
}
}
if(mp[i][j][3]){
if(mp[i-1][j][1]==0){
ansl++;
}
}
if(res==3){
(ansr+=p3)%=mod;
}
else if(res==2){
(ansr+=p2)%=mod;
}
}
}
cout<<ansl<<" "<<ansr<<endl;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
L
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
#define pii pair<int,int>
using namespace std;
const int mod =998244353;
const int N = 2e6 + 10;
string s;
int x, a, b, p;
int prec[N],prep[N],prei[N];
int precp[N],prepc[N],preic[N],preicp[N];
int precpc[N];
int preicpc[N];
int askl[N],askr[N];
int ans,sum;
inline void solve(){
int n, q; cin >> n >> q >> s;
cin >> x >> a >>b >>p;
s=" "+s;
for(int i=1;i<=n;i++){
prei[i]=prei[i-1]+(s[i]=='I');
prec[i]=prec[i-1]+(s[i]=='C');
prep[i]=prep[i-1]+(s[i]=='P');
preic[i]=(preic[i-1]+prei[i-1]*(s[i]=='C'))%mod;
prepc[i]=(prepc[i-1]+prep[i-1]*(s[i]=='C'))%mod;
precp[i]=(precp[i-1]+prec[i-1]*(s[i]=='P'))%mod;
preicp[i]=(preicp[i-1]+preic[i-1]*(s[i]=='P'))%mod;
preicpc[i]=(preicpc[i-1]+preicp[i-1]*(s[i]=='C'))%mod;
precpc[i]=(precpc[i-1]+precp[i-1]*(s[i]=='C'))%mod;
}
for(int i = 1; i <= q; i++){
x=(x*a+b)%p;
askl[i]=x%n;
}
for(int i = 1; i <= q; i++){
x=(x*a+b)%p;
askr[i]=x%n;
if(askl[i]>askr[i])swap(askl[i],askr[i]);
askl[i]++;askr[i]++;
}
for(int i=1;i<=q;i++){
int l=askl[i],r=askr[i];
int lrc=prec[r]-prec[l-1],lrpc=(prepc[r]-prepc[l-1]-prep[l-1]*lrc%mod+mod+mod)%mod;
ans=(preicpc[r]-preicpc[l-1]-preicp[l-1]*lrc%mod+mod+mod)%mod;
ans=(ans-preic[l-1]*lrpc%mod+mod)%mod;
int lrcpc=(precpc[r]-precpc[l-1]+mod-precp[l-1]*lrc%mod+mod-prec[l-1]*lrpc%mod+mod)%mod;
ans=(ans-prei[l-1]*lrcpc%mod+mod)%mod;
(sum+=ans)%=mod;
}
cout<<sum;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}