总结:
做了两道题,比上次有进步。尤其是1002,团队配合特别好,一个小队写了代码,另一个小队debug出来了然后过了。
1006
题意
给出一个无向图,有n个点和m条边。第i条边的长度为2^i。每个点i有一个值ai,ai为0或1。
d(i,j)是i到j的最短距离。对所有点秋所有的可能配对形式d(i,j)*[ai1^a00]的和,最后对1e9+7取模。
思路
因为第i条边的长度为2^i,所以对于第i条边来说,如果他所连接的 两点x,y在第i条边之前那就已经被连接了,那么第i条边不会被最短路经过,因为前i-1条边的总权值为比2i小。按照每个边的权值排序建边,建树。计算每条边左右两侧各有多少值为0、1的点,值为x0,x1,y0,y1。求每条边的贡献值,计算边在最短路中出现的次数,即(x0*y1+x1*y0)*边的权值2i。
代码
#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false),cin.tie(0);
#define ll long long
#define inf 0x3f3f3f3f
const int N=2e5+10;
//set<string>b;
//set<string>::iterator it;
const int mod=1e9+7;
const int M=N<<1;
struct node
{
int u,v,w;
}e[N<<1];
struct Tree
{
int v,nxt,w;
}et[N];
int num=0,head[N],c[N],f[N],n0[N],n1[N];
vector<int>edge;
vector<int>::iterator it;
ll quick_pow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
void init(int n)
{
num=0;
edge.clear();
memset(head,-1,sizeof(head));
int i;
for(i=1;i<=n;i++) f[i]=i;
}
void add(int u,int v,int w)
{
et[num]=Tree{v,head[u],w};
head[u]=num++;
}
int find(int p)
{
return p==f[p]?p:f[p]=find(f[p]);
}
void kruskal(int m,int n)
{
int t1=0,i;
for(i=0;i<=m;i++)
{
int fu=find(e[i].u);
int fv=find(e[i].v);
if(fu==fv) continue;
f[fu]=fv;
t1++;
add(e[i].u,e[i].v,e[i].w);
add(e[i].v,e[i].u,e[i].w);
edge.push_back(e[i].w);
if(t1==n-1) break;
}
}
struct Fun
{
int c0,c1;
Fun operator+(const Fun &a) const
{
return Fun{c0+a.c0,c1+a.c1};
}
};
Fun dfs(int p,int fa)
{
Fun t=Fun{0,0};
int i;
for(i=head[p];i!=-1;i=et[i].nxt)
{
int v=et[i].v;
if(v==fa) continue;
Fun cnt=dfs(v,p);
t=t+cnt;
n0[et[i].w]=cnt.c0;
n1[et[i].w]=cnt.c1;
}
if(c[p]==0) return t+Fun{1,0};
return t+Fun{0,1};
}
int main()
{
IO;
int T,n,m,i;
ll ans;
cin>>T;
while(T--)
{
cin>>n>>m;
init(n);
int zero=0,one=0;
for(i=1;i<=n;i++)
{
cin>>c[i];
if(c[i]==0) zero++;
else one++;
}
for(i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
e[i-1]=node{x,y,i};
}
kruskal(m-1,n);
dfs(1,-1);
ans=0;
for(it=edge.begin();it!=edge.end();it++)
{
int t0=zero-n0[*it];
int t1=one-n1[*it];
ans=(ans+(t0*n1[*it]%mod)*quick_pow(2,*it)%mod)%mod;
ans=(ans+(t1*n0[*it]%mod)*quick_pow(2,*it)%mod)%mod;
}
cout<<ans<<endl;
}
return 0;
}
1002
题意
给你一个等式,让你判断这是几进制的等式[2,16],如果都不满足则输出-1。
思路
只有2到16,暴力模拟。
代码
#include<bits/stdc++.h>
#define pb(a) push_back(a)
#define ms(x,y) memset(x,y,sizeof(x))
#define sci(a) scanf("%d",&a)
#define scl(a) scanf("%lld",&a)
#define scd(a) scanf("%lf",&a)
#define ll long long
using namespace std;
bool che(char x){
if(x=='+'||x=='-'||x=='*'||x=='/'||x=='=')
return 1;
else return 0;
}
int main(){
char s[105];
while(scanf("%s",&s)!=EOF){
int n=strlen(s);
int m=0;
for(int i=0;i<n;++i){
int z=0;
if(!che(s[i])){
if(s[i]>='0'&&s[i]<='9')
z=(s[i]-'0');
else
z=(s[i]-'A'+10);
if(z>m)
m=z;
}
}
m=max(m,1);
int i;
for(i=m+1;i<17;++i){
ll ans=0,x=0,y=0,z=0,zz=0;
ll a=0;
int cc=0;
while(!che(s[cc])&&cc<n){
if(x!=0)
x*=i;
if(s[cc]>='0'&&s[cc]<='9')
z=(s[cc]-'0');
else
z=(s[cc]-'A'+10);
cc++;
x+=z;
}
char xx=s[cc++];
while(!che(s[cc])&&cc<n){
if(y!=0)
y*=i;
if(s[cc]>='0'&&s[cc]<='9')
z=(s[cc]-'0');
else
z=(s[cc]-'A'+10);
cc++;
y+=z;
}
cc++;
while(!che(s[cc])&&cc<n){
if(a!=0)
a*=i;
if(s[cc]>='0'&&s[cc]<='9')
z=(s[cc]-'0');
else
z=(s[cc]-'A'+10);
cc++;
a+=z;
}
bool f=1;
if(xx=='+') ans=x+y;
else if(xx=='-') ans=x-y;
else if(xx=='*') ans=x*y;
else if(xx=='/') {
ans=x/y;
if(x%y!=0) f=0;
}
if(a==ans&&f){
printf("%d\n",i);
break;
}
}
if(i==17)
puts("-1");
}
return 0;
}
1009
题意
给你b和x,然后让你按照题意计算f(y),然后要求如果f f(f(⋯f(y)⋯)) 这个东西%x==0,那么y%x==0,反之则都不为0。
思路
首先想到的是10进制的3,然后就想着特判一下,wa了,然后稍微想了下发现只要判断b%x是不是1就行了,因为进位这个操作可以考虑成原数字-b再在高位+1,如果b%x==1,那么结果就是不变的。
代码
#include<bits/stdc++.h>
#define pb(a) push_back(a)
#define ms(x,y) memset(x,y,sizeof(x))
#define sci(a) scanf("%d",&a)
#define scl(a) scanf("%lld",&a)
#define scd(a) scanf("%lf",&a)
#define ll long long
using namespace std;
int main(){
int te;
cin>>te;
while(te--){
ll b,x;
cin>>b>>x;
if(b%x==1)
cout<<"T\n";
else
cout<<"F\n";
}
return 0;
}
1001
题意
给出一个数列s1,s2,⋯,sns1,s2,⋯,sn,有序对(i,j)(i,j)(1≤i≤j≤n1≤i≤j≤n)的值为1j−i+1∑jk=isk1j−i+1∑k=ijsk(即si,si+1,⋯,sjsi,si+1,⋯,sj的平均值),在所有有序对中等概率随机取一个,问其值的数学期望为多少?(1≤n≤2×105,1≤si≤109,∑n≤1061≤n≤2×105,1≤si≤109,∑n≤106)
思路
为方便计算平均值,先预处理出s[i]s[i]的前缀和sum[i]sum[i];一共有(n2)+n(n2)+n个有序对,等概率取值,考虑枚举段长求和。可以O(n)O(n)预处理出所有逆元,最终时间复杂度O(n)O(n)。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 2e5 + 10;
int n, s[maxn];
LL sum[maxn], pre[maxn];
LL qpow(LL a, LL b)
{
LL res = 1;
a %= MOD;
while(b)
{
if(b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
LL inv[maxn];
void get_inv()
{
inv[1] = 1;
for(int i = 2; i <= 200000; i++)
inv[i] = (MOD - MOD / i) * inv[MOD%i] % MOD;
}
int main()
{
get_inv();
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
sum[0] = pre[0] = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d", &s[i]);
sum[i] = (sum[i-1] + s[i]) % MOD;
pre[i] = (pre[i-1] + sum[i]) % MOD;
}
LL ans = 0;
for(int i = 1; i <= n; i++)
{
LL tot = 0;
tot = (tot + pre[n] - pre[i-1]) % MOD;
tot = (tot - pre[n-i]) % MOD;
ans = (ans + tot * inv[i] % MOD) % MOD;
}
ans = (ans % MOD + MOD) % MOD;
ans = ans * qpow(1LL * n * (n - 1) % MOD * qpow(2, MOD - 2) % MOD + n, MOD - 2) % MOD;
printf("%lld\n", ans);
}
return 0;
}