zoukankan      html  css  js  c++  java
  • CSUOJ 1162 Balls in the Boxes 快速幂

    Description

    Mr. Mindless has many balls and many boxes,he wants to put all the balls into some of the boxes.Now, he wants to know how many different solutions he can have.
    you know,he could put all the balls in one box,and there could be no balls in some of the boxes.Now,he tells you the number of balls and the numbers of boxes, can you to tell him the number of different solutions? Because the number is so large, you can just tell him the solutions mod by a given number C.
    Both of boxes and balls are all different.

    Input

    There are multiple testcases. In each test case, there is one line cantains three integers:the number of boxes ,the number of balls,and the given number C separated by a single space.All the numbers in the input are bigger than 0 and less than 2^63.


    Output

     For each testcase,output an integer,denotes the number you will tell Mr. Mindless

    Sample Input

    3 2 4
    4 3 5

    Sample Output

    1
    4

    Hint

    简单题,快速幂

    /***************************************************/
    数据更新了就wa了!!!

    #include<stdio.h>
    typedef long long ll;
    ll quickmod(ll a, ll b, ll m)
    {
    	ll ans = 1;
    	while (b)
    	{
    		if (b & 1)
    		{
    			ans = (ans%m*a) % m;
    			b--;
    		}
    		b >>= 1;
    		a = a%m*a%m;
    	}
    	return ans%m;
    }
    int main()
    {
    	ll m, n, c;
    	while (~scanf("%lld%lld%lld", &n, &m, &c))
    	{
    		printf("%lld
    ", quickmod(n, m, c));
    	}
    	return 0;
    }
    /**********************************************************************
    	Problem: 1162
    	User: leo6033
    	Language: C++
    	Result: WA
    **********************************************************************/
    

    改成了unsigned long long以后直接快速幂  wa!!!

    然后看了别人的博客之后 用二分法实现乘法  这数据是要有多大!QAQ

    #include<stdio.h>
    typedef unsigned long long ll;
    ll mod_(ll a, ll b, ll m)
    {
    	if (b == 0)
    		return 0;
    	ll r = mod_(a, b / 2, m);
    	r = (r + r) % m;
    	if (b % 2)
    		r = (r + a) % m;
    	return r;
    }
    ll mod(ll a, ll b, ll c)
    {
    	if (b == 0)return 1;
    	ll r = mod(a, b / 2, c);
    	r = mod_(r, r, c);
    	if (b % 2)
    		r = mod_(r, a, c);
    	return r;
    }
    int main()
    {
    	ll m, n, c;
    	while (~scanf("%lld%lld%lld", &n, &m, &c))
    	{
    		printf("%lld
    ", mod(n, m, c));
    	}
    	return 0;
    }
    
    /**********************************************************************
    	Problem: 1162
    	User: leo6033
    	Language: C++
    	Result: AC
    	Time:28 ms
    	Memory:1120 kb
    **********************************************************************/
    


  • 相关阅读:
    JS BOM对象 History对象 Location对象
    JS 字符串对象 数组对象 函数对象 函数作用域
    JS 引入方式 基本数据类型 运算符 控制语句 循环 异常
    Pycharm Html CSS JS 快捷方式创建元素
    CSS 内外边距 float positio属性
    CSS 颜色 字体 背景 文本 边框 列表 display属性
    【Android】RxJava的使用(三)转换——map、flatMap
    【Android】RxJava的使用(二)Action
    【Android】RxJava的使用(一)基本用法
    【Android】Retrofit 2.0 的使用
  • 原文地址:https://www.cnblogs.com/csu-lmw/p/9124453.html
Copyright © 2011-2022 走看看