跳到正文

个人记录

Interesting Numbers(牛客夏训9)

FASTergood约 10 分钟
原文
文章信息与版本

文章信息

发布
2024年8月14日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 10 分钟

版本

  1. v2当前
  2. v1

统计类dp

题目

C++
#include <bits/stdc++.h>
using namespace std;
using i128 = __int128_t;

auto &operator>>(istream &it, __int128_t &j)
{
    string val;
    it >> val;
    reverse(val.begin(), val.end());
    __int128_t ans = 0;
    bool f = 0;
    char c = val.back();
    val.pop_back();
    for (; c < '0' || c > '9'; c = val.back(), val.pop_back())
    {
        if (c == '-')
        {
            f = 1;
        }
    }
    for (; c >= '0' && c <= '9'; c = val.back(), val.pop_back())
    {
        ans = ans * 10 + c - '0';
    }
    j = f ? -ans : ans;
    return it;
}

auto &operator<<(ostream &os, const __int128_t &j)
{
    string ans;
    function<void(__int128_t)> write = [&](__int128_t x)
    {
        if (x < 0)
            ans += '-', x = -x;
        if (x > 9)
            write(x / 10);
        ans += x % 10 + '0';
    };
    write(j);
    return os << ans;
}

// 手动实现平方根计算函数
i128 sqrt_i128(i128 x)
{
    if (x == 0 || x == 1)
        return x;

    i128 left = 1, right = x, res = 0;
    while (left <= right)
    {
        i128 mid = left + (right - left) / 2;
        if (mid <= x / mid)
        {
            left = mid + 1;
            res = mid;
        }
        else
        {
            right = mid - 1;
        }
    }
    return res;
}

const int N = 1e5 + 5;
int n;
string l, r;

void init(i128 &a, i128 &b, string s)
{
    int len = n / 2;
    i128 res = 0;
    for (int i = 0; i < len; ++i)
    {
        res = res * 10 + s[i] - '0';
    }
    a = res;
    res = 0;
    for (int i = len; i < n; ++i)
    {
        res = res * 10 + s[i] - '0';
    }
    b = res;
}
i128 ksm(i128 a,int b) {
    i128 res = 1;
    while(b) {
        if(b&1) res = (res * a);
        a = a*a;
        b >>=1;
    }
    return res;
}
void solve()
{
    cin >> n;
    cin >> l >> r;
    i128 l1, l2, r1, r2;
    init(l1, l2, l);
    init(r1, r2, r);
    i128 ans = 0;
    i128 p = sqrt_i128(r1 - 1);
    i128 pl = sqrt_i128(l1);
    i128 pp = sqrt_i128(ksm(10,n/2)-1) + 1;
    ans += (p - pl) * pp;

    i128 nr = sqrt_i128(r1);
    if (nr * nr == r1)
    {
        ans += sqrt_i128(r2) + 1;
    }

    i128 nl = sqrt_i128(l1);
    if (nl * nl == l1)
    {
        if (l2 == 0)
            ans += pp;
        else
            ans += pp - sqrt_i128(l2 - 1) - 1;
    }
    cout << ans << endl;
}

signed main()
{
    int T = 1;
    while (T--)
    {
        solve();
    }
    return 0;
}

下面是我的code同样正确

C++
#include<bits/stdc++.h>
#define int  __int128_t
using namespace std;
auto& operator>>(istream& it, __int128_t& j)
{
    string val;
    it >> val;
    reverse(val.begin(), val.end());
    __int128_t ans = 0;
    bool f = 0;
    char c = val.back();
    val.pop_back();
    for (; c < '0' || c > '9'; c = val.back(), val.pop_back())
    {
        if (c == '-')
        {
            f = 1;
        }
    }
    for (; c >= '0' && c <= '9'; c = val.back(), val.pop_back())
    {
        ans = ans * 10 + c - '0';
    }
    j = f ? -ans : ans;
    return it;
}
auto& operator<<(ostream& os, const __int128_t& j)
{
    string ans;
    function<void(__int128_t)> write = [&](__int128_t x)
        {
            if (x < 0)
                ans += '-', x = -x;
            if (x > 9)
                write(x / 10);
            ans += x % 10 + '0';
        };
    write(j);
    return os << ans;
}
int sqrtl(int  x)
{
    if (x == 0 || x == 1)return x;
    int left =1, right = x, res = 0;
    while (left <right)
    {
        int mid =left+(right-left+1) / 2;
        if (mid <= x / mid){left= mid;res=mid;}
        else right = mid-1;
    }
    return  res;
}
const int N = 1e5 + 5;
int n;
string l, r;
void init(int& a, int& b, string s)
{
    int len = n / 2;
    int res = 0;
    for (int i = 0;i<len;i++)
    {
        res = res * 10 + s[i] - '0';
    }
    a = res;
    res = 0;
    for (int i = len;i < n;i++)
    {
        res = res * 10 + s[i] - '0';
    }
    b = res;
}
int qmi(int a, int b)
{
    int res=1;
    while (b)
    {
        if (b & 1)res = res * a;
            a = a * a;
            b >>= 1;

    }
    return res;
}
void solve()
{
    cin >> n;
    cin >> l >> r;
   int l1, l2, r1, r2;
    init(l1, l2, l);
    init(r1, r2, r);
    int ans = 0;
    int p = sqrtl(r1 - 1);
    int pl = sqrtl(l1);
    int pp = sqrtl(qmi(10, n / 2) - 1) + 1;
    ans += (p - pl) * pp;

    int  nr = sqrtl(r1);
    if (nr * nr == r1)
    {
        ans += sqrtl(r2) + 1;
    }

   int nl = sqrtl(l1);
    if (nl * nl == l1)
    {
        if (l2 == 0)
            ans += pp;
        else
            ans += pp - sqrtl(l2 - 1) - 1;
    }
    cout << ans << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t = 1;
    while (t--)
    {
        solve();
    }
    return 0;
}
原文