B–Black and white
题解:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 5005;
struct node {
int u,v;
ll w;
bool operator < (const node& r) const {return w<r.w;}
}e[maxn*maxn];
int fa[maxn<<1];
int find(int x)
{
return fa[x] == x?x:fa[x] = find(fa[x]);
}
ll n,m,a,b,c,d,p;
int main()
{
cin>>n>>m>>a>>b>>c>>d>>p;
int cnt = 1;
e[0].w = a;
for(int i = 0; i <= n+m+2; i++) fa[i] = i;
for(int i = 1; i <= n; i++)
for(int j = n+1; j <= n+m; j++)
{
e[cnt].u = i,e[cnt].v = j;
e[cnt].w = (e[cnt-1].w*e[cnt-1].w*b+e[cnt-1].w*c+d)%p;
cnt++;
}
sort(e+1,e+1+cnt);
ll ans = 0;
for(int i=1;i<=cnt;i++){
int x = find(e[i].u);
int y = find(e[i].v);
if(x != y)
{
ans+=e[i].w;
fa[x] = y;
}
}
cout<<ans<<"\n";
return 0;
}
E–Math
题解:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
const ll inf=1e18;
int t;
vector<ll> ans;
void init(){
ans.push_back(1);
for(ll i=2;i<=1000000;i++){
__int128 x=i,y=i*i*i,k=i*i;
while(y<=inf){
ans.push_back(y);
__int128 t=y;
y=k*y-x;
x=t;
}
}
sort(ans.begin(),ans.end());
}
int main(){
init();
scanf("%d",&t);
ll x;
while(t--){
scanf("%lld",&x);
ll res=upper_bound(ans.begin(), ans.end(), x)-ans.begin();
printf("%lld\n",res);
}
}
J–Counting Triangles
题解:
#include <bits/stdc++.h>
using namespace std;
#define int long long
namespace GenHelper {
unsigned z1, z2, z3, z4, b, u;
unsigned get() {
b = ((z1 << 6) ^ z1) >> 13;
z1 = ((z1 & 4294967294U) << 18) ^ b;
b = ((z2 << 2) ^ z2) >> 27;
z2 = ((z2 & 4294967288U) << 2) ^ b;
b = ((z3 << 13) ^ z3) >> 21;
z3 = ((z3 & 4294967280U) << 7) ^ b;
b = ((z4 << 3) ^ z4) >> 12;
z4 = ((z4 & 4294967168U) << 13) ^ b;
return (z1 ^ z2 ^ z3 ^ z4);
}
bool read() {
while (!u) u = get();
bool res = u & 1;
u >>= 1;
return res;
}
void srand(int x) {
z1 = x;
z2 = (~x) ^ 0x233333333U;
z3 = x ^ 0x1234598766U;
z4 = (~x) + 51;
u = 0;
}
}
using namespace GenHelper;
bool edge[8005][8005];
int num[8005];
signed main() {
int n, seed;
cin >> n >> seed;
srand(seed);
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) edge[j][i] = edge[i][j] = read();
int res = 0, a = 0, b = 0;
for (int i = 0; i < n; i++) {
int n1 = 0, n0 = 0;
for (int j = 0; j < n; j++) {
if (i == j) continue;
if (edge[i][j])
n1++;
else if (!edge[i][j])
n0++;
}
a += n0 * (n0 - 1) / 2 + n1 * (n1 - 1) / 2;
b += n0 * n1;
}
res = (a - b / 2) / 3;
cout << res << endl;
return 0;
}