LightOJ 1054 - Efficient Pseudo Code (求n^m的因子和)
来源:互联网 发布:在淘宝开店怎么样 编辑:程序博客网 时间:2024/05/10 21:10
Sometimes it's quite useful to write pseudo codes forproblems. Actually you can write the necessary steps to solve a particularproblem. In this problem you are given a pseudo code to solve a problem and youhave to implement the pseudo code efficiently. Simple! Isn't it? :)
pseudo code
{
take two integers n and m
let p = n ^ m (n tothe powerm)
let sum = summation of all thedivisors ofp
let result = sum MODULO1000,000,007
}
Now given n and m you have to find the desiredresult from the pseudo code. For example ifn = 12 and m = 2.Then if we follow the pseudo code, we get
pseudo code
{
take two integers n and m
so, n = 12 and m = 2
let p = n ^ m (n tothe powerm)
so, p = 144
let sum = summation of all thedivisors ofp
so, sum = 403, since the divisors ofp are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 36, 48, 72, 144
let result = sum MODULO1000,000,007
so, result = 403
}
Input
Input starts with an integer T (≤ 5000),denoting the number of test cases.
Each test case will contain two integers, n (1 ≤ n)and m (0 ≤ m). Each of n and m will be fit into a32bit signed integer.
Output
For each case of input you have to print the case number andthe result according to the pseudo code.
Sample Input
Output for Sample Input
3
12 2
12 1
36 2
Case 1: 403
Case 2: 28
Case 3: 3751
题意:看题就能知道了,求n^m的因子和mod1000000007
思路:卡时间,普通的分解因子不行,所以说打个素数表优化分解,然后优化下等比数列就好了
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1000100#define LL long long#define ll __int64#define INF 0x7fffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-10using namespace std;int gcd(int a,int b){return b?gcd(b,a%b):a;}int lcm(int a,int b){return a/gcd(a,b)*b;}LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}//headstruct s{int num;int cnt;}p[MAXN];int prime[MAXN];int v[MAXN];int q;LL fun(LL a,LL n,LL MOD){if(n==0)return 1;if(n%2)return ((1+powmod(a,n/2+1,MOD))%MOD*fun(a,n/2,MOD)%MOD)%MOD;elsereturn (powmod(a,n/2,MOD)+(1+powmod(a,n/2+1,MOD))%MOD*fun(a,(n-1)/2,MOD)%MOD)%MOD;}void db(){int i,j;q=0;mem(v);for(i=2;i<=100000;i++){if(!v[i]){prime[q++]=i;for(j=i*2;j<=100000;j+=i)v[j]=1;}}}int main(){db();LL a,b,i;int tt,cas=0;scanf("%d",&tt);while(tt--){scanf("%lld%lld",&a,&b);int x=a;int ccnt=0;for(i=0;i<q&&prime[i]*prime[i]<=x;i++){if(x%prime[i]==0){p[ccnt].num=prime[i];int t=0;while(x%prime[i]==0) x/=prime[i],t++;p[ccnt++].cnt=t;}if(x==1)break;}if(x!=1){p[ccnt].num=x;p[ccnt].cnt=1;ccnt++;}LL ans=1;for(i=0;i<ccnt;i++)ans=(ans*fun(p[i].num,p[i].cnt*b,1000000007))%1000000007;printf("Case %d: ",++cas);printf("%lld\n",ans);}return 0;}
- LightOJ 1054 - Efficient Pseudo Code (求n^m的因子和)
- lightoj 1054 - Efficient Pseudo Code 求所有n^m的所有因子和
- Light OJ 1054 Efficient Pseudo Code 求n^m的约数和
- LightOj 1054 Efficient Pseudo Code
- Light oj 1054 - Efficient Pseudo Code
- Light OJ 1054 - Efficient Pseudo Code
- 求n!中因子m的个数
- LightOJ 1054 求n^m%mod
- LightOJ 1028 求n的因子的个数
- LightOJ 1282 求n^m的高三位和低三位
- 求n的因子的个数和。.
- Lightoj 1067【逆元模板(求C(N,M))】
- poj1845求n的因子和
- LightOJ 1138 给出数字n,求m使得m!的末尾含有n个0
- 给出两个数m和n,求它们的最大公因子,即能够同时整出m和n的最大正整数
- 求数n和n!的因子和
- 求m,n的最小公倍数和最大公约数m>n
- 求m和n的算法题
- 面向对象程序设计
- 一起talk C栗子吧(第一百二十三回:C语言实例--显示变量和函数的地址)
- PB中treeview 的checkboxes用法完整版
- poj 1604
- 喊啥
- LightOJ 1054 - Efficient Pseudo Code (求n^m的因子和)
- 二次采样
- github入门实践(分支的相关操作)
- 使用Dictionary集合编写的通用的后台添加数据
- 转行自学android,Day6-real编程
- C++ const用法 尽可能使用const
- 指针与引用的区别
- SOCKET通信
- 弹窗式恶搞网页的设计与发布