记录两三道题目吧,顺便再补上java代码,好好搞一下高精度的问题。
here
I HDU 5050 Divided Land
求一个GCD,输入的是二进制,输出的是二进制,存在一点问题就是输入的数值可能非常的大,c++的lld是处理的不了的,需要用到高精度。
这里是用java代码写的,重铸java荣光,我辈义不容辞 ☺
ac code
package ac;
import java.util.Scanner;
import java.math.BigInteger;
public class acgo {
public static void main(String args[])
{
Scanner cin =new Scanner(System.in);
int n = cin.nextInt();
for(int i=1;i<=n;i++)
{
Scanner sc =new Scanner(System.in);
String s=sc.next();
String s2=sc.next();
BigInteger w2 = new BigInteger(s2,2);
BigInteger w=new BigInteger(s,2);
System.out.print("Case #"+i+": ");
System.out.println(w.gcd(w2).toString(2));
}
}
}
F - Sawtooth HDU - 5047
这一道题,给你一个平面,你可以用M这个字符将平面分成两份。
两个M可以将平面最多分成19块。
比问直线划分线段稍微复杂一些。
但是问题不大,俗话说的好,极限思想击碎一切规律题!!!
观察M发现他们最多分块发生在M的四条边和另一个M的四条边相交。
那就好办了,我直接把M看成四条重合直线,极限化,再加上他的下边会新增加的一块。规律就有了
f
(
0
)
=
1
f(0)=1
f(0)=1
f
(
n
)
=
16
∗
(
n
−
1
)
+
1
+
f
(
n
−
1
)
f(n)=16*(n-1)+1+f(n-1)
f(n)=16∗(n−1)+1+f(n−1)
这里为什么是16呢,因为
4
∗
(
n
−
1
)
4*(n-1)
4∗(n−1)是之前累积的边数 乘以
4
4
4就是我现在新增加的数量,不要忘记了1.
直接归纳到
f
(
n
)
=
8
∗
n
2
−
7
∗
n
+
1
f(n)=8*n^2-7*n+1
f(n)=8∗n2−7∗n+1 但是你会发现这样还不够,因为数量会非常的大远超lld的范围。
好了 上java code
package ac;
import java.util.Scanner;
import java.math.BigInteger;
public class acgo {
public static void main(String args[])
{
Scanner cin =new Scanner(System.in);
int n = cin.nextInt();
int i=1;
BigInteger e=new BigInteger("8");
BigInteger o=new BigInteger("1");
BigInteger se=new BigInteger("-7");
while(i<=n)
{
BigInteger z=new BigInteger("0");
String s=cin.next();
BigInteger w = new BigInteger(s,10);
System.out.print("Case #"+i+": ");
BigInteger t1 = w.multiply(w);
t1=t1.multiply(e);
z=z.add(t1);
w = w.multiply(se);
z=z.add(o);
z=z.add(w);
System.out.println(z);
i++;
}
}
}
J - Fraction HDU - 5051
一道极限题目,巧合之下推出了福特定律。当时连WA 10+发 不愧是while WA er
这是一个什么东西呢?
这个也就意味着当数值特别大的时候b和q的值是没有意义的,除了q=10的幂次方的情况。
后面就不细说了吧。没有什么意义,也不是java写的。给代码好了
#include <bits/stdc++.h>
using namespace std;
//double pn[] = {0,0.301032,0.176089,0.124943,0.0969039,0.0791853,0.0669467,0.0579962,0.0511462,0.0457576};
//double pn[] = {0,0.30103, 0.17608, 0.12494, 0.09690, 0.07918, 0.06694, 0.05799, 0.05114, 0.04575};
void solve()
{
int t;
cin>>t;
int n,b,q;
for(int i=1;i<=t;i++)
{
cin>>n>>b>>q;
if(q==1)
{
if(n==b||n==b/10||n==b/100||n==b/1000) printf("Case #%d: 1.00000\n",i);
else printf("Case #%d: 0.00000\n",i);
}
if(q==10||q==100||q==1000)
{
if(n==b*10||n==b*100||n==b*1000||n==b||n==b/10||n==b/100||n==b/1000) printf("Case #%d: 1.00000\n",i);
else printf("Case #%d: 0.00000\n",i);
}
else
{
double temp = (double)(n+1)/(double)n;
double ans = log(temp)/log(10);
//cout<<ans<<endl;
printf("Case #%d: %.5f\n",i,ans);
}
//
}
}
/*
5
2 2 1000
2 4 1000
1 3 101
2 2 101
*/
signed main()
{
int T=1;
//cin>>T;
for(int index=1;index<=T;index++)
{
solve();
}
return 0;
}