The North American Invitational Programming Contest 2016 I.Tourists(LCA求树上任意两点距离+埃式筛法)

思路来源

https://www.cnblogs.com/acjiumeng/p/7249890.html

题意

给定一棵树,求

\sum dis(i,j),1<=i<j<=n,i|j

dis(i,j)定义为树上两点距离+1,n≤2e5

题解

dfs预处理,求根节点到每一点的距离,即点i的深度depth[i]。

这里默认1号点是根节点。

然后在线二分搜索+倍增求LCA,求i和j的最近公共祖先z。

即使树退化成链表,倍增也能logn解决。

i和j的距离就是depth[i]+depth[j]-2*depth[z],

即i到z的距离+j到z的距离。

然后埃氏筛法一波就A了。

代码

  #include<stdio.h>
  #include<string.h>
  #include<algorithm>
  using namespace std;
  typedef long long ll;
  ll cnt,head[200010];
  struct edge{ll to,nex;}e[400010];
  ll parent[200010][30],depth[200010];
  ll ans;
  void init()
  {
	memset(head,-1,sizeof(head));
	cnt=0;
  }
  void add(ll u,ll v)
  {
	e[cnt].to=v;
	e[cnt].nex=head[u];
	head[u]=cnt++;
  } 
 void dfs(ll u,ll pre,ll deep)
 {
   parent[u][0]=pre;
   depth[u]=deep;
   for (int i=head[u];~i;i=e[i].nex)
   {
   	 ll v=e[i].to;
     if(v==pre) continue;
     dfs(v,u,deep+1);
   }
 }
 void double_build(ll n)
 {
   int i,j; 
   for (i=0;i+1<=25;i++)//路径倍增
     for (j=1;j<=n;j++)
       if (parent[j][i]<0) parent[j][i+1]=-1;
       else parent[j][i+1]=parent[parent[j][i]][i];
 }
 ll double_query(ll u,ll v)
 {
   int i;
   if (depth[u]>depth[v]) swap(u,v);
   for (i=0;i<=25;i++)
     if ((depth[v]-depth[u])>>i&1)
       v=parent[v][i];
   if (u==v) return u;
   for (i=25;i>=0;i--) //二进制拆分思想达到倍增
     if (parent[u][i]!=parent[v][i])
     {
       u=parent[u][i];
       v=parent[v][i];
     }
   return parent[u][0];
 }
 int main()
 {
     ll x,y,n;
     scanf("%lld",&n);
     init();
     for(int i=1;i<n;i++)
     {
       scanf("%lld%lld",&x,&y);
       add(x,y);
	   add(y,x);
     }
     dfs(1,-1,1);
     double_build(n);
     for(int i=1;i<=n;++i)
     {
     	for(int j=2*i;j<=n;j+=i)
     	{ 
         ll z=double_query(i,j);
         ans+=1+depth[i]+depth[j]-2*(depth[z]);//距离计算
         //printf("%d->%d=%lld\n",i,j,1+depth[i]+depth[j]-2*(depth[z]));
        } 
     }
     printf("%lld\n",ans);
     return 0;
 }

 

### 2024 AIME Benchmark for Large Models #### Overview of AIME as a Benchmark The American Invitational Mathematics Examination (AIME) has been adapted into a benchmark dataset specifically designed to evaluate the performance of large language models (LLMs). This adaptation allows researchers to assess how well these models handle complex mathematical reasoning tasks. The primary metric used in this context is **Pass@1**, which measures the proportion of problems solved correctly by the model on its first attempt[^2]. #### Performance Metrics and Evaluation Criteria In evaluating LLMs using the AIME 2024 dataset, several key aspects are considered: - **Strict Accuracy Measurement**: Pass@1 serves as an indicator of direct correctness without retries or iterative refinement. - **Error Analysis Insights**: An analysis revealed common sources of errors include difficulties in extracting necessary concepts and computational mistakes. Specifically, out of 80 evaluated questions, concept extraction issues accounted for 34 cases while calculation errors were present in 30 instances[^4]. #### Importance of Curated Datasets To enhance the effectiveness of benchmarks such as AIME 2024, it becomes crucial to utilize carefully curated small-scale datasets rather than relying solely on randomly selected samples or focusing merely on sample diversity and length. Such high-quality data significantly boosts the inference capabilities of LLMs when tackling challenging math problems[^3]. ```python def calculate_pass_at_one(questions_solved_correctly_first_attempt, total_questions): """ Calculate the Pass@1 score given the number of questions answered correctly at first try. Args: questions_solved_correctly_first_attempt (int): Number of questions solved correctly initially. total_questions (int): Total number of questions attempted. Returns: float: Percentage representing the Pass@1 accuracy rate. """ pass_rate = (questions_solved_correctly_first_attempt / total_questions) * 100 return round(pass_rate, 2) # Example usage correct_answers_on_first_try = 75 total_attempts = 100 print(f"Pass@1 Score: {calculate_pass_at_one(correct_answers_on_first_try, total_attempts)}%") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小衣同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值