zoukankan      html  css  js  c++  java
  • Codeforces Round #489 (Div. 2) B、C

    B. Nastya Studies Informatics
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.

    We define a pair of integers (a, bgood, if GCD(a, b) = x and LCM(a, b) = y, where GCD(a, b) denotes the greatest common divisorof a and b, and LCM(a, b) denotes the least common multiple of a and b.

    You are given two integers x and y. You are to find the number of good pairs of integers (a, b) such that l ≤ a, b ≤ r. Note that pairs (a, b)and (b, a) are considered different if a ≠ b.

    Input

    The only line contains four integers l, r, x, y (1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ y ≤ 109).

    Output

    In the only line print the only integer — the answer for the problem.

    Examples
    input
    1 2 1 2
    output
    2
    input
    1 12 1 12
    output
    4
    input
    50 100 3 30
    output
    0
    Note

    In the first example there are two suitable good pairs of integers (a, b): (1, 2) and (2, 1).

    In the second example there are four suitable good pairs of integers (a, b): (1, 12), (12, 1), (3, 4) and (4, 3).

    In the third example there are good pairs of integers, for example, (3, 30), but none of them fits the condition l ≤ a, b ≤ r.

    题意  在区间[l , r]内 有多少对a,b  的最小公倍数为y(lcm)  和 最大约数为x(gcd)

    解析   我们知道 a*b=y*x  所以y里面肯定还有一个因子x  我们只需要考虑 y/x 有多少对因子p,q p*q=y/x且l<=q*x,p*x<=r 且 gcd(q,p)=1

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+50,mod = 998244353,inf=0x3f3f3f3f;
    typedef long long ll;
    ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
    int main()
    {
        ll l,r,x,y;
        cin>>l>>r>>x>>y;
        ll ans=0;
        if(y%x==0)
        {
            y=y/x;
        }
        else
        {
            cout<<ans<<endl;
            return 0;
        }
        for(ll i=1;i<=sqrt(y);i++)
        {
            if(y%i==0)
            {
                ll temp=y/i;
                if(temp*x>=l&&temp*x<=r&&i*x>=l&&i*x<=r&&gcd(temp,i)==1)
                {
                    if(temp==i)
                        ans++;
                    else
                        ans+=2;
                   // cout<<i<<" "<<temp<<endl;
                }
            }
        }
        cout<<ans<<endl;
    }
    C. Nastya and a Wardrobe
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Nastya received a gift on New Year — a magic wardrobe. It is magic because in the end of each month the number of dresses in it doubles (i.e. the number of dresses becomes twice as large as it is in the beginning of the month).

    Unfortunately, right after the doubling the wardrobe eats one of the dresses (if any) with the 50% probability. It happens every month except the last one in the year.

    Nastya owns x dresses now, so she became interested in the expected number of dresses she will have in one year. Nastya lives in Byteland, so the year lasts for k + 1 months.

    Nastya is really busy, so she wants you to solve this problem. You are the programmer, after all. Also, you should find the answer modulo 109 + 7, because it is easy to see that it is always integer.

    Input

    The only line contains two integers x and k (0 ≤ x, k ≤ 1018), where x is the initial number of dresses and k + 1 is the number of months in a year in Byteland.

    Output

    In the only line print a single integer — the expected number of dresses Nastya will own one year later modulo 109 + 7.

    Examples
    input
    2 0
    output
    4
    input
    2 1
    output
    7
    input
    3 2
    output
    21
    Note

    In the first example a year consists on only one month, so the wardrobe does not eat dresses at all.

    In the second example after the first month there are 3 dresses with 50% probability and 4 dresses with 50% probability. Thus, in the end of the year there are 6 dresses with 50% probability and 8 dresses with 50% probability. This way the answer for this test is (6 + 8) / 2 = 7.

    题意  有x件裙子 有k+1个月 每过一个月裙子增长一倍 但有50%的可能会少一条不包括最后一个月 问最后的数学期望

    解析  数学规律题 推一推就发现 答案是有规律的 是上一个答案的两倍-1 但是我们不能模拟 要写出规律来 所以再总结一下 发现差值是 q=(4*x-2)*2的等比数列 然后求和一下加上初始值就是答案。 

    教训 :取模不是随便取的 ,先算一下,要爆ll的时候再取模。

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+50,inf=0x3f3f3f3f;
    typedef long long ll;
    const int mod=1e9+7;
    ll poww(ll n,ll m)
    {
        ll ans = 1;
        while(m > 0)
        {
            if(m & 1)ans = (ans * n) % mod;
            m = m >> 1;
            n = (n * n) % mod;
        }
        return ans;
    }
    ll sum(ll a,ll n)
    {
        if(n==0)
            return 0;
        if(n==1)
            return a;
        ll t=sum(a,n/2);
        if(n&1)
        {
            ll cur=poww(a,n/2+1)%mod;
            t=(t+(t*cur)%mod)%mod;
            t=(t+cur)%mod;
        }
        else
        {
            ll cur=poww(a,n/2)%mod;
            t=(t+(t*cur)%mod)%mod;
        }
        return t;
    }
    int main()
    {
        ll x,m;
        cin>>x>>m;
        x*=2;
        ll temp=x*2-2;
        if(x==0)
            cout<<0<<endl;
        else if(m==0)
            cout<<x%mod<<endl;
        else if(m==1)
            cout<<(temp+1)%mod<<endl;
        else
        {
            ll ans=(x*2-1)%mod;
            ans=ans+((sum(2,m-2)+1)%mod)*(temp%mod)%mod;
            cout<<ans%mod<<endl;
        }
    }
  • 相关阅读:
    强连通分量 Tarjan
    【二叉搜索树】hdu 3791
    【二叉树】hdu 1622 Trees on the level
    【二叉树】hdu 1710 Binary Tree Traversals
    【leetcode】lower_bound
    【leetcode dp】629. K Inverse Pairs Array
    【leetcode最短路】818. Race Car
    【leetcode 字符串】466. Count The Repetitions
    【leetcode dp】132. Palindrome Partitioning II
    【leetcode dp】Dungeon Game
  • 原文地址:https://www.cnblogs.com/stranger-/p/9270035.html
Copyright © 2011-2022 走看看