UVA - 1393 Highways (和紫书不一样的解法)

题目链接

题解:

由于我的方法和紫书不一样,所以专门写一下记录一下解题思路。

显然“\”和“/”两个方向的直线数量是相同的,所以我们考虑计算“/”方向的线的数量。

统计类问题,都需要找到合适的分类方法,使我们能够不重复、不遗漏的统计出所有情况。

本题我们以直线的起点分类,把直线分类,来进行统计。

对于点(i,j),其右上角有(i-1)*(j-1)个点,总共可以连(i-1)*(j-1)条直线,其中某些直线重复出现,假如我们计算出只出现了一次的直线有多少条记为x_{i,j}。那么所有点的x的和,即\sum x_{i,j} (1<=i<=n,1<=j<=m),就是我们的答案。

很容易证明,对于每条直线,我们只会在最右上角那一段计算一次,所以不会有重复,我们计算了所有起点,所以没有遗漏。

对于每一个点(i,j),如何计算x_{i,j} ?

我们设点(i,j)为坐标源点,设其右上角的点坐标为(a,b) (1<=a<=i-1,1<=b<=j-1)

我们要直线(0,0)-(a,b)只出现一次,那么(a,b)一定要同时满足以下条件:

1,GCD(a,b)==1

2,2*a>i-1 或 2*b>j-1

如何计算满足条件的(a,b)有多少对呢?

我们设f_{i,j}为有多少对(a,b) (1<=a<=i,1<=b<=j)互质?即GCD(a,b)==1

那么f_{i-1,j-1}-f_{(i-1)/2,(j-1)/2} 就是要求解的答案。

如何计算f_{i,j}呢?

我们可以递推计算 f_{i,j}=f_{i-1,j}+v_{i,j}v_{i,j}表示数字1到i有多少个数和j互素。

我们可以暴力预处理出所有f_{i,j}

复杂度

预处理f_{i,j}的复杂度O(n^2)

每次询问计算的复杂度O(n^2)

所以复杂度为O(n^2)

代码如下:

#include<bits/stdc++.h>

using namespace std;
const int nn =11000000;
const int inff = 0x3fffffff;
const double eps = 1e-8;
typedef long long LL;
const double pi = acos(-1.0);
const LL mod = 1000000007;
int f[310][310];
int GCD(int a,int b)
{
    if(b==0)
        return a;
    return GCD(b,a%b);
}
void init()
{
    memset(f,0,sizeof(f));
    int pre[310];
    memset(pre,0,sizeof(pre));
    for(int i=1;i<=300;i++)
    {
        f[i][1]=f[i-1][1]+1;
        for(int j=1;j<=300;j++)
        {
            if(GCD(i,j)==1)
                pre[j]++;
        }
        for(int j=2;j<=300;j++)
        {
            f[i][j]=f[i][j-1]+pre[j];
        }
    }
}
int main()
{
    init();
    int n,m;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            break;
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                ans+=f[i-1][j-1]-f[(i-1)/2][(j-1)/2];
            }
        }
        cout<<ans*2<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sklearn中自带了波士顿房价数据集,可以通过以下代码导入: ``` from sklearn.datasets import load_boston boston = load_boston() X = boston.data # 特征矩阵 y = boston.target # 目标向量 ``` 其中,X是一个13维的特征矩阵,y是一个样本数量为506的目标向量。可以通过以下代码查看数据集的描述: ``` print(boston.DESCR) ``` 输出结果如下: ``` .. _boston_dataset: Boston house prices dataset --------------------------- **Data Set Characteristics:** :Number of Instances: 506 :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target. :Attribute Information (in order): - CRIM per capita crime rate by town - ZN proportion of residential land zoned for lots over 25,000 sq.ft. - INDUS proportion of non-retail business acres per town - CHAS Charles River dummy variable (= 1 if tract bounds river; 0 otherwise) - NOX nitric oxides concentration (parts per 10 million) - RM average number of rooms per dwelling - AGE proportion of owner-occupied units built prior to 1940 - DIS weighted distances to five Boston employment centres - RAD index of accessibility to radial highways - TAX full-value property-tax rate per $10,000 - PTRATIO pupil-teacher ratio by town - B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town - LSTAT % lower status of the population - MEDV Median value of owner-occupied homes in $1000's :Missing Attribute Values: None :Creator: Harrison, D. and Rubinfeld, D.L. This is a copy of UCI ML housing dataset. https://archive.ics.uci.edu/ml/machine-learning-databases/housing/ This dataset was taken from the StatLib library which is maintained at Carnegie Mellon University. The Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic prices and the demand for clean air', J. Environ. Economics & Management, vol.5, 81-102, 1978. Used in Belsley, Kuh & Welsch, 'Regression diagnostics ...', Wiley, 1980. N.B. Various transformations are used in the table on pages 244-261 of the latter. The Boston house-price data has been used in many machine learning papers that address regression problems. **References** - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261. - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值