Raising Modulo Numbers
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5500   Accepted: 3185

Description

People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that this market segment
was so far underestimated and that there is lack of such games. This kind of game was thus included into the KOKODáKH. The rules follow: 



Each player chooses two numbers Ai and Bi and writes them on a slip of paper. Others cannot see the numbers. In a given moment all players show their numbers to the others. The goal is to determine the sum of all expressions AiBi from all players
including oneself and determine the remainder after division by a given number M. The winner is the one who first determines the correct result. According to the players' experience it is possible to increase the difficulty by choosing higher numbers. 



You should write a program that calculates the result and is able to find out who won the game. 


Input

The input consists of Z assignments. The number of them is given by the single positive integer Z appearing on the first line of input. Then the assignements follow. Each assignement begins with line containing an integer M (1 <= M <= 45000). The sum will be
divided by this number. Next line contains number of players H (1 <= H <= 45000). Next exactly H lines follow. On each line, there are exactly two numbers Ai and Bi separated by space. Both numbers cannot be equal zero at the same time.

Output

For each assingnement there is the only one line of output. On this line, there is a number, the result of expression 

(A1B1+A2B2+ ... +AHBH)mod M.

Sample Input

3
16
4
2 3
3 4
4 5
5 6
36123
1
2374859 3029382
17
1
3 18132

Sample Output

2
13195
13


#include<stdio.h>
#include<string.h>
int ksm(long long a,long long b,long long n)
{
long long ans=1;
while(b)
{
if(b&1)
ans=ans*a%n;
//写成ans*=a%n不行...输出错误
a=a*a%n;
b>>=1;
}
return ans;
}
int main()
{
int z,h,m,u,i;
scanf("%d",&z);
while(z--)
{
int sum = 0;
scanf("%d",&m);
scanf("%d",&h);
while(h--)
{
scanf("%d%d",&u,&i);
sum += ksm(u,i,m);
sum %= m;
}
printf("%d\n",sum);
}
return 0;
} /*************************************************************/ #include<stdio.h>
#include<string.h>
int main()
{
long long z,h,m,a,b,ans;
scanf("%lld",&z);
while(z--)
{
int sum = 0;
scanf("%lld",&m);
scanf("%lld",&h);
while(h--)
{
scanf("%lld%lld",&a,&b);
ans = 1;
while(b)
{
if(b&1)
ans=ans*a%m;
a=a*a%m;
b>>=1;
}
sum += ans;
sum %= m;
}
printf("%d\n",sum%m);
}
return 0;
}



最新文章

  1. oracle查询包含某个字段的表
  2. .NetCore~Linux环境下部署
  3. GZFramework代码生成器插件使用教程
  4. SQL Server 2012 各版本功能比较
  5. HYSBZ 1269文本编辑器 splay
  6. 使用JavaScript闭包,以工厂模式实现定时器对象
  7. graph_tool源码及其注释
  8. mysql添加用户和用户权限
  9. 9款完美体验的HTML5/jQuery应用
  10. arrayAccess的接口使用
  11. TCP协议三次握手过程分析
  12. (Android) Chinese Character
  13. 创建Activity
  14. 2000W条数据,加入全文检索的总结
  15. #Python3.6.2(32位) pip安装 和 pygame 环境配置
  16. IDEA 创建包和类及基本操作
  17. scrapy选择器归纳
  18. Python Machine Learning-Chapter3
  19. 深入Spring Boot:怎样排查expected single matching bean but found 2的异常
  20. 微信WeixinJSBridge API使用实例

热门文章

  1. 洛谷 P1211 [USACO1.3]牛式 Prime Cryptarithm
  2. 常用加密算法的Java实现总结(二)
  3. node event中 on emit off 的封装
  4. javascript的组成
  5. &lt;一&gt; 爬虫的原理
  6. 想在子线程里面触发的信号的槽函数在子线程执行,信号槽连接必须使用DirectConnection 方式(即使跨线程,也可以强迫DirectConnection,而不能是AutoConnection)
  7. 【z02】选择客栈
  8. Android 自己定义ViewGroup 实战篇 -&amp;gt; 实现FlowLayout
  9. 《SPA设计与架构》之MV*框架
  10. Loader之一:基本原理 分类: H1_ANDROID 2013-11-16 10:29 1923人阅读 评论(0) 收藏