Frogs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1509    Accepted Submission(s): 498

Problem Description
There are m stones lying on a circle, and n frogs are jumping over them.
The stones are numbered from 0 to m−1 and the frogs are numbered from 1 to n. The i-th frog can jump over exactly ai stones in a single step, which means from stone j mod m to stone (j+ai) mod m (since all stones lie on a circle).

All frogs start their jump at stone 0, then each of them can jump as many steps as he wants. A frog will occupy a stone when he reach it, and he will keep jumping to occupy as much stones as possible. A stone is still considered ``occupied" after a frog jumped away.
They would like to know which stones can be occupied by at least one of them. Since there may be too many stones, the frogs only want to know the sum of those stones' identifiers.

 
Input
There are multiple test cases (no more than 20), and the first line contains an integer t,
meaning the total number of test cases.

For each test case, the first line contains two positive integer n and m - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109).

The second line contains n integers a1,a2,⋯,an, where ai denotes step length of the i-th frog (1≤ai≤109).

 
Output
For each test case, you should print first the identifier of the test case and then the sum of all occupied stones' identifiers.
 
Sample Input
3
2 12
9 10
3 60
22 33 66
9 96
81 40 48 32 64 16 96 42 72
 
Sample Output
Case #1: 42
Case #2: 1170
Case #3: 1872
 
Source
题意:有n只青蛙,m个石头(围成圆圈)。第i只青蛙每次只能条ai个石头,问最后所有青蛙跳过的石头的下标总和是多少?
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=2*1e5+10;
int factor[N],num[N],appear[N],step[N];
ll add[N]; int gcd(int a,int b)
{
if(b==0) return a;
else return gcd(b,a%b);
} int main()
{
int cas,n,m,kk=0;
SC("%d",&cas);
while(cas--){
SC("%d%d",&n,&m);
int cnt=0;MM(num,0);MM(appear,0);
for(int i=1;i<=n;i++) SC("%d",&step[i]);
for(int i=1;i*i<=m;i++) if(m%i==0){
factor[++cnt]=i;
if(i*i!=m) factor[++cnt]=m/i;
}
sort(factor+1,factor+cnt+1);
cnt--;
for(int i=1;i<=cnt;i++){
ll k=(m-1)/factor[i];
add[i]=k*(k+1)/2*factor[i];
} for(int i=1;i<=n;i++){
int k=gcd(step[i],m);
for(int j=1;j<=cnt;j++)
if(factor[j]%k==0) appear[j]=1;
} ll ans=0;
for(int i=1;i<=cnt;i++) if(num[i]!=appear[i]){
ans+=add[i]*(appear[i]-num[i]);
for(int j=i+1;j<=cnt;j++)
if(factor[j]%factor[i]==0) num[j]+=(appear[i]-num[i]);
}
printf("Case #%d: %lld\n",++kk,ans);
}
return 0;
}

  分析:

1.一个数的因子个数大概是log级别;

2.ax+by=c有非负整数解的条件是c%gcd(a,b);取余

=>ax+by=k*gcd(a,b) =>ax%b=k*gcd(a,b)%b =>ai*x%m=k*gcd(ai,m)%m;

所以,青蛙能走到的格子数是k*gcd(ai,m),而gcd(ai,m)又必然是m的因数,所以可以先分解出m的因子

3.但是因为有些格子可能同时被多只青蛙走,因此需要容斥一下,设appear[i]为格子i应该走的次数(只有0,1)两个值,num[i]为格子i到当前为止实际走的次数,如果当前appear[i]>appear[i],就需要加,否则减去,然后将是当前因子factor[i]倍数的因子也同时跟新num值

最新文章

  1. linux安装中文字体
  2. 烂泥:Postfix邮件服务器搭建之软件安装与配置
  3. go语言 新手学习笔记 go基础教程
  4. .NET中六个重要的概念:栈、堆、值类型、引用类型、装箱和拆箱 (转)
  5. Firefox便携版
  6. 标签(Tag)的各种设计方案
  7. BCryptPasswordEncoder加密及判断密码是否相同
  8. Docker之容器
  9. Mac上使用jenkins+ant执行第一个程序
  10. 元素化设计原理及规则v1.0
  11. 【shiro】(3)---了解Shiro
  12. will-change属性
  13. Linux使用netstat命令查看并发连接数
  14. Spring导出可以运行的jar包
  15. 为什么要用 Node.js
  16. Git(五)IDEA应用Git
  17. Chrome浏览器查看 iframe信息 OpenFrame
  18. fiddler script建议教程
  19. Windows10电脑安装macOS Mojave系统的方法(最新版系统,含超详细步骤截图)
  20. 如何使用Navicat连接Oracle

热门文章

  1. 基于docker的mongodb安装以及PHP使用
  2. python-----模块【第一部分】-----
  3. 【Python基础】14_Python中的TODO注释
  4. js 替换html
  5. c#本地文件配置xml
  6. iview表单验证数字
  7. JS基础_函数的返回值
  8. 9.动态SQL
  9. spring boot 的一些高级用法
  10. 【坑】Mybatis原始获取配置方式,获取配置失败