利用bzoj2705的结论我们很容易优化这道等价类计数的问题

sum(n^gcd(i,n))/n mod p (1<=i<=n)

=sum(phi(n/L)*n^L)/n mod p (n mod L=0)

=sum(phi(n/L)*n^(L-1)) mod p

这题时限略紧,我的pascal怎么都卡不过去,请指教

 var b:array[..] of longint;
    s,p,i,t,m:longint;
    n,ans:longint; function quick(x:longint):longint;
  var i,j:longint;
  begin
    j:=;
    while x<> do
    begin
      inc(j);
      b[j]:=x mod ;
      x:=x shr ;
    end;
    quick:=;
    for i:=j downto do
    begin
      quick:=sqr(quick) mod p;
      if b[i]= then quick:=quick*m mod p;
    end;
  end; function phi(x:longint):longint;
  var i:longint;
  begin
    phi:=;
    for i:= to trunc(sqrt(x)) do
      if x mod i= then
      begin
        phi:=phi*(i-);
        x:=x div i;
        while x mod i= do
        begin
          x:=x div i;
          phi:=phi*i;
        end;
        if x= then break;
      end;
    if x> then phi:=phi*(x-);
    phi:=phi mod p;
  end; begin
  readln(t);
  while t> do
  begin
    readln(n,p);
    m:=n mod p;
    ans:=;
    for i:= to trunc(sqrt(n)) do
      if n mod i= then
      begin
        ans:=ans+phi(n div i)*quick(i-) mod p;
        if i<>n div i then ans:=ans+phi(i)*quick(n div i-) mod p;
        ans:=ans mod p;
      end;
    writeln(ans);
    dec(t);
  end;
end.

poj2154的更多相关文章

  1. 【poj2154】 Color

    http://poj.org/problem?id=2154 (题目链接) 题意 n个珠子的项链,可以染上n中颜色,项链可以旋转不能翻转,求染色方案数. Solution 经典的公式: \begin{ ...

  2. 【POJ2154】Color Pólya定理+欧拉函数

    [POJ2154]Color 题意:求用$n$种颜色染$n$个珠子的项链的方案数.在旋转后相同的方案算作一种.答案对$P$取模. 询问次数$\le 3500$,$n\le 10^9,P\le 3000 ...

  3. [POJ1286&POJ2154&POJ2409]Polya定理

    Polya定理 L=1/|G|*(m^c(p1)+m^c(p2)+...+m^c(pk)) G为置换群大小 m为颜色数量 c(pi)表示第i个置换的循环节数 如置换(123)(45)(6)其循环节数为 ...

  4. POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)

    由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...

  5. 【poj2154】Color Polya定理+欧拉函数

    题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题 ...

  6. 【数论】【Polya定理】【枚举约数】【欧拉函数】【Java】poj2154 Color

    你随便写一下出来,发现polya原理的式子里面好多gcd是相同的,gcd(n,i)=k可以改写成gcd(n/k,i/k)=1,也就是说指数为k的项的个数为phi(n/k),就很好求了,最后除的那个n直 ...

  7. poj2154(polya定理+欧拉函数)

    题目链接:http://poj.org/problem?id=2154 题意:n 种颜色的珠子构成一个长为 n 的环,每种颜色珠子个数无限,也不一定要用上所有颜色,旋转可以得到状态只算一种,问有多少种 ...

  8. POJ2154 Color

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10322   Accepted: 3360 Description Bead ...

  9. POJ2154 Color(Polya定理)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11654   Accepted: 3756 Description Bead ...

随机推荐

  1. Android adapter适配器的使用

    说起Adapter的使用,首先想到的就是listview或各种各样的Adapter.下面我们对常用的一些Adapter进行简单的使用讲解. 这是Adapter的关系图: 下面的所有例子均使用同一个布局 ...

  2. angularjs + springmvc 上传和下载

    jsp: <form ng-submit="uploadFile()" class="form-horizontal" enctype="mul ...

  3. SqlServer 由于未在SqlServer的此实例上安装复制组件解决方法

    sqlserver2005在复制订阅时出现: “由于未在SqlServer的此实例上安装复制组件,Microsoft SQL server 无法访问这些组件,请参阅SQL Server……” 解决方法 ...

  4. 学习笔记_Java get和post区别(转载_GET一般用于获取/查询资源信息,而POST一般用于更新资源信息)

    转载自:[hyddd(http://www.cnblogs.com/hyddd/)] 总结一下,      Get是向服务器发索取数据的一种请求      而Post是向服务器提交数据的一种请求,在F ...

  5. O-C相关05:方法的封装.

    前言:在 OC 中进行封装, 就是实现设置实例变量和获取实例变量数据的方法, 常常称为 setter 方法和 getter 方法. 或称为 get set 读写器. 1,setter 方法 sette ...

  6. 在类成员函数后面加const

    在看开源代码时,经常会看到在类的成员函数后面加const,之前了没有太关注过,近来闲来无事,就想起这件事,网上查了一下,大概明白了是怎么回事,这里引用CSDN愽文里的段话:“编译器会自动给每一个函数加 ...

  7. Entity SQL 初入

    Entity SQL 是 ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM).Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询 ...

  8. leetcode problem 32 -- Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  9. leetcode problem 31 -- Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  10. 《sed的流艺术之四》-linux命令五分钟系列之二十四

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...