1. 程式人生 > >codeforces 117B B. Very Interesting Game(同餘模定理+取模導致的迴圈節)

codeforces 117B B. Very Interesting Game(同餘模定理+取模導致的迴圈節)

題目連結:

題目大意:

給出三個數a,b,m,將不大於a,b的兩個數連線後得到一個新的數,如果這個新的數能夠整除m,那麼2贏,否則2贏。

題目分析:

因為取模會導致出現迴圈節,所以如果給出的資料範圍大於mod,那麼我們可以只考慮一個迴圈節,那麼我們只需要預處理出b能夠通過得到的值,然後列舉A,判斷是否能出現一個A不能和任何一個B組合除能被mod整除的情況,那麼這個數就是最小解,如果迴圈節中都沒有出現可行解,那麼就是誤解了。

AC程式碼:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; typedef long long LL; const LL MAX = 1e7+7; const LL xx = 1e9; LL a,b,m; int dp[MAX]; char s[12]; int main ( ) { while ( ~scanf ( "%lld%lld%lld" , &a , &b , &m ) ) { memset ( dp , 0 , sizeof ( dp ) ); LL lim = min ( b , m ); for
( int i = 0 ; i <= lim ; i++ ) dp[i%m] = 1; lim = min ( a , m); LL ans = -1; for ( LL i = 0 ; i <= lim ; i++ ) { LL x = i*xx%m; x = (m-x)%m; if ( !dp[x] ) { ans = i; break
; } } if ( ans == -1 ) puts ( "2" ); else { printf ( "1 " ); s[9] = 0; int n = 8; for ( int i = 0 ; i < 9 ; i++ ) s[i] = '0'; while ( ans ) { s[n--] = ans%10+48; ans /= 10; } puts ( s ); } } }

相關推薦

codeforces 117B B. Very Interesting Game(定理+導致迴圈)

題目連結: 題目大意: 給出三個數a,b,m,將不大於a,b的兩個數連線後得到一個新的數,如果這個新的數能夠整除m,那麼2贏,否則2贏。 題目分析: 因為取模會導致出現迴圈節,所以如果給出的資料範圍大於mod,那麼我們可以只考慮一個迴圈節,那

A - Find a Number CodeForces - 1070A -記憶化廣搜-定理

A - Find a Number  CodeForces - 1070A  題意: 思路:與數位dp思想有點相似,數太大無法儲存,選擇保留其對搜尋目標有用的資訊,並且記憶化減少無效多餘搜尋, vis[i][j]兩位含義是 當前的 餘數為i,各個

中國剩餘定理求解線性方程組(數互素和非互素的情況)

中國剩餘定理      中國剩餘定理是中國古代求解一次同餘方程組的方法,是數論中的一個重要定理。      設m1,m2,m3,...,mk是兩兩互素的正整數,即gcd(mi,mj)=1,i!=j,i,j=1,2,3,...,k. 則同餘方程組: x = a1

Codeforces 519D A and B and Interesting Substrings(二維map+前綴和)

substring open 一段 ast n) 題目 rest ons 記錄 題目鏈接:http://codeforces.com/problemset/problem/519/D 題目大意:給你一串字符串s僅由小寫字母組成,並且對於‘a‘~‘z‘都給了一個值。求子串t

codeforces 754 B Ilya and tic-tac-toe game

題目描述:   Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. He played a lot of games today

CodeForces 17D Notepad(定理)

D. Notepad time limit per test 2 seconds memory limit per test 64 megabytes input standa

codeforces#687 B. Remainders Game

out true its rem space == tor con fin 題意:給出n個數,和一個數p,問你在知道 x%ai 的情況下,能不能確定x%p的值 結論:當n個數的最小公倍數是p的倍數時,可以確定 代碼: #include <bits/stdc++.h

exgcd 解方程ax=b(%n)

ax=n(%b)  ->   ax+by=n 方程有解當且僅當 gcd(a,b) | n ( n是gcd(a,b)的倍數 ) exgcd解得 a*x0+b*y0=gcd(a,b) 記k=n/gcd(a,b) 則方程ax+ny=b的所有解為 x=k*x0 +  [

HDU 2035 人見人愛A^B基本性質)

原題連結:Here!思路:可以利用同餘的基本性質 ab ≡ (a%m)(b%m) (mod m) ,也非常好證明。  設 a = k1*m + c1 , b = k2*m + c2 ( k1,k2

擴充套件gcd以及方程ax=b(mod M)

關於擴充套件gcd其實沒有必要搞懂,背下來就好了如果不會的自行學習 對於方程ax=b(mod M),我們可以將其化簡成為ax+My=b,讓後用擴充套件gcd求解 當b|r=gcd(a,M)時,方程有r

[CodeForces - 614B] B - Gena's Code

std specified note doesn fin imp order codeforce sed A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree

CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

scanf 如何 about graph its pri include art int 思路來自這裏,重點大概是想到建樹和無解情況,然後就變成樹形DP了- - /* CodeForces 840B - Leha and another game about gra

Codeforces 841D Leha and another game about graph - 差分

新的 number == 處理 ace ood int 多余 一點 Leha plays a computer game, where is on each level is given a connected graph with n vertices and m e

codeforces 862B B. Mahmoud and Ehab and the bipartiteness

com set pac 並且 復雜 oid include ces http http://codeforces.com/problemset/problem/862/B 題意: 給出一個有n個點的二分圖和n-1條邊,問現在最多可以添加多少條邊使得這個圖中不存在自環,重邊,

Codeforces Round #436 A. Fair Game

set string ems 題意 include 代碼 example return round 題意:給你n張卡片,上面寫有數字,兩個人選擇兩個數字,把相同數字的卡片都拿走,問能不能拿走所有的卡片並且兩個人拿的卡片書相同。 Examples Input 4112727

Codeforces 850C Arpa and a game with Mojtaba

得到 cond get 每次 while %d mem sizeof void 題意:給定一個正整數序列,兩人輪流對這個數列進行如下修改:選取一個素數p和一個整數k將序列中能整除p^k的數除以p^k,問誰有必勝策略。 借此復習一下sg函數吧,sg(x) = mex (

Codeforces 805A/B/C

bsp 正整數 void 問題 最短 最短路 char tar logs A. Fake NP 傳送門:http://codeforces.com/contest/805/problem/A 本題是一個數學問題。 給定兩個正整數l,r(l≤r),對於區間[l..r]上的任一

Codeforces 816C/815A - Karen and Game

操作 判斷 spa contest printf define font 技術 大於 傳送門:http://codeforces.com/contest/816/problem/C 本題是一個模擬問題。 有一個n×m的矩陣。最初,這個矩陣為零矩陣O。現有以下操作: a.行

Codeforces 851A/B/C

-a font 必要條件 for efi ces bool rcc 空間 A. Arpa and a research in Mexican wave 傳送門:http://codeforces.com/contest/851/problem/A 水題,參考程序如下: #

CodeForces 884A.B.C Book Reading(水) | Japanese Crosswords Strike Back(水) | Bertown Subway(DFS)

有序 req air 改變 rain ont ring cal word Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book