Description

项链是人体的装饰品之一,是最早出现的首饰。项链除了具有装饰功能之外,有些项 链还具有特殊显示作用,如天主教徒的十字架
链和佛教徒的念珠。 从古至今人们为了美化人体本身,也美 化环境,制造了各种不同风格,不同特点、不同式样的项链,满足了不同肤色、不同民族、不同审美观的人的审美需要。就材料而论,首
饰市场上的项链有黄金、白银、珠宝等几种。珍珠项链为珍珠制成的饰品,即将珍珠 钻孔后用线串在一起,佩戴于项间。天然珍珠项链具有一定的护养作用。 
  
最近,铭铭迷恋上了一种项链。与其他珍珠项链基本上相同,不过这种项链的珠子却 与众不同,是正三菱柱的泰山石雕刻而成的。三菱柱的侧面是正方形构成的,上面刻有数字。 能够让铭铭满意的项链必须满足下面的条件: 
1:这串项链由n颗珠子构成的。 
2:每一个珠子上面的数字x,必须满足0<x<=a,且珠子上面的数字的最大公约数要恰 好为1。两个珠子被认为是相同的,当且仅当他们经过旋转,或者翻转后能够变成一样的。 3:相邻的两个珠子必须不同。 
4:两串项链如果能够经过旋转变成一样的,那么这两串项链就是相同的! 铭铭很好奇如果给定n和a,能够找到多少不同串项链。由于答案可能很大,所以对输 出的答案mod 1000000007。

Input

数据由多组数据构成: 
第一行给定一个T<=10,代表由T组数据。 
接下来T行,每行两个数n和a。 
 

Output

对于每组数据输出有多少不同的串。

Sample Input


2 2

Sample Output

3

HINT

对于100%的数据:所有的n<=10^14,a<=10^7,T<=10;

样例解释:由三种珠子:[1,1,1],[1,1,2],[1,2,2].组成的串有:[1,2],[1,3],[2,3]。

Source

Dragonite修正数据 vfleaking加强数据

有一篇超级详细的题解,所以我就不说了

啊涨姿势了学会了7k+的$O(1)$快速乘

inline LL mul(LL x,LL y)
{
  LL tmp=(x*y-(LL)((LD)x/Mod*y+1.0e-8)*Mod);
  return tmp< ? tmp+Mod : tmp;
}

