【牛客多校 #7】 H Dividing 整数分块

链接:https://ac.nowcoder.com/acm/contest/5672/H
来源:牛客网

The following rules define a kind of integer tuple - the Legend Tuple:
(1, k) is always a Legend Tuple, where k is an integer.
if (n, k) is a Legend Tuple, (n + k, k) is also a Legend Tuple.
if (n, k) is a Legend Tuple, (nk, k) is also a Legend Tuple.

We want to know the number of the Legend Tuples (n, k) where 1 \le n \le N, 1 \le k \le K1≤n≤N,1≤k≤K.

In order to avoid calculations of huge integers, report the answer modulo 10^9+710
9
+7 instead.
输入描述:
The input contains two integers N and K, 1 \le N, K \le 10^{12}1≤N,K≤10
12
.
输出描述:
Output the answer modulo 10^9+710
9
+7.
示例1
输入
复制
3 3
输出
复制
8
示例2
输入
复制
3 9
输出
复制
14

题意:用题给的三种操作,问N , K范围内有多少个不同二元组

思路:

找找规律会发现对于每个k,n%k == 0的所有点都可以凑到,n%k == 1也如此。那么就每次累加(n/1 + n/2 + n/ 3…+ n/4…+n/k) + ((n-1)/1 + (n-1)/2 + (n-1)/ 3…+ (n-1)/4…+(n-1)/k) , 统计完去掉k=1时候重合的,加上除k>=2时候每次漏的1即是答案。利用整数分块算法解答。(最后累加完要多加个 + mod再取模,血的教训。。。)

AC代码:

#include
#include
#include
#include
#include
#include
#include
#include 
#include
#include 
#include 
#include 
#include
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(long long i=a;i<=n;++i)
#define per(i,n,a) for(long long i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll n,k;

inline ll func(ll x)
{
    ll res=0;
    for(ll l=1,r=0;l<=min(x,k);l=r+1)
    {
        r=min(x/(x/l),min(x,k));
        res=(res+(r-l+1)*(x/l)%mod)%mod;
    }
    return res;
}

int main()
{
    n = read(), k = read();
    ll t = n;
    ll ans = 0; ll m = min(n,k);
    if(k > 1)
    ans = ((func(n))%mod + func(n-1)%mod - (n-1)%mod + (m-1)%mod + mod )%mod;
    else ans = n%mod;
    n = t;
    if(k>n)
    ans += k - n ;
    ans %= mod;
    cout<<ans%mod<<'\n';
    return 0;
}

你可能感兴趣的:(数学,校队训练,题解)