Codeforces Round #489 (Div. 2) ABC总结

貌似是数学题专场?

一共做了三道  BC 各 WA 5次和3次 最后还都挂了终测。。。= =。

掉了30多分。。。rk 1600+

A题就是求除0以外的所有不相同的数字个数,用set写很简单。

B题给定范围[l,r],你要找出对数(a,b),使gcd(a,b)=x&&lcm(a,b)=y。(1<=l,r,a,b<=1e9)

写了个暴力+剪枝,结果活生生终测TLE on test 94  醉了!!!

看到一个比较快速的解法,枚举i (i*x=a),则i'=y/(i*x) (i'*x=b)   即(a*b==x*y)

i的范围是[1,sqrt(y/x)]。

然后时间复杂度就可以大大降低。。。再加个set去重就可以了。

留一份代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=200010;
ll n,m,k;
int a[maxn],sum[maxn];
int c[maxn];
ll ans,ct,cnt,tmp,flag;
ll l,r,x,y;
ll gcd(ll x,ll y)
{
    while(x^=y^=x^=y%=x);
    return y;
}
int main()
{
    //cout<<gcd(15,6);
    while(scanf("%lld%lld%lld%lld",&l,&r,&x,&y)!=EOF)
    {
        set<pair<ll,ll> >p;
        ans=0;   flag=1;
        if(y%x) {puts("0");continue;}
        tmp=y/x;
        for(ll i=1;i<=sqrt(tmp);i++){
            if(tmp%i==0)
            {
                ll a=i*x,b=(tmp/i)*x;
                if(a>=l&&a<=r&&b>=l&&b<=r&&gcd(i,tmp/i)==1)
                {
                    p.insert(make_pair(a,b));
                    p.insert(make_pair(b,a));
                }
            }
        }
        printf("%d\n",p.size());
      //  if(flag) puts("Yes"); else puts("No");
    }
    return 0;
}

C题就是找规律,题意不说了,分两种情况

1、if x=0  答案为0  必须特判!!!

2、if y=0  答案为(2*x)%mo

3、else 答案为 ((2*x+-1+mo)%mo*power(2,y-1))%mo;

注: 一定不要先把x取mod 否则 x=1e9+7的时候就会WA!!!

留一份代码:

#include<bits/stdc++.h>
#define ll unsigned long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=200010;
const ll mo=1e9+7;
ll n,m,k;
int a[maxn],sum[maxn];
int c[maxn];
ll ans,ct,cnt,tmp,flag;
ll l,r,x,y;
ll power(ll a,ll n)   //a的n次方mod
{
    ll ans=1;
    a=a%mo;
    while (n)
    {
        if(n&1) ans=(ans*a)%mo;
        n>>=1;
        a=(a*a)%mo;
        }
    return ans;
}
ll min(ll a,ll b){if(a<b) return a;return b;}
ll max(ll a,ll b){if(a<b) return b;return a;}
int main()
{
    while(cin>>x>>y)
    {
        if(y==0) cout<<(2*(x%mo))%mo<<endl;
        else if(x==0) cout<<x<<endl;
        else {
        tmp=(2*x-1)%mo;
        tmp=(tmp*power(2,y-1))%mo;
        tmp=(2*tmp)%mo+1;
        cout<<tmp%mo<<endl;}
      //  if(flag) puts("Yes"); else puts("No");
    }
    return 0;
}




猜你喜欢

转载自blog.csdn.net/lsd20164388/article/details/80736443