code:

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define LL long long
#define LD long double
using namespace std;
const LL Maxn = ;
LL Modd = , Mod;
LL n, m;
LL mu[Maxn], pr[Maxn], pl, muu[Maxn], ph[Maxn];
bool v[Maxn], bk;
LL mul ( LL x, LL y ){
if ( bk == true ){
LL ret = , op = ;
if ( x < ){ x = -x; op = -op; }
while (x){
if ( x & ) ret = (ret+y)%Mod;
y = (y<<)%Mod;
x >>= ;
}
return ret*op;
}
else {
LL tmp=(x*y-(LL)((LD)x/Mod*y+1.0e-8)*Mod);
return tmp< ? tmp+Mod : tmp;
}
}
/*inline LL mul(LL x,LL y)
{
LL tmp=(x*y-(LL)((LD)x/Mod*y+1.0e-8)*Mod);
return tmp<0 ? tmp+Mod : tmp;
}*/
void init (){
pl = ;
LL i, j; mu[] = muu[] = ph[] = ;
for ( i = ; i <= ; i ++ ){
if ( v[i] == false ){
pr[++pl] = i;
mu[i] = -;
ph[i] = i-;
}
for ( j = ; j <= pl && i*pr[j] <= ; j ++ ){
v[i*pr[j]] = true;
if ( i % pr[j] == ){ ph[i*pr[j]] = ph[i]*pr[j]; mu[i*pr[j]] = ; break; }
else ph[i*pr[j]] = ph[i]*ph[pr[j]], mu[i*pr[j]] = -mu[i];
}
muu[i] = muu[i-]+mu[i];
}
}
struct matrix {
LL a[][];
LL l1, l2;
void clear (){ memset ( a, , sizeof (a) ); }
}trans, x, fi;
matrix ttimes ( matrix x, matrix y ){
matrix ret;
ret.clear ();
ret.l1 = x.l1; ret.l2 = y.l2;
LL i, j, k;
for ( i = ; i < ret.l1; i ++ ){
for ( j = ; j < ret.l2; j ++ ){
for ( k = ; k < x.l2; k ++ ){
ret.a[i][j] = (ret.a[i][j]+mul(x.a[i][k],y.a[k][j])%Mod)%Mod;
}
}
}
return ret;
}
LL p[Maxn], ppl;
LL phi ( LL x ){
if ( x <= ) return ph[x];
LL ret = x, u = x;
for ( LL i = ; i <= pl; i ++ ){
if ( x % pr[i] == ){
ret = ret/pr[i]*(pr[i]-);
while ( x % pr[i] == ) x /= pr[i];
}
if ( pr[i]*pr[i] > u ) break;
}
if ( x > ) ret = ret/x*(x-);
return ret;
}
LL pow ( LL x, LL k ){
LL ret = ;
while (k){
if ( k & ) ret = mul(ret,x)%Mod;
x = mul(x,x)%Mod;
k >>= ;
}
return ret;
}
int main (){
LL i, j, k, T;
scanf ( "%lld", &T );
init ();
while ( T -- ){
scanf ( "%lld%lld", &n, &m );
if(n== && m==) bk=;
if ( m == ){
printf ( "0\n" );
continue;
}
LL invn, phii;
Mod = Modd;
if ( n % Modd != ) invn = pow ( n, Modd- ), phii = Modd-;
else invn = pow ( n/Modd, Modd- ), Mod = Modd*Modd, phii = Modd*(Modd-);
LL ans2 = , ans3 = ;
LL pos;
for ( i = ; i <= m; i = pos+ ){
LL u = m/i, t; pos = m/(m/i);
t = mul (u,u);
ans2 = (ans2+mul(t,muu[pos]-muu[i-]))%Mod;
t = mul (t,u);
ans3 = (ans3+mul(t,muu[pos]-muu[i-]))%Mod;
}
LL inv = pow ( (LL), phii- );
LL o = mul((ans3+(mul(ans2,(LL)))+)%Mod,inv);
ppl = ; LL sq = sqrt (n);
for ( i = ; i <= sq; i ++ ){
if ( n % i == ) p[++ppl] = i;
}
for ( i = ppl; i >= ; i -- ){
if ( p[i]*p[i] == n ) continue;
p[++ppl] = n/p[i];
}
p[++ppl] = n;
trans.l1 = trans.l2 = ;
trans.a[][] = ; trans.a[][] = o-;
trans.a[][] = ; trans.a[][] = o-;
fi.l1 = ; fi.l2 = ;
fi.a[][] = ; fi.a[][] = mul(o,o-);
LL ans = ;
p[] = ;
for ( i = ; i <= ppl; i ++ ){
x = trans;
for ( j = p[i]-p[i-]; j >= ; j >>= ){
if ( j & ) fi = ttimes ( fi, x );
x = ttimes ( x, x );
}
ans = ( ans + mul(fi.a[][],phi(n/p[i])) ) % Mod;
}
if ( n % Modd == ) ans /= Modd;
printf ( "%lld\n", ((ans*invn)%Modd+Modd)%Modd );
}
return ;
}

