当前位置:网站首页>[noi simulation] Simen Nong number (number theory, linked list)
[noi simulation] Simen Nong number (number theory, linked list)
2022-07-21 01:49:00 【DD(XYX)】
Background
The eternal God OneInDark A linear algebra problem that no one can do , Beat with this question EI Master queen , Run to crashed Fight in front of the door .
But I didn't think ,crashed When the door is not fully opened , This problem was broken by him , This is beyond OneInDark The expectation of his second cut .
“ Just find me some linear algebra problems , Is this looking down on me !” crashed Roaring . Voice is not falling. , The two gods appeared on the Simen arena .
“ Look good. , I'll give you a function problem this time !……”
OneInDark Studied for a second , Something's wrong ,“ Um. ? This is not a Tao generating function !”
“ It's been a second for you ! ha-ha ! Be hurt !” crashed laugh .
however OneInDark Immediately cut the problem faster than the speed of light , Make the final completion time become − 1 m s -1\rm\,ms −1ms .
Topic
Defined function F ( x ) = ∏ k = 0 ⌊ x − 1 ⌋ ( x − k 2 ) F(x)=\prod_{k=0}^{\lfloor\sqrt{x-1}\rfloor} (x-k^2) F(x)=∏k=0⌊x−1⌋(x−k2) , Seek for [ l , r ] [l,r] [l,r] How many positive integers are there in x x x Satisfy F ( x ) F(x) F(x) yes m m m Multiple .
The input line l r m
, Output a line of answers .
Limit : 1 ≤ l ≤ r ≤ 1 0 12 , 2 ≤ m ≤ 1 0 6 1\leq l\leq r\leq 10^{12},2\leq m\leq 10^6 1≤l≤r≤1012,2≤m≤106 ,1 s,128 mb .
Examples
1 1000000000000 1000000
599999999844
Answer key
When you think about this problem completely with your brain doing mathematical problem deduction , You lose. .
We might as well revise it to F ( x ) = ∏ k = 0 ⌊ x − 1 ⌋ ( ( x − k 2 ) m o d m ) F(x)=\prod_{k=0}^{\lfloor\sqrt{x-1}\rfloor} ((x-k^2)\mod m) F(x)=∏k=0⌊x−1⌋((x−k2)modm) , So it is not difficult to find , F ( x + m ) F(x+m) F(x+m) It must be F ( x ) F(x) F(x) Multiple .
That means , modulus m m m An equal series x x x , stay F ( x ) F(x) F(x) Whether it is m m m The multiple of is monotonous .
therefore , We can find the smallest of every congruence class x x x Satisfy F ( x ) F(x) F(x) yes m m m Multiple . Because of another k 2 k^2 k2 All numerical contributions to a congruence class are equal , So we just need to find out k 2 k^2 k2 Less than it should be x x x One of the biggest k k k That's it .
We enumerate from small to large k k k , And statistics k k k The contribution of . But such violence will be overtime .
We might as well m m m Prime factor decomposition , m = ∏ p i w i m=\prod p_i^{w_i} m=∏piwi, For each p i p_i pi Consider enumeration alone k k k It's a contribution , Find out that each congruence class contains p i w i p_i^{w_i} piwi Minimum timestamp of , Finally, find the maximum value of the timestamp for each congruence class .
So every enumeration k k k When , from k 2 m o d m k^2\mod m k2modm Start dancing , Every time you jump back p i p_i pi length , Maintain and O ( 1 ) O(1) O(1) Skip all already included p i w i p_i^{w_i} piwi The location of , Violence multiplies contribution . Because every calculation will make a congruence p i p_i pi More times , Finally arrive at p i w i p_i^{w_i} piwi stop it , It can be proved that the time complexity is O ( m log m ) O(m\log m) O(mlogm) Of .
Total algorithm time complexity O ( m log m + r log m ) O(m\log m+\sqrt{r}\log m) O(mlogm+rlogm) .
CODE
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<random>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#pragma GCC optimize(2)
using namespace std;
#define MAXN 1000005
#define LL long long
#define ULL unsigned long long
#define ENDL putchar('\n')
#define DB double
#define lowbit(x) (-(x) & (x))
#define FI first
#define SE second
#define PR pair<int,int>
#define UIN unsigned int
int xchar() {
static const int maxn = 1000000;
static char b[maxn];
static int pos = 0,len = 0;
if(pos == len) pos = 0,len = fread(b,1,maxn,stdin);
if(pos == len) return -1;
return b[pos ++];
}
// #define getchar() xchar()
inline LL read() {
LL f = 1,x = 0;int s = getchar();
while(s < '0' || s > '9') {
if(s<0)return -1;if(s=='-')f=-f;s = getchar();}
while(s >= '0' && s <= '9') {
x = (x<<1) + (x<<3) + (s^48);s = getchar();}
return f*x;
}
void putpos(LL x) {
if(!x)return ;putpos(x/10);putchar((x%10)^48);}
inline void putnum(LL x) {
if(!x) {
putchar('0');return ;}
if(x<0) putchar('-'),x = -x;
return putpos(x);
}
inline void AIput(LL x,int c) {
putnum(x);putchar(c);}
int MOD = 1;
int n,m,s,o,k;
int dp[MAXN],f[MAXN];
LL c[MAXN];
PR fct[MAXN];
int hd[MAXN],nx[MAXN];
int main() {
LL l = read(),r = read();MOD = read();
int nn = MOD;
for(int i = 2;i*i <= nn;i ++) {
if(nn % i == 0) {
int ct = 0;
while(nn % i == 0) nn /= i,ct ++;
fct[++ m] = {
i,ct};
}
}
if(nn > 1) fct[++ m] = {
nn,1};
for(int i = 1;i <= m;i ++) {
int x = fct[i].FI,w = fct[i].SE,xx = 1;
for(int j = 0;j < w;j ++) xx *= x;
for(int j = 0;j < x;j ++) hd[j] = -1;
for(int j = 0;j < MOD;j ++) {
f[j] = 1000001,c[j] = 1;
nx[j] = hd[j%x]; hd[j%x] = j;
}
for(int j = 0;j*1ll*j < r;j ++) {
int ad = j*1ll*j%x,po = j*1ll*j%MOD;
vector<int> emp;
int tl = hd[ad]; hd[ad] = -1;
for(int y = tl;y>=0;y = tl) {
tl = nx[y];
int le = (y+MOD-po)%MOD;
(c[y] *= le) %= MOD;
if(c[y] % xx == 0) f[y] = j;
else nx[y] = hd[ad],hd[ad] = y;
}
}
for(int j = 0;j < MOD;j ++) {
dp[j] = max(dp[j],f[j]);
}
}
LL ans = 0;
for(int i = 0;i < MOD;i ++) {
LL st = dp[i]*1ll*dp[i]+1;
st = st + (MOD-st%MOD+i)%MOD;
if(st <= r) ans += (r-st)/MOD+1;
if(st < l) ans -= (l-1-st)/MOD+1;
}
AIput(ans,'\n');
return 0;
}
边栏推荐
- 第九天(抓取流量、路由策略)
- Koch curve
- LeetCode.302 场周赛___03_6121. 裁剪数字后查询第 K 小的数字____排序
- 编译+链接和预处理
- JVM调优方法
- Record of force deduction and question brushing 2---35 Search insertion location
- 使用OpenCV调整图像的亮度和对比度
- ICLR 2022 | gnnaskernel: a general framework that can improve the expression ability of any GNN
- C语言_结构体引入
- LeetCode.1217. 玩筹码____简单贪心
猜你喜欢
测试人遇到难以重现的bug,要怎么办?
20元一支的洗面奶,7天卖了上万,他们是如何做到的?
【R语言文本挖掘】:情感分析与词云图绘制
NetFlow and SNMP are two different network monitoring methods
数据工程系列精讲(第五讲): Data-centric AI之数据集质量
AT32使用内核DWT寄存器设定延时时间
[trivia] about some unity editors, they lack the tiles option when creating tile maps
【琐琐碎碎小知识】 关于部分Unity编辑器在创建瓦片地图时缺乏Tiles选项
力扣刷题14. 最长公共前缀
Handbrake installation problem: prompt to install frameworks NET
随机推荐
LeetCode. 302 weekly games___ 01_ 6120. How many pairs can an array form__ Simple hash
【读后感】NIO 的相关博客
接口测试到底怎么做,看完这篇文章彻底搞清楚
函数方法封装——图片类型QPixmap、QImage与Mat的相互转化
LeetCode.1217. 玩筹码____简单贪心
Record of force deduction and question brushing 3---34 Find the first and last positions of elements in a sorted array
Scala foundation [high order function programming]
计算任意根号n的值
On the surface, the development logic of the meta universe and the Internet is the same, but in fact, it is not
从镜像仓库工具、镜像下载加速工具、安全扫描工具理解镜像存储和镜像安全
Flink1.15源码阅读——flink-annotations
【R语言文本挖掘】:情感分析与词云图绘制
NETFLOW 与 SNMP两种不同的网络监控方法
归一化原来这么重要!深入浅出详解Transformer中的Normalization
Go 每日一库之 gore
【Golang】golang实现发送微信服务号模板消息
JVM调优方法
[trivia] about some unity editors, they lack the tiles option when creating tile maps
力扣刷题7. 整数反转
力扣刷题记录4-----69. x 的平方根