HDU,hdu.edu.cn
Problem DescriptionNumber theory is interesting, while this problem is boring.
Here is the problem. Given an integer sequence a1, a2, …, an, let S(i) = {j|1<=j<i, and aj is a multiple of ai}. If S(i) is not empty, let f(i) be the maximum integer in S(i); otherwise, f(i) = i. Now we define bi as af(i). Similarly, let T(i) = {j|i<j<=n, and aj is a multiple of ai}. If T(i) is not empty, let g(i) be the minimum integer in T(i); otherwise, g(i) = i. Now we define ci as ag(i). The boring sum of this sequence is defined as b1 * c1 + b2 * c2 + … + bn * cn.
Given an integer sequence, your task is to calculate its boring sum.
InputThe input contains multiple test cases.
Each case consists of two lines. The first line contains an integer n (1<=n<=100000). The second line contains n integers a1, a2, …, an (1<= ai<=100000).
The input is terminated by n = 0.
OutputOutput the answer in a line.
Sample Input
51 4 2 3 90
Sample Output
136題意:給你一個數組,讓你產生兩個新的數組,A要求每個數如果能在它的前面找個最近的一個是它倍數的數,那就變成那個數,否則是自己,C是往後找,輸出交叉相乘的和思路:掃描記錄因子處理#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef __int64 ll; const int MAXN = 100005;int a[MAXN], b[MAXN], c[MAXN], vis[MAXN];int n;int main() {while (scanf("%d", &n) == 1) {if (n == 0)break;for (int i = 1; i <= n; i++) scanf("%d", &a[i]); memset(vis, 0, sizeof(vis));for (int i = 1; i <= n; i++) {if (vis[a[i]])b[i] = a[vis[a[i]]];elseb[i] = a[i];for (int j = 1; j <= (int)sqrt((double)a[i]+0.5); j++) {if (a[i] % j == 0) {vis[j] = i;vis[a[i] / j] = i;}}}memset(vis, 0, sizeof(vis));for (int i = n; i >= 1; i--) {if (vis[a[i]])c[i] = a[vis[a[i]]];elsec[i] = a[i];for (int j = 1; j <= (int)sqrt((double)a[i]+0.5); j++) {if (a[i] % j == 0) {vis[j] = i;vis[a[i] / j] = i;}}}ll sum = 0;for (int i = 1; i <= n; i++) {sum += (ll)b[i] * c[i]; }printf("%I64d\n", sum); }return 0;}
hdu acm 2036 我覺得我沒錯
你方法有問題,我沒仔細看,是海倫公式嗎?這種方法損失精度。。。
算三角形的面積用向量的叉乘,這樣損失精度。。不知道的話,去HDOJ論壇看下LCY的課件。。
課件:(申請個號就可以了)acm.hdu.edu.cn/forum/read.php?tid=3608
我的AC代碼:
#include<math.h>
void main()
{
int n,x[150],y[150],x1,y1,x2,y2,i;
double s;
while(scanf("%d",&n)!=EOF,n)
{
s=0;
for(i=1;i<=n;i++)
scanf("%d%d",&x[i],&y[i]);
for(i=2;i<n;i++)
{
x1=x[1]-x[i];
y1=y[1]-y[i];
x2=x[1]-x[i+1];
y2=y[1]-y[i+1];
s=s+(x1*y2-y1*x2)/2.0; //叉乘計算面積。。
}
printf("%.1lf\n",s);
}
}
HDU 1002
不知道,你寫的我有點看不懂,你可以考慮看看My Code吧。AC了的
代碼:
#include<stdio.h>
#include<string.h>
int main()
{
char a[5001],b[5001];
int aa[5001],bb[5001],k,n=0;
scanf("%d",&k);
for (int c=1;c<=k;c++)
{
scanf("%s%s",a,b);
int alen=strlen(a);
int blen=strlen(b);
memset(aa,0,sizeof(aa)); //清楚aa裡的數值,讓其為0
memset(bb,0,sizeof(bb));
int maxlen=blen;
if(alen>blen) maxlen=alen;
for(int i=alen-1;i>=0;i--)
aa[alen-i]=a[i]-'0';
for(int i=blen-1;i>=0;i--)
bb[blen-i]=b[i]-'0';
for(int i=1;i<=maxlen;i++)
{
aa[i]+=bb[i];
if(aa[i]>9)
{
if(i==maxlen) maxlen++;
aa[i+1]++;
aa[i]-=10;
}
}
printf("Case %d:\n",++n);
printf("%s + %s = ",a,b);
for(int i=maxlen;i>=1;i--)
printf("%d",aa[i]);
if (k==c)
printf("\n");
else
printf("\n\n");
}
return 0;
}