J题:
一道结论题,比赛的时候队友想出来的,算每个点对于破坏一个三角形的贡献。
波浪线代表白色,直线代表黑色,我们发现只有当一个点它连接的两条线颜色不同的时候才能破坏一个三角形。设一个点连着a条白线,b条黑线,那么一条白线能破坏b条黑线,一条黑线也能破坏a条白线,那么一个角能破坏的三角形个数就是a * b。因为两个角会破坏同一个三角形,所以一个角破坏的三角形个数就是a * b / 2。
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<queue>
#include<bits/stdc++.h>
#define lowbit(i) ((i)&(-i))
#define ll long long
#define INF 0x3f3f3f3f
#define sf(x) scanf("%d",&x)
#define sfl(x) scanf("%lld",&x)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define debug(x) cout << #x << " = " << x << "\n"
const ll N=1e7+5;
const int M=5e3+5;
using namespace std;
typedef pair<ll, ll> P;
typedef vector<P> V;
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];
ll black[8005],white[8005];
int 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();
if(edge[i][j]) black[i]++,black[j]++;
else white[i]++,white[j]++;
}
}
ll ans=0;
for(int i=0;i<n;i++){
ans+=white[i]*black[i];
}
ll all=n;
all=all*(all-1)*(all-2)/6;
printf("%lld\n",all-ans/2);
return 0;
}
E题:
打表找规律题,比赛的时候只看出来了(x,x^3)这一对。
题目是找出能满足x * y +1能被x^2 + y ^2 整除的x和y,可得x^2 + y^2=k(xy+1),根据韦达定理可得 y1+y2=kx , 所以 (x,kx-y) 是满足条件的一组解,同理可得 (y,ky-x) 也是满足条件的一组解。
将 (x,x^3) 带入x^2 + y^2=k(xy+1),可得 k=x^2。所以当时打表得出的另外一组解就是 (x^3 , x ^ 5-x^3),直接预处理出答案二分查找就行。
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<queue>
#include<bits/stdc++.h>
#define lowbit(i) ((i)&(-i))
#define ll long long
//#define INF 0x3f3f3f3f
#define sf(x) scanf("%d",&x)
#define sfl(x) scanf("%lld",&x)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define debug(x) cout << #x << " = " << x << "\n"
const ll N=1e18+5;
const int M=5e6+10;
const ll INF=9e18;
using namespace std;
typedef pair<ll, ll> P;
typedef vector<P> V;
ll a[M],cnt;
void init(){
for(ll i=2;i*i*i<=1e18;i++){
// printf("i=%lld cnt=%lld\n",i,cnt);
ll x=i,y=i*i*i,k=i*i;
a[++cnt]=y;
while(y<=(1e18+x)/k){
x=k*y-x;
swap(x,y);
a[++cnt]=y;
}
}
}
int main(){
int t;
sf(t);
init();
// printf("123\n");
sort(a+1,a+cnt+1);
while(t--){
ll n;
sfl(n);
ll x=upper_bound(a+1,a+cnt+1,n)-a;
pfl(x);
}
return 0;
}
B题:
居然是一道图论题,比赛的时候一直还以为是DP,把行和列看成点,让行和列连起来就是满足条件的图。比如让(3,4)连起来,就是选择(3,4)这个点。
一共要选择n+m-1个点就可以让图连通,就是最小生成树。剩下的点就可以免费了,因为行只能连向列,所以在最小生成树更新点的时候注意行只能更新列,列也只能更新行。
我这里用的是prim,kruskal好像也能写。
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<queue>
#include<bits/stdc++.h>
#define lowbit(i) ((i)&(-i))
#define ll long long
#define INF 0x3f3f3f3f
#define sf(x) scanf("%d",&x)
#define sfl(x) scanf("%lld",&x)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define debug(x) cout << #x << " = " << x << "\n"
const ll N=5e3+5;
const int M=5e3+5;
using namespace std;
typedef pair<ll, ll> P;
typedef vector<P> V;
int g[N+M][M+N],dis[N+M];
bool st[N+M];
int prim(int n,int m){
for(int i=1;i<=n+m;i++) dis[i]=INF;
dis[1]=0;
ll ans=0;
for(int i=0;i<n+m;i++){
int t=-1;
for(int j=1;j<=n+m;j++){
if(!st[j]&&(t==-1||dis[j]<dis[t])) t=j;
}
st[t]=true;
// printf("dis[%d]=%d\n",t,dis[t]);
ans=ans+dis[t];
if(t<=n){
for(int j=1;j<=m;j++){
dis[n+j]=min(dis[n+j],g[t][j]);
// printf("dis[%d]=%d\n",n+j,dis[n+j]);
}
}
else{
for(int j=1;j<=n;j++){
dis[j]=min(dis[j],g[j][t-n]);
// printf("dis[%d]=%d\n",j,dis[j]);
}
}
}
return ans;
}
int main(){
int n,m,b,c,d,p;
ll a;
scanf("%d%d%lld%d%d%d%d",&n,&m,&a,&b,&c,&d,&p);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a=(a*a*b%p+a*c%p+d%p)%p;
g[i][j]=a;
}
}
ll ans=prim(n,m);
pfl(ans);
return 0;
}