Advertisement

HDU 7100 Cut The Wire

阅读量:

Problem Description

Each streetlight x adheres to specific connection rules:

Kris stands midway between street lights numbered n and n+1, capable of severing every wire along his path.

Specifically, he will cut all wires connecting any two lights a and b where a \leq n and b > n.

Curious about the count, he seeks your assistance in figuring it out.

Each test case begins with an integer T (1 \leq T \leq 105), denoting the number of test cases to follow.

Following this are T lines, each containing an integer n within the range [1, 109].

For each test case, output a single integer representing the solution.

复制代码

2 12 60

Sample Output

复制代码

10 50

题意

给定点x,则若其编号是偶数,则连接至x/2;同样地,在这种情况下也连接至3*x + 1。
设给定一个节点编号为n,则克里斯位于节点n与节点n+1之间,并切断所有连接到节点n或节点n+1的边。
问题在于询问会被切断的所有边的数量是多少。

不管n是取到偶数值还是奇数值,在这个区间内的所有偶数值点之间的连线都将全部被切除;当且仅当n取到的是一个奇数值时,则切除这样的连线数量将等于\frac{n - \frac{n - 1}{3}}{2}条;而如果此时n是一个偶数值,则同样数量即\frac{n - \frac{n - 1}{3}}{2}条。

AC代码:

复制代码
 #include<iostream>

    
 #include<cstring>
    
 #include<cstdio>
    
 using namespace std;
    
  
    
 int n,m;
    
  
    
 int main()
    
 {
    
 	int t;
    
 	cin>>t;
    
 	while(t--)
    
 	{
    
 		cin>>n;
    
 		int res=n-n/2;
    
 		if(n%2)
    
 			res+=(n-(n-1)/3+1)/2;
    
 		else 
    
 			res+=(n-(n-1)/3)/2;
    
 		cout<<res<<endl;
    
 	}
    
 }
    
    
    
    
    AI写代码

全部评论 (0)

还没有任何评论哟~