bzoj 3202: [Sdoi2013]项链的更多相关文章

  1. 洛谷 P3307: bzoj 3202: [SDOI2013] 项链

    题目传送门:洛谷P3307.这题在bzoj上是权限题. 题意简述: 这题分为两个部分: ① 有一些珠子,每个珠子可以看成一个无序三元组.三元组要满足三个数都在$1$到$m$之间,并且三个数互质,两个珠 ...

  2. bzoj 3202 [Sdoi2013]项链——容斥+置换+推式子

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3202 可见Zinn博客:https://www.cnblogs.com/Zinn/p/100 ...

  3. BZOJ3202 [Sdoi2013]项链

    Problem E: [Sdoi2013]项链 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 427  Solved: 146[Submit][Sta ...

  4. BZOJ 3202 项链

    题目连接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3202 题意:一个项链由n个珠子组成.每个珠子有三个面,每个面上有一个数字,要求每个 ...

  5. bzoj 3202 [Sdoi 2013] 项链 —— 置换+计数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3202 参考了博客: https://www.cnblogs.com/zhoushuyu/p/ ...

  6. BZOJ 3790 神奇项链 hash/后缀自动机+贪心

    Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字母组成的字符串,每个小写字母表示一种颜色. 为了制作这个项链,小 H 购买了两个机器.第一个机器可 ...

  7. bzoj3202:[Sdoi2013]项链

    思路:首先考虑如何求珠子个数,一个珠子由a,b,c三个数组成且属于区间[1,a],并满足gcd(a,b,c)=1.由于要求本质相同,对于a,b,c这样的一个无序的数列且满足gcd(a,b,c)=1,设 ...

  8. bzoj 1493: [NOI2007]项链工厂(线段树)

    1493: [NOI2007]项链工厂 Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 1256  Solved: 545[Submit][Status] ...

  9. [BZOJ 3198] [Sdoi2013] spring 【容斥 + Hash】

    题目链接:BZOJ - 3198 题目分析 题目要求求出有多少对泉有恰好 k 个值相等. 我们用容斥来做. 枚举 2^6 种状态,某一位是 1 表示这一位相同,那么假设 1 的个数为 x . 答案就是 ...

随机推荐

  1. pycharm快捷键及一些常用设置

    pycharm快捷键及一些常用设置,有需要的朋友可以参考下. Alt+Enter 自动添加包 Ctrl+t SVN更新 Ctrl+k SVN提交 Ctrl + / 注释(取消注释)选择的行 Ctrl+ ...

  2. Ajax商品分类三级联动实现

    思路分析: 效果:当页面加载时,利用ajax异步向后台请求数据,加载一级商品类别,当选择一级商品时加载二级商品,选择二级商品加载三级商品. 实现: 1.当拿到数据后加载pid为0的商品,并动态创建op ...

  3. LUA中将未分类数据分为测试集和训练集

    require 'torch' require 'image' local setting = {parent_root = '/home/pxu/image'} function list_chil ...

  4. RubyOnRails local_assigns

    http://api.rubyonrails.org/classes/ActionView/Template.html#method-i-local_assigns Returns a hash wi ...

  5. saltstack命令执行过程

    saltstack命令执行过程 具体步骤如下 Salt stack的Master与Minion之间通过ZeroMq进行消息传递,使用了ZeroMq的发布-订阅模式,连接方式包括tcp,ipc salt ...

  6. [正则表达式]PCRE环视功能

    设想一下这个问题,假设为了方便长串数字的阅读性,需要为其添加逗号作为分隔,需要怎么做呢? 2569836495 => 2,569,836,495 正则表达式的匹配通常是从左往右的,这导致无法使用 ...

  7. idea Error:java: Compilation failed: internal java compiler error

    idea 遇到Error:java: Compilation failed: internal java compiler error 是提示说你当前使用的编译器jdk版本不对. 按住Ctrl+Alt ...

  8. oh my zsh

    简单使用oh my zsh 安装oh my Zsh 安装zsh 安装curl或者wget 下载并安装oh my zsh: curl 下载方式curl -L https://raw.github.com ...

  9. CoreData __ 基本原理

    操作过程 Context想要获取值,先要告诉连接器,我要什么东西 链接器再告诉store, 你给我什么东西, store去找 找到之后返回给链接器,链接器再返回给Context          Co ...

  10. LeetCode 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...