UVA12546_LCM Pair Sum
题目的意思是求 [西伽马(p+q)]其中lcm(p,q)=n。
又见数论呀。
其实这个题目很简单,考虑清楚了可以很简单的方法飘过。
我一开始是这样来考虑的。
对于每一个单独的质因子,如果为p,它的次数为x,那么在p和q中一定有一个为p^x,另一个为p^y(0<=y<=x),只有这样才能保证lcm为p^x。
这样我们可以枚举第一个为p^x,第二个数就是等比数列求和了。
同时我们再枚举第二个为p^x,这样我们就又是等比数列求和了。。。。
这样我们每次分别计算每一个质因子,同时每一个质因子其实是相对独立的,所以我们最后只要做一次乘法就可以了。不过注意每一个质因子出现的次数哦。
嗯到了这里我们就可以知道了,不过对于每一个答案还是统计了两遍,所以要把多出来的减出来。
嗯,大概就是这样的。
但是A掉后,我好像又秒懂了更更简单的办法。诶,深坑啊。自己考虑考虑就知道啦。。。
这个是经过预处理之后才勉强A掉的,内牛满面啊。。。。。——————————
#include <iostream>
#include <cstring>
#include <cstdio>
#define ll long long
#define M 1000000007
using namespace std; ll t,c,p[],a[],cas=;
ll ans;
ll f1[],f2[],f3[]; ll power(ll x,ll y)
{
ll tot=;
while (y)
{
if (y&) tot=(tot*x)%M;
y>>=;
x=(x*x)%M;
}
return tot;
} ll mod(ll x)
{
if (x<M) return x;
x-=M;
if (x<M) return x;
return x-M;
} ll count(ll x)
{
ll A=,B=,F,G;
for (ll i=; i<=c; i++)
{
if (x&(<<(i-)))
{
F=(f1[i]*(a[i]+))%M;
G=((f2[i]-)*(f3[i]))%M;
}
else
{
F=((f1[i]-)*(f3[i]))%M;
G=(f1[i]*(a[i]))%M;
}
A=(A*F)%M;
B=(B*G)%M; //cout<<" a: & b: "<<A<<' '<<B<<endl;
}
return mod(A+B);
} ll over=power(,M-); int main()
{
scanf("%lld",&t);
while (t--)
{
ans=;
scanf("%lld",&c);
for (ll i=; i<=c; i++) scanf("%lld%lld",&p[i],&a[i]);
for (ll i=; i<=c; i++)
{
f1[i]=power(p[i],a[i]);
f2[i]=power(p[i],a[i]+);
f3[i]=power(p[i]-,M-);
}
//ans=count(1<<(c)-1); cout<<"ans : "<<ans<<endl;
for (ll i=; i<(<<c); i++) ans=mod(ans+count(i));
ll tep=;
for (ll i=; i<=c; i++) tep=(tep*f1[i])%M;
ans=mod(ans+*tep);
ans=(ans*over)%M;
if (ans<) ans+=M;
printf("Case %lld: %lld\n",++cas,ans);
}
return ;
}
UVA12546_LCM Pair Sum的更多相关文章
- uva12546. LCM Pair Sum
uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...
- bzoj3114 LCM Pair Sum
题意:以质因数分解的方式给定n,求所有满足:lcm(a, b) = n的无序数对的价值和.其中(a, b)的价值为a + b 解: 定义首项为a,公比为q,项数为n的等比数列的和为getQ(a, q, ...
- light oj 1236 - Pairs Forming LCM & uva 12546 - LCM Pair Sum
第一题给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,……em, 则结果为((1+2*e1)*(1+2*e2)……(1+2*em)+1)/2. 代码如下: #include <st ...
- Subarray Sum Closest
Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...
- LeetCode 1099. Two Sum Less Than K
原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and inte ...
- [LC] 1099. Two Sum Less Than K
Given an array A of integers and integer K, return the maximum S such that there exists i < j wit ...
- 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- Java中的泛型 (上) - 基本概念和原理
本节我们主要来介绍泛型的基本概念和原理 后续章节我们会介绍各种容器类,容器类可以说是日常程序开发中天天用到的,没有容器类,难以想象能开发什么真正有用的程序.而容器类是基于泛型的,不理解泛型,我们就难以 ...
- 三维网格精简算法(Quadric Error Metrics)附源码
在计算机图形应用中,为了尽可能真实呈现虚拟物体,往往需要高精度的三维模型.然而,模型的复杂性直接关系到它的计算成本,因此高精度的模型在几何运算时并不是必须的,取而代之的是一个相对简化的三维模型,那么如 ...
随机推荐
- 10.11课后练习——MyOD系统调用版本
MyOD系统调用版本 题目要求 参考教材<深入理解计算机(第三版)>第十章内容 用Linux IO相关系统调用编写myod.c 用myod XXX实现Linux下od -tx -tc XX ...
- 20155320 2016-2017-2《Java程序设计》第十二周课堂实践项目
20155320 2016-2017-2<Java程序设计>第十二周课堂实践项目 1.修改教材P98 Score2.java, 让执行结果数组填充是自己的学号: 2.在IDEA中以TDD的 ...
- Swift - 重写导航栏返回按钮
// 重写导航栏返回按钮方法 func configBackBtn() -> Void { // 返回按钮 let backButton = UIButton(type: .custom) // ...
- day9 匿名函数 lambda
1. list列表排序 #### sort排序 nums = [,,,,,] nums.sort() print(nums) ### 结果 [, , , , , ] ######## 逆序 In [] ...
- 前端 layui
如果想实现类似 alert这种效果又不想阻塞html运行的话,就是用layer吧! http://www.layui.com/
- Zigbee系列(网络)
Zigbee设备类型 Coordinator:形成网络,选择信道.PANID.允许其他设备加入等. Router: 作为路由节点,转发报文. End Device: 终端节点,不转发报文. Zigbe ...
- Ubuntu 安装python后,安装python-dev
1.通常情况下: sudo apt install python-dev 或者 在 sudo apt install python 命令下安装应该也附带了 python-dev 上述 pyhthon ...
- webpack Error: Cannot find module 'webpack/lib/Chunk' Extract-text-webpack-plugin 分离CSS
深入浅出webpack 1.5章节使用Extract-text-webpack-plugin分离css 安装插件后打包提示错误 Error: Cannot find module 'webpack/l ...
- 微信小程序转换为百度小程序
据粗略预估,微信小程序和百度小程序,有至少90%以上的相似代码,而且api的参数和返回的数据都是一致的,有一些不一致的将做如下介绍:.wxml文件,改成后辍名.swan.wxss文件,改成后辍名为.c ...
- Java Basic&Security Tools
JDK Tools and Utilities Basic Tools These tools are the foundation of the JDK. They are the tools yo ...