CSU 1803 2016(同餘公式)2016年湖南省第十二屆大學生電腦程式設計競賽

來源:互聯網
上載者:User

標籤:

題意
給出正整數 n 和 m,統計滿足以下條件的正整數對 (a,b) 的數量:
1. 1 ≤ a ≤ n, 1 ≤ b ≤ m;
2. a×b 是 2016 的倍數。

範例輸入
32 63
2016 2016
1000000000 1000000000

範例輸出
1
30576
7523146895502644

思路
由同餘公式可得
a * b % 2016 = (a % 2016) * (b % 2016) % 2016
所以如果 x*y 是2016的倍數的話,那麼(2016*k + x)*y也是
那麼只需要統計1-n的各個數模2016的餘數的出現次數,就可以把枚舉時的迴圈次數縮小到2016*2016。

 1 #include <cstdio> 2 typedef long long int LL; 3 const int N = 2016; 4 int a[N], b[N]; 5 LL ans; 6 int main(void) 7 { 8     int n, m, nx, mx; 9     while(~scanf("%d %d", &n, &m))10     {11         nx = n / N;12         mx = m / N;13         for(int i=0; i<N; ++i)14             a[i] = nx, b[i] = mx;15         nx = n % N;16         mx = m % N;17         for(int i=1; i<=nx; ++a[i++]);18         for(int i=1; i<=mx; ++b[i++]);19         ans = 0;20         for(int i=0; i<N; ++i)21             for(int j=0; j<N; ++j)22                 if(i*j%N == 0)23                     ans += (LL)a[i] * b[j];24         printf("%lld\n", ans);25     }26     return 0;27 }

 

CSU 1803 2016(同餘公式)2016年湖南省第十二屆大學生電腦程式設計競賽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.