zoukankan      html  css  js  c++  java
  • Comet_oj_#0_A

     这道题通过简化方程可得,当n为平方数的时候,解为无穷,当%4不为0时为0 0;计算

    但是要小心,这道题的代码要经过优化。一下为两种代码,一个tle,一个accept。

    #include<iostream>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    const ll mod=1e9+7;
    int main()
    {
        int t;
        ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
        cin>>t;
        while(t--)
        {
            ll n;
            cin>>n;
            double k=sqrt(n);
            if(k==(int)k)
            {
                cout<<"infty"<<endl;
              continue;
            }
              if(n%4!=0){
                cout<<0<<" "<<0<<endl;
              continue;
            }else
            {
                int cnt=0;
                ll ans=0ll;
                    n/=4;
                    int m=sqrt(n);
                    for(ll i=1;i<=m;i++)
                    {
                        if((n%i)==0)
                        {
                            ans+=n*(i+n/i);
                            ans%=mod;
                            cnt++;
                        }
                    }
                    cout<<cnt<<" "<<ans<<endl;
            }
        }
        return 0;
    }
    tle
    #include<iostream>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    const ll mod=1e9+7;
    int main()
    {
        //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
        //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
        int t;
        ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
        cin>>t;
        while(t--)
        {
            int n;
            cin>>n;
            double k=sqrt(n);
            if(k==(int)k)
            {
                cout<<"infty"<<endl;
              continue;
            }
              if(n%4!=0){
                cout<<0<<" "<<0<<endl;
              continue;
            }else
            {
                int cnt=0;
                ll ans=0ll;
                    n/=4;
                    int x,y,z;
                    int m=sqrt(n);
                    for(int i=1;i<=m;i++)
                    {
                        if((n%i)==0)
                        {
                            ans+=1ll*n*(i+n/i);
                            ans%=mod;
                            cnt++;
                        }
                    }
                    cout<<cnt<<" "<<ans<<endl;
            }
        }
        return 0;
    }
    accept

    以上告诉我们,要学会优化!

  • 相关阅读:
    sql试题
    sql中的游标
    SQL Server存储过程 对数组参数的循环处理
    MongoDB安装并随windows开机自启
    延长或控制Session的有效期的方法总结
    回忆我们经典的开发工具(转)
    多线程实例,占用CPU过多
    啥叫单例模式?
    判断字符或字符串里是否有汉字
    百年历19552055年
  • 原文地址:https://www.cnblogs.com/Ean1zhi/p/12215937.html
Copyright © 2011-2022 走看看