挺有趣的恩:洛谷P2155

在纸上打打草稿,写出n!个数,从先往后,遇到不互质的就筛掉——发现一个奇妙的性质!:筛掉的次数、顺序好像是周期性出现的呢~

而且更加妙妙的是,好像还是m!一轮..那么因为n!一定能被m!整除,所以问题转变为:(n!\m! - 有多少个循环节)*(φ(m))。

接下来,φ(m) = m!*(1 - 1/p1)*(1 - 1/p2)...任务就只剩下打出阶乘表&逆元啦。离线的处理会快很多。

#include <bits/stdc++.h>
using namespace std;
#define maxn 10000050
#define ll long long
#define int long long
int maxx, now = , P, T, tot, inv[maxn], ans[], pri[maxn],fac_a[maxn], fac_b[maxn], fac_c[maxn];
bool is_prime[maxn];
struct query
{
int n, m, id, pri;
}Q[]; int read()
{
int x = ;
char c;
c = getchar();
while(c < '' || c > '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} bool cmp1(query a, query b)
{
return a.m < b.m;
} int Get_Pri(int n)
{
for(int i = ; i <= n; i ++)
{
if(!is_prime[i])
{
pri[++ tot] = i;
while(now <= T && pri[tot] > Q[now].m)
{
Q[now].pri = tot - ;
now ++;
}
}
while(now <= T && i == n)
{
Q[now].pri = tot;
now ++;
}
for(int j = ; j <= tot; j ++)
{
if(i * pri[j] > n) break;
is_prime[i * pri[j]] = ;
if(!(i % pri[j])) break;
}
}
} int Get_fac(int n)
{
fac_a[] = fac_a[] = fac_b[] = fac_b[] = fac_c[] = fac_c[] = ;
inv[] = inv[] = ;
for(int i = ; i <= n; i ++)
{
fac_a[i] = (fac_a[i - ] * i) % P;
inv[i] = ((P - P / i) * inv[P % i]) % P;
}
for(int i = ; i <= tot; i ++)
{
fac_b[i] = inv[pri[i]];
fac_b[i] = (fac_b[i] * fac_b[i - ]) % P;
fac_c[i] = pri[i] - ;
fac_c[i] = (fac_c[i] * fac_c[i - ]) % P;
}
} signed main()
{
T = read(), P = read();
for(int i = ; i <= T; i ++)
{
Q[i].n = read(), Q[i].m = read(), Q[i].id = i;
maxx = max(maxx, max(Q[i].n, Q[i].m));
}
sort(Q + , Q + + T, cmp1);
Get_Pri(maxx);
Get_fac(maxx);
for(int i = ; i <= T; i ++)
ans[Q[i].id] = ((fac_a[Q[i].n] * fac_b[Q[i].pri]) % P * fac_c[Q[i].pri]) % P;
for(int i = ; i <= T; i ++)
printf("%lld\n", ans[i]);
return ;
}

最新文章

  1. 自发行python版本制作(二)编译
  2. Android中各组件的生命周期
  3. iOS block示例
  4. JRebel_修改class后无法正确调试问题解决【2014-03-12】
  5. Ajax之数据连接信息捕获
  6. Unix/Linux 网络 IO 模型简介
  7. Asp.NET Core2.0 项目实战入门视频课程_完整版
  8. 【原】Oracle EBS 11无法打开Form及Form显示乱码的解决
  9. JS将/Date(1446704778000)/转换成str
  10. 【python】python为何多线程无法切换
  11. cf29d 深搜,dfs序
  12. CentOS安装Navicat
  13. Git学习篇之git remote add origin错误
  14. Centos7.x gnome 桌面美化
  15. 测试思想-集成测试&#160;关于接口测试&#160;Part&#160;2
  16. 查看linux 内核版本信息
  17. undefined reference to...
  18. photoshop cs6安装过程中安装程序遇到错误:请重启计算机,解决办法
  19. Jad查看源码
  20. Gridview排序与分页-不使用“DataSourceControl DataSource”的情况下如何分页和排序 ...

热门文章

  1. 6 大主流 Web 框架优缺点对比(转)
  2. python的爬虫代理设置
  3. linux文件操作篇 (二) 打开和关闭文件
  4. PHP.45-TP框架商城应用实例-后台20-权限管理-RBAC表构造与代码生成
  5. 谈谈WPF中的CollectionView与CollectionViewSource (1)
  6. 在WPF中自定义控件(3) CustomControl (下)
  7. Windows 10 下如何彻底关闭 Hyper-V 服务(翻外篇)
  8. 『JavaScript』new关键字
  9. volatility的使用
  10. cocos2d-x 键盘和鼠标事件