主题链接:点击打开链接

特定n*m矩阵,[i,j]分值为gcd(i,j)

给定一个k长的序列,问能否匹配上 矩阵的某一行的连续k个元素

思路:

我们要求出一个解(i,j) 使得 i<=n && j<=m 此时输出 YES

对于j

j % b[0] = 0

j+1 % b[1] = 0

···

j+l % b[l] = 0

依据定理:若 a == b (mod n) => (a+c) == b+c (mod n)

所以将上式变换为

j % b[0] = 0

j % b[1] = -1

···

j % b[n-1] = - (n-1)

最后求出的i,j 检验一下是否满足 gcd(i,j+k) == input[k];

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
#define ll __int64
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
void extend_gcd (ll a , ll b , ll& d, ll &x , ll &y) {
if(!b){d = a; x = 1; y = 0;}
else {extend_gcd(b, a%b, d, y, x); y-=x*(a/b);}
}
ll china(ll l, ll r, ll *m, ll *a){ //下标[l,r] 方程x%m=a;
ll lcm = 1;
for(ll i = l; i <= r; i++)lcm = lcm/gcd(lcm,m[i])*m[i];
for(ll i = l+1; i <= r; i++) {
ll A = m[l], B = m[i], d, x, y, c = a[i]-a[l];
extend_gcd(A,B,d,x,y);
if(c%d)return -1;
ll mod = m[i]/d;
ll K = ((x*c/d)%mod+mod)%mod;
a[l] = m[l]*K + a[l];
m[l] = m[l]*m[i]/d;
}
if(a[l]==0)return lcm;
return a[l];
}
#define N 10005
ll n[N],b[N],tmp[N],len,nn,mm;
bool work(){
memset(b, 0, sizeof b);
memcpy(tmp,n,sizeof n);
ll i = china(1,len,n,b);
if(i>nn || i<=0)return false;
for(ll hehe = 1; hehe <= len; hehe++)b[hehe] = -hehe+1;
memcpy(n,tmp, sizeof n);
ll j = china(1,len,tmp,b);
if(j+len-1>mm || j<=0)return false;
for(ll hehe = 1; hehe <= len; hehe++)if(gcd(i,j+hehe-1)!=n[hehe])return false;
return true;
}
int main(){
while(cin>>nn>>mm>>len){
for(ll i = 1; i <= len; i++) cin>>n[i];
work()? puts("YES"):puts("NO");
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

Codeforces 338D GCD Table 中国剩余定理的更多相关文章

  1. codeforces 338D GCD Table

    什么都不会只能学数论QAQ 英文原题不贴了 题意: 有一张N*M的表格,i行j列的元素是gcd(i,j)读入一个长度为k,元素大小不超过10^12的序列a[1..k],问这个序列是否在表格的某一行中出 ...

  2. gcd,扩展欧几里得,中国剩余定理

    1.gcd: int gcd(int a,int b){ ?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题: ...

  3. [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...

  4. (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)

    前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...

  5. hihocode 九十七周 中国剩余定理

    题目1 : 数论六·模线性方程组 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:今天我听到一个挺有意思的故事! 小Hi:什么故事啊? 小Ho:说秦末,刘邦的将军 ...

  6. hdu 3579 Hello Kiki 不互质的中国剩余定理

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  7. POJ1006——中国剩余定理

    题目:http://poj.org/problem?id=1006 中国剩余定理:x= m/mj + bj + aj 讲解:http://www.cnblogs.com/MashiroSky/p/59 ...

  8. 数论E - Biorhythms(中国剩余定理,一水)

    E - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  9. POJ1006 Biorhythms —— 中国剩余定理

    题目链接:https://vjudge.net/problem/POJ-1006 Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total ...

随机推荐

  1. Context Switch and System Call

    How many Context Switches is “normal”? This depends very much on the type of application you run. If ...

  2. MySQL 触发器例子(两张表同步增加和删除)

    以下两个例子来自:http://www.cnblogs.com/nicholas_f/archive/2009/09/22/1572050.html实测有效,但是原帖的分隔符不正确,因此稍作修改.其中 ...

  3. Windows phone 8 学习笔记(3) 通信

    原文:Windows phone 8 学习笔记(3) 通信 Windows phone 8 可利用的数据通信方式比较广泛,在硬件支持的前提下,我们可以利用WiFi.蓝牙.临近感应等多种方式.数据交互一 ...

  4. MyEclipse中“擅自乱改”项目名导致项目报错的处理

    最近几天培训的过程中,经常有同学手一抖,默默的修改了本来配置部署好的项目名,导致项目报错…… 遇到这种事情,我一般会做的处理就是重新新建项目,然后把包和各种文件ctrl+c ctrl+v,遇到项目小还 ...

  5. windows phone 使用相机并获取图片(3)

    原文:windows phone 使用相机并获取图片(3) 使用相机需要引用如下命名空间 " Margin="12,10,12,0" ></Image> ...

  6. Scala Hello 示例

    object ScalaDemo1 {   def main(args: Array[String]) {     println("Hello,world!");   } }

  7. POJ 1002 487-3279 Trie解读

    这个问题的解决方法是多种多样的.如本文所用,Trie为了解决这个问题. 它也可用于hash表.map等解决方案,由于输入是特定7数字,因此,你应该能够解决. 如本文所用,Trie不是非常快.最后,我主 ...

  8. 递归遍历XML所有节点

    package xml; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.DocumentEx ...

  9. javascript---在自由落体实现

    实现一些简单的物业自由落体需要理解: clientHeight:浏览器客户机的整体高度 offsetHeight:物(实例div)高低 offsetTop:从对象client最顶层的距离 简单demo ...

  10. document.getElementById()使用方法

    document.getElementById使用 语法:oElement = document .getElementById ( sID ) 參数:sID――必选项. 字符串 (String) . ...