Count a * b

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 872    Accepted Submission(s): 315

Problem Description
Marry likes to count the number of ways to choose two non-negative integers a and b less than m to make a×b mod m≠0.

Let's denote f(m) as the number of ways to choose two non-negative integers a and b less than m to make a×b mod m≠0.

She has calculated a lot of f(m) for different m, and now she is interested in another function g(n)=∑m|nf(m). For example, g(6)=f(1)+f(2)+f(3)+f(6)=0+1+4+21=26. She needs you to double check the answer.

Give you n. Your task is to find g(n) modulo 264.

 
Input
The first line contains an integer T indicating the total number of test cases. Each test case is a line with a positive integer n.

1≤T≤20000
1≤n≤109

 
Output
For each test case, print one integer s, representing g(n) modulo 264.
 
Sample Input
2
6
514
 
Sample Output
26
328194
 
Source
 
  • 要推公式啊,贴过程:

  • 注:
  • (1)后半个公式可以看做对于变量a在整数意义上的划分,之后再进行gcd结果的加和。这里对于划分变量的方式有改进的余地,可以根据gcd结果进行划分。考虑gcd(x,a)|x,所以用x的因数对gcd结果进行划分,而且最好用Dirichlet的形式进行改进,毕竟gcd和常函数都是积性函数,Dirichlet形式下的新函数保持积性函数的性质,利于简化计算。
  • (2)对于f(x)的化简结果,考虑带入g(n)有进一步化简的可能
  • (3)考虑整除的传递性,这里如果可以把变量x消去是最好的。我们这里先枚举d,再找x,之后用i代替x/d,i*d|n,其中i*d==x,根据整除性质不难得到i|(d/n),那么就看到一个很熟悉的公式,后半公式就是对于n求全部因数的欧拉函数,结果就是n本身
  • (4)最终结果
  • (5)把因数k次方和理解成多项式相乘处理是有一定好处的,好处在于没有对于除法的处理,全都是简单的加法和乘法,这对于取模运算是一大福音。此外,考虑到因数k次方函数是积性函数,也可以先对n分解质因数之后分步求解,但是这里就要对于每一个质因数的乘方做等比数列求和,牵扯到除法,对应的在代码中要做防溢出操作(好吧,我这个鶸是不会这种骚操作的QAQ)
  • 至于说题目里对于2^64取模就是在unsigned longlong下运算就ok
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; ULL T, n, prime[maxn], nd, theta2;
bool vis[maxn];
int tot, cnt;
void init(int top){
tot=;
memset(vis,true,sizeof(vis));
for(int i=;i<=top;i++){
if(vis[i]){
prime[tot++]=(ULL)i;
}
for(int j=;j<tot&&(i*prime[j]<=top);j++){
vis[i*prime[j]]=false;
if(i%prime[j]==)
break;
}
}
}
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
init(1e5+);
while(~scanf("%llu",&T)){
while(T--){
scanf("%llu",&n);
theta2=1ULL;
nd=n;
for(int i=;i<tot && prime[i]*prime[i]<=n;i++)
if(n%prime[i]==){
ULL p=prime[i], alpha=0ULL;
ULL sigma=1ULL, square=1ULL;
while(n%p==0ULL){
alpha++;
square*=p;
sigma+=square*square;
n/=p;
}
nd*=alpha+1ULL;
theta2*=sigma;
}
if(n>){
nd*=2ULL;
theta2*=(1ULL+n*n);
}
printf("%llu\n",theta2-nd);
}
}
return ;
}

HDU_5528_Count a * b的更多相关文章

随机推荐

  1. 单片机成长之路(51基础篇) - 012 MCS-51单片机控制详解–TMOD T2MOD

    TMOD:工作方式控制寄存器 寄存器地址89H,不可位寻址. 位序 B7 B6 B5 B4 B3 B2 B1 B0 位符号 GATE C/T M1 M0 GATE C/T M1 M0 GATE——门控 ...

  2. Effective Java 第三版——80. EXECUTORS, TASKS, STREAMS 优于线程

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  3. 【Java】Java8的Lambda入门记录

    简化定义匿名实现类 匿名实现类的传统方式 创建一个线程,需要实现Runnable接口,并实现public void run()方法,用传统的方式是这样的: public static void mai ...

  4. 【转】RPC简单介绍

    RPC简单介绍 RPC 1. RPC是什么 RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络 ...

  5. 北京Java笔试题整理

    北京Java笔试题整理 1.什么是java虚拟机?为什么ava被称作是"平台无关的编程语言? 答:Java虚拟机可以理解为一个特殊的"操作系统",只是它连接的不是硬件,而 ...

  6. SQL 性能分析

    SELECT s2.dbid, s1.sql_handle, (SELECT TOP SUBSTRING(s2.text,statement_start_offset / + , ( (CASE WH ...

  7. MyBatis ResultMap Assocation 返回属性为null的问题

    Model: public class Employee { private Integer id; private String lastName; private String email; pr ...

  8. VS IISExpress REST DELETE 405 Method Not Allowed

    [参考].net IIS MVC Rest api 跨域 PUT DELETE 404 无法使用问题解决方案 今日在使用泛型處理常式處理檔案上傳時,使用了 HTTP 動詞的 PUT.DELETE 進行 ...

  9. SQL Server CTE 递归查询全解

    在TSQL脚本中,也能实现递归查询,SQL Server提供CTE(Common Table Expression),只需要编写少量的代码,就能实现递归查询,本文详细介绍CTE递归调用的特性和使用示例 ...

  10. 再整合ssh时,关于Spring IOC注入问题

    No matching editors or conversion strategy found IOC问题: EmpService bean 实现了 Iemp接口,就不能直接作为参数传入Action ...