利用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. webresource.axd文件的配置及使用

    今天看到同事的代码中使用到了webresource.axd,特地认真地看了一下它的使用.主要用途有两点: 1.当作httphandler用,但是比handler更好用一点,不需要考虑路径,用的时候,只 ...

  2. swift-闭包和类的声明

    //闭包:类似Oc中的block 反向传值引起代码的回调 func hasClosureMathes(arr : [Int],value:Int,cb:(num:Int,value : Int)-&g ...

  3. iOS常见的设计模式

    ios开发学习中,经常弄不清楚ios的开发模式,今天我们就来进行简单的总结和探讨~ (一)代理模式 应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则 ...

  4. RS485接线 - 为什么要给2线制RS485接3根线?

    http://www.chipkin.com/rs485-cables-why-you-need-3-wires-for-2-two-wire-rs485/ RS485needs 3 conducto ...

  5. css样式 第6节

    程序员语录: 不要太刻意地把写程序这件事和挣钱挂起来,局限了你挣钱的本事 <html> <head> <title>网页样式</title> </ ...

  6. UVA 11078 Open Credit System(扫描 维护最大值)

    Open Credit System In an open credit system, the students can choose any course they like, but there ...

  7. UVA 10795 A Different Task(汉诺塔 递归))

    A Different Task The (Three peg) Tower of Hanoi problem is a popular one in computer science. Briefl ...

  8. C++类继承内存布局(二)

    转自:http://blog.csdn.net/jiangyi711/article/details/4890889# (二 )成员变量 前面介绍完了类布局,接下来考虑不同的继承方式下,访问成员变量的 ...

  9. Hash函数的安全性

    我们为了保证消息的完整性,引进了散列函数,那么散列函数会对安全正造成什么影响呢?这是需要好好研究一番的问题. 三个概念: 1.如果y<>x,且h(x)=h(y),则称为碰撞. 2.对于给定 ...

  10. height为100%的问题

    问题描述 很多同学,对于设置div 的高度为100%时,有疑惑. 设置div 的高度为100%,意思是此 div 的高度 铺满父元素. 那么 怎么使 div 铺满浏览器屏幕? 代码 <!DOCT ...