数论小知识(不定期更新)

题目1:Leading and Trailing

分析

本题求n的k次方的前三位和后三位
1)后三位较为好求,快速幂不断%1000即可
2)前三位:推导公式
n k n^k nk = 1 0 p 10^p 10p (p为double)
(取log) => k l o g n log{n} logn = p
设y = p - (int)p
1 0 y 10^y 10y即为所求

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1000;
const int maxn = 2e6;
const int inf = 0x3f3f3f3f;

ll qpow(ll a, ll n)
{
    ll res = 1;
    while(n)
    {
        if(n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    int t; cin >> t;
    int cases = 0;
    while(t--)
    {
        ll n, k; cin >> n >> k;
        double p = 1.0 * k * log10(double(n));
        ll x = 1.0 * k * log10(double(n));
        double y = p - x;
        ll res1 = 100.0 * pow(10, y);
        ll res2 = qpow(n, k);

        printf("Case %d: %03lld %03lld\n", ++cases, res1, res2);
    }
}

题目2:因数个数和

给个n,求1到n的所有数的约数个数的和~
做法一:分块

ll solve(ll n){
    ll ans = 0;
    for(ll l = 1,r;l <= n;l = r + 1){
        r = n / (n / l);  //l-r的中数贡献了相同数量的约数,因而可以通过分块批量求
        ans += (r-l+1)*(n/l);
    }
    return ans;
}

分析:

参考博客

代码:

#include<stdio.h>
#include<math.h>
int main() {
    int t;
    scanf("%d",&t);
    int n;
    while(t--) {
        int i;
        int t;
        long long sum=0;
        scanf("%d",&n);
        t=(int)sqrt((double)n);
        for(i=1;i<=t;i++)
            sum+=(n/i);
        sum*=2;
        sum=sum-t*t;
        printf("%lld\n",sum);
    }
    return 0;
}

题目3:Harmonic Number LightOJ - 1234

在这里插入图片描述

分析:

神奇打表:小数打表,大数套公式
H n = l o g n + 1 / ( 2 ∗ n ) + C H_n=logn+1/(2 *n) +C Hn=logn+1/(2n)+C(C为欧拉常数,详见代码)

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const double gama = 0.57721566490153286060651209;
const ll mod = 1e9 + 7;
const int maxn = 2e6;
const int inf = 0x3f3f3f3f;
double sum[maxn];

void make()
{
    double s = 0;
    for(int i = 1; i < maxn; i++)
        s += 1.0 / i, sum[i] = s;
}


int main()
{   
    make();
    int t, cases = 0; cin >> t;
    while(t--)
    {
        int n; cin >> n;
        double res;
        if(n < maxn) res = sum[n];
        else res = log(double(n)) + 1.0 / (2 * n) + gama;
        printf("Case %d: %.10lf\n", ++cases, res); 
    }
}

题目4:Harmonic Number (II) LightOJ - 1245

分析:

暴力肯定超时,不妨换个角度——顺瓜摸藤:枚举商
通过分析,商最大为sqrt(n)
枚举商(除数)时,对于除数,直接计算即可,对于商,则需先计算出这个商出现了多少次,在乘以商的值即为贡献。
最后计算完成后扣除重复计算的部分

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 2e6;
const int inf = 0x3f3f3f3f;

int main()
{
    int t; cin >> t;
    int cases = 0;
    while(t--)
    {
        ll n; cin >> n;
        ll sum = 0;
        for(int i = 1; i <= sqrt(n); i++)
        {
            sum += n / i;
            sum += (n / i - n / (i + 1)) * i;
        }
        if(n / (ll)sqrt(n) == (ll)sqrt(n)) sum -= (ll)sqrt(n);
        printf("Case %d: %lld\n", ++cases, sum);
    }
}

题目6:Trailing Zeroes (III) LightOJ - 1138 思维+二分

分析:

我们知道零是由10决定的,10可以分解为素数2和5,而2的数量大于5,根据木桶定理,零的个数就是1-n中5的个数。5含有1个5,10含有一个5,25含有两个5,那么n可能是大于等于5,小于等于5*q,以此为界限进行二分即可。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 2e6;
const int inf = 0x3f3f3f3f;

ll calc(ll n)
{
    ll res = 0;
    while(n)
    {
        res += n;
        n /= 5;
    }
    return res;
}

int main()
{
    int cases = 0;
    int t; cin >> t;
    while(t--)
    {
        ll n; cin >> n;
        int flag = 0;
        ll l = 1, r = n;
        ll mid;
        while(l <= r)
        {
            mid = l + r >> 1;
            ll ans = calc(mid);
            if(ans == n)
            {
                flag = 1;
                break;
            }
            else if(ans < n) l = mid + 1;
            else r = mid - 1;
        }
        if(flag) printf("Case %d: %lld\n", ++cases, mid * 5);
        else printf("Case %d: impossible\n", ++cases);
        
    }
}

题目7:The Super Powers UVA - 11752

分析:

这个题主要是注意上限是 2 64 − 1 2^{64}-1 2641,所以我们需要用unsigned long long.
为了防止ull溢出,我们在枚举的时候需要通过log设置判断条件:

for(int i = 2; i < 65536; i++)
    {
        for(int j = 4; j <= 64 && j < 64 * log(2) / log(i); j++)
        {
            if(vis[j]) res.insert(qpow(i, j));
        }
    }

数学推导为:
i j < 2 64 i^j < 2^{64} ij<264
j < 64log2 / logi

代码:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 1e5;
const int inf = 0x3f3f3f3f;
int vis[maxn];
vector<int> p;
set<ll> res;

void make()
{
    for(int i = 2; i < maxn; i++)
        if(!vis[i])
        {
            for(int j = i * 2; j < maxn; j += i)
                vis[j] = 1;
        }
}

ll qpow(ll a, ll n)
{
    ll res = 1;
    while(n)
    {
        if(n & 1) res *= a;
        a *= a, n >>= 1;
    }
    return res;
}

int main()
{
    make();
    res.insert(1);
    for(int i = 2; i < 65536; i++)
    {
        for(int j = 4; j <= 64 && j < 64 * log(2) / log(i); j++)
        {
            if(vis[j]) res.insert(qpow(i, j));
        }
    }
    for(auto it : res)
        cout << it << endl;
    
}

题目8:

分析:

代码:


# 高校智慧校园解决方案摘要 智慧校园解决方案是针对高校信息化建设的核心工程,旨在通过物联网技术实现数字化校园的智能化升级。该方案通过融合计算机技术、网络通信技术、数据库技术和IC卡识别技术,初步实现了校园一卡通系统,进而通过人脸识别技术实现了更精准的校园安全管理、生活管理、教务管理和资源管理。 方案包括多个管理系统:智慧校园管理平台、一卡通卡务管理系统、一卡通人脸库管理平台、智能人脸识别消费管理系统、疫情防控管理系统、人脸识别无感识别管理系统、会议签到管理系统、人脸识别通道管理系统和图书馆对接管理系统。这些系统共同构成了智慧校园的信息化基础,通过统一数据库和操作平台,实现了数据共享和信息一致性。 智能人脸识别消费管理系统通过人脸识别终端,在无需接触的情况下快速完成消费支付过程,提升了校园服务效率。疫情防控管理系统利用热成像测温技术、视频智能分析等手段,实现了对校园人员体温监测和疫情信息实时上报,提高了校园公共卫生事件的预防和控制能力。 会议签到管理系统和人脸识别通道管理系统均基于人脸识别技术,实现了会议的快速签到和图书馆等场所的高效通行管理。与图书馆对接管理系统实现了一卡通系统与图书馆管理系统的无缝集成,提升了图书借阅的便捷性。 总体而言,该智慧校园解决方案通过集成的信息化管理系统,提升了校园管理的智能化水平,优化了校园生活体验,增强了校园安全,并提高了教学和科研的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值