2017ACM暑期多校联合训练 - Team 4 1003 HDU 6069 Counting Divisors (区间素数筛选+因子数)
Problem Description
In mathematics, the function d(n) denotes the number of divisors of positive integer n.
For example, d(12)=6 because 1,2,3,4,6,12 are all 12's divisors.
In this problem, given l,r and k, your task is to calculate the following thing :
(∑i=lrd(ik))mod998244353
Input
The first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.
In each test case, there are 3 integers l,r,k(1≤l≤r≤1012,r−l≤106,1≤k≤107).
Output
For each test case, print a single line containing an integer, denoting the answer.
Sample Input
3
1 5 1
1 10 2
1 100 3
Sample Output
10
48
2302
分析:
如果一个数n可以分解成n=p1m1*p2m2*···*pn^mn的话(其中p1,p1···为素数),那么这个数的因子个数就为(m1+1)*(m2+1)*···*(mn+1)。
同样的,这个数由n变为nk的话,相应的次数前面分别乘以k即可。即nk的因子个数为(k*m1+1)*(km2+1)*···*(kmn+1)。
这个问题解决掉之后,我们会发现数据范围太大,我们的数组分本没办法开到那么大,我们可以把数据由前半部分来推出后半部分。
先打个1e6范围内的素数表,然后枚举可行范围内的每个素数,在区间[ l , r ]内寻找所有的该素数的倍数,将其分解质因数。
到最后如果一个数没有变成1,那就说明这个数是大于1e6的质数。(质数只有1和它本身)那么如果按照规律计算的话,只需要乘上一个(k+1)就行了。
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
const int mod=998244353;
int n;
int cnt=0;
int primes[maxn];
int vis[maxn];
void get_primes()///筛选法求出1e6之内的素数,比1e6大的素数可以通过这些素数间接的求出来
{
int m=sqrt(maxn+0.5);///开方,循环到这个就行了
for(int i=2; i<=m; i++)
{
if(!vis[i])
{
for(int j=i*i; j<=maxn; j+=i)
vis[j]=1;
}
}
for(int i=2; i<=maxn; i++)
if(!vis[i]) primes[cnt++]=i;
}
ll l, r, k;
ll sum[maxn], num[maxn];
int main()
{
get_primes();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld",&l,&r,&k);
ll ans=0;
///因为l和r的范围比较大,但是它们之间的差值不会查过1e6,可以将数组缩小一点
for(ll i=l; i<=r; i++)
{
sum[i-l]=1;///个数
num[i-l]=i;///表示的是这个数
}
for(int i=0; i<cnt && primes[i]*primes[i]<=r; i++)///所有的素数
{
///求出的是[l,r]区间中第一个能够被rimes[i]整除的数
ll tmp=ceil((long double)l/primes[i])*primes[i];
for(ll j=tmp; j<=r; j+=primes[i])///枚举所有的这个素数的倍数
{
if(num[j-l]%primes[i]==0)
{
int res=0;
while(num[j-l]%primes[i]==0)
{
res++;
num[j-l]/=primes[i];
}
sum[j-l]=(sum[j-l]*(((ll)res*k+1))%mod)%mod;
}
}
}
for(ll i=l; i<=r; i++)
{
if(num[i-l]!=1)///那些本身是素数的数
sum[i-l]=(sum[i-l]*(k+1))%mod; ///大于1e6的质数
ans=(ans+sum[i-l])%mod;
}
printf("%lld\n",ans);
}
return 0;
}
2017ACM暑期多校联合训练 - Team 4 1003 HDU 6069 Counting Divisors (区间素数筛选+因子数)的更多相关文章
- 2017ACM暑期多校联合训练 - Team 6 1003 HDU 6098 Inversion (模拟)
题目链接 Problem Description Give an array A, the index starts from 1. Now we want to know Bi=maxi∤jAj , ...
- 2017ACM暑期多校联合训练 - Team 3 1003 HDU 6058 Kanade's sum (模拟)
题目链接 Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th largest elem ...
- 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)
题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...
- 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)
题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...
- 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)
题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...
- 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...
- 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)
题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...
随机推荐
- <问吧>调查问卷心得体会
<问吧>调查问卷心得与体会 在这之前,我们已经组成了一个六个人的小团队---“走廊奔跑队”,我们这次做的这个项目的名称是:问吧.在项目实施之前,我们必做的一步就是需求分析,目的就是充分了解 ...
- 【php】session读写锁
事件:a文件中操作$_SESSION['start'] = 'yes'; sleep(100); 休眠100s 在这休眠的时间段中,b文件操作$_SESSION['start'] = 'no'; 结 ...
- PreparedStatement的execute误解
boolean execute() throws SQLException在此 PreparedStatement 对象中执行 SQL 语句,该语句可以是任何种类的 SQL 语句.一些特别处理过的语 ...
- EF中Json序列化对象时检测到循环引用的解决办法
MVC4 EF中将数据表外键引用的是自身,转换成Json时,总是提示错误:“序列化类型为....的对象时检测到循环引用.”: 解决办法: 把要序列化的对象转为匿名对象去掉导航属性,如下 :本来是var ...
- SPAMS:稀疏建模工具箱
https://chunqiu.blog.ustc.edu.cn/?p=570 http://spams-devel.gforge.inria.fr/index.html 在一篇显著性检测文章:Sal ...
- js null表示没有取到html中的元素 undenfind 表示没有被赋值
js null表示没有取到html中的元素 undenfind 表示没有被赋值
- 51nod 1624 取余最短路(set)
题意: 佳佳有一个n*m的带权矩阵,她想从(1,1)出发走到(n,m)且只能往右往下移动,她能得到的娱乐值为所经过的位置的权的总和. 有一天,她被下了恶毒的诅咒,这个诅咒的作用是将她的娱乐值变为对p取 ...
- android面试(3)---基本问题
1.值类型,引用类型? 基本数据类型都是值类型:byte,short,int,long,float,double,char,boolean 其他类型都是引用类型. 引用类型在传入方法是,方法内部对引用 ...
- (转)解决点击a标签返回页面顶部的问题
本文转载至http://www.cnblogs.com/chenluomenggongzi/p/5950670.html 1 <!DOCTYPE html> 2 <html lang ...
- BZOJ4939 Ynoi2016掉进兔子洞(莫队+bitset)
容易发现要求三个区间各数出现次数的最小值.考虑bitset,不去重离散化后and一发就可以了.于是莫队求出每个区间的bitset.注意空间开不下,做多次即可.输出的东西错了都能调一年服了我了. #in ...