When repairing an electronic board, Basil discovered that the board had already been under repair, and one of the resistors on it was replaced with a strange design. The design consisted of k series of connected links, and each link in them — of two resistors connected in parallel.
Moreover, in each link, along with the typical resistor, with a clearly readable denomination, there was a resistor with a strange, non-standard marking. Having examined the non-standard resistors, Basil came to the conclusion that they were of the same type. However, he could not determine their value. The total resistance value of the entire circuit was equal to R ohms. Having written out the nominal value of the known resistor for each link in the circuit, Basil received a series of integers r1, r2, …, rk.
Help Basil — write a program that calculates the value of the mysterious resistors from the total resistance of the circuit R, and the known values r1, r2, …, rk.
Input:
The first line of the input data contains two integers k (1≤k≤1000) and R (1≤R≤100000), separated by a space.
The second line contains k integers separated by spaces: r1, r2,…, rk (1≤ri≤100000; 2R≤r1+r2+…+rk).
Output:
The program should output a single number — the estimated value of the mysterious resistors. The value must be outputed with an accuracy of 10−6.
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int n,R,r[10005];
double cnt(double k)
{
double sum=0;
for(int i=1; i<=n; i++)
{
sum+=(r[i]*k)/(r[i]+k);
}
return sum;
}
double f(double l,double r)
{
double mid=(l+r)/2;
double ans=cnt(mid);
if(abs(ans-R)<0.0000001)
{
return mid;
}
if(ans>R)
return f(l,mid);
else
return f(mid,r);
}
int main()
{
cin>>n>>R;
for(int i=1; i<=n; i++)
cin>>r[i];
printf("%.8lf\n",f(0,100000));
return 0;
}
—————————————————————————————————
题目链接
While analyzing a mathematical problem, a programmer, Basil by name, noticed an interesting fact: for the numbers 2 and 4 holds the equality 24=42. ‘This could be a great challenge for the participants of the programming championship,’ he thought. Unfortunately, Basil could not find other such pairs of numbers, the pair 2 and 4 was his only combination that he could think of. ‘Okay, then we’ll change the conditions, let there be three numbers,’ Basil decided. The written program of enumeration options confirmed that with three numbers the task made sense.
Your task: find the integer x by the given integers a and b such that falls in the range from 1 to 1018 (Basil’s program didn’t deal with greater numbers) such that ax=xb. If there are several such numbers, type the smaller of them. If such a number does not exist, type 0.
Input:
The input data consist of two integers a and b (2≤a,b≤10000; a≠b), separated by a blank space.
Output:
The integer x if there is a solution, or 0 (zero) if there is no solution.
#include <iostream>
#include <cmath>
#include <cstdio>
#define ll long long
using namespace std;
ll a,b;
ll f(ll l,ll r)
{
if(l>r)
return 0;
ll mid=(l+r)/2;
double x=(double)(log(mid)/log(a));
if(abs(x*b-mid)<0.00000000001)
{
ll ans = f(1,mid-1);
if(ans)
return ans;
return mid;
}
if(x*b>mid)
return f(mid+1,r);
else
return f(l,mid-1);
}
int main()
{
cin>>a>>b;
printf("%lld\n",f(1,1e18));
return 0;
}
—————————————————————————————————
题目链接
在一个二维平面上,有一个顶点分别在 (x0, y0), (x0 + a, y0), (x0, y0 + b) 三点上的三角形 A。
请给出一条平行于 y 轴的直线 L: x = c,使得三角形 A 被直线 L 分成面积相等的两半。
正式地讲,请给出一个实数 c,使得三角形 A 中,坐标小于或等于 c 的点所组成图形的面积 SLeft,与坐标大于 c 的点所组成图形的面积 SRight 相等。
Input:
一行,四个整数 x0, y0, a, b ( - 1, 000 ≤ x0, y0 ≤ 1, 000, 1 ≤ a, b ≤ 1, 000)。
表示三角形 A 三个顶点 (x0, y0), (x0 + a, y0), (x0, y0 + b) 所对应的参数。
Output:
一个实数 c,需满足 x0 ≤ c ≤ x0 + a,表示一条直线 L: x = c,可将三角形 A 分成面积相等的两半。
但你不需要给出该实数的精确表示,只需要输出一个浮点数,使得它与标准答案的绝对误差或对标准答案的相对误差,不超过 10 - 4,即认为你给出的答案正确。
正式地讲,记你给出的浮点数答案是 c,标准答案为 d,当 ,则认为你给出的答案正确。
#include <iostream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define ll long long
using namespace std;
const int maxn = 1e5+10;
double x,y,a,b;
double area;
double find(double l,double r)
{
double mid = (l+r)*1.0/2;
double h = ((x+a-mid)*b)/a;
double S = (h*(x+a-mid))/2;
if(abs(S-area)<0.00000000001)
{
return mid;
}
if(S>area)
return find(mid,r);
else
return find(l,mid);
}
int main()
{
scanf("%lf%lf%lf%lf",&x,&y,&a,&b);
area = a*b/2;
area/=2;
double ans = find(x,x+a);
// double l=x,r=x+a;
// while (r-l>0.000000001)
// {
// double mid = (l+r)/2;
// double h = b*(a+x-mid)/a;
// double right = (x+a-mid)*h/2;
// if(right>=area)
// l=mid;
// else
// r=mid;
// }
// printf("%.10lf\n",l);
printf("%.10lf\n",ans);
return 0;
}
—————————————————————————————————
奇怪的奇数
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define ll long long
#define lowbit(x) ((~x+1)&x)
using namespace std;
ll n;
int main() {
cin>>n;
ll l=1,r=1e9+10;
ll ans;
while(l<=r) {
ll mid = (l+r)>>1;
if(mid*log10(mid)+1>=n) {
ans = mid;
r=mid-1;
}
else l=mid+1;
}
cout<<ans<<endl;
return 0;